Sqareroot and squared

This is a discussion on Sqareroot and squared within the C++ Programming forums, part of the General Programming Boards category; Hey everybody. I'm new to programming (still ) I was trying to make some calcuators, but realised that i didn't ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    7

    Question Sqareroot and squared

    Hey everybody.

    I'm new to programming (still )
    I was trying to make some calcuators, but realised that i didn't know how to use squareroot ( if possible ) can anybody please tell me !

    Thanks!

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    Code:
    #include <cmath>
    
    int main() {
        double result;
    
        result = sqrt(9);
    }
    EDIT: removed the std namespace qualifier from sqrt. Force of habit, I guess
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    Thanks.. i have now included the sqrt command in my code. Still it doesn't put out the number i want it to ( actually no number is calculated )
    I would appreciate any kind of help, especially if i have made any mistakes.
    Thanks!

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    double a;
    double b;
    double c;
    double d;
    
    int main ()
    {
    cout<<"a*x^2 + b*x + c = 0";
    cin.get();
    cout<<"a: ";
    cin>> a;
    cout<<"b: ";
    cin>> b;
    cout<<"c: ";
    cin>> c;
    d = b*b - 4 * a * c;
    cout<< ( -b+sqrt(d) ) / ( 2 * a );
    cin.get();
    }

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    d = b*b - 4 * a * c;
    cout<< ( -b+sqrt(d) ) / ( 2 * a );
    Try changing the 4 to 4.0, and the 2 to 2.0. I believe you may be running into issues where numbers are being converted to integers.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

  6. #6
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    Tried to change numbers to 2.0 and 4.0, and added cin.ignore();
    but when i type in all numbers it says:" -1.#IND "
    Anybody know what to do?

    Updated code:
    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    double a;
    double b;
    double c;
    double d;
    
    int main ()
    {
    cout<<"a*x^2 + b*x + c = 0";
    cin.get();
    cout<<"a: ";
    cin>> a;
    cin.ignore();
    cout<<"b: ";
    cin>> b;
    cin.ignore();
    cout<<"c: ";
    cin>> c;
    cin.ignore();
    d = b*b - 4.0 * a * c;
    cout<< (( -b+sqrt(d) ) / ( 2.0 * a ));
    cin.get();
    }

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Are the numbers you're using to test it actually valid (i.e. is b^2-4ac > 0)?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Some suggestions:
    1. Read the link that indigo0086 posted.
    2. Study the output of the following code:
      Code:
      #include <iostream>
      #include <cmath>
      
      int main()
      {
         std::cout << sqrt(-1.0) << std::endl;
         return 0;
      }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  9. #9
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    Thanks guys. I realized that "d" just had to be positive or 0! Just had to type in the right numbers !

  10. #10
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    maybe you can ensure d is positive by doing something like

    Code:
        do
        {
            cout << "a: ";
            cin >> a;
            cout << endl <<"b: ";
            cin >> b;
            d = b*b - 4.0 * a * c;
        }
        while(d < 0);
        //do the rest

  11. #11
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Quote Originally Posted by XSquared
    Are the numbers you're using to test it actually valid (i.e. is b^2-4ac > 0)?
    They don't have to be greater than zero! Use your imagination

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    Well, they do twomers
    You get a std::domain_error otherwise

    EDIT: err... unless you mean they can be greater or equal to zero
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,527
    Quote Originally Posted by twomers
    They don't have to be greater than zero! Use your imagination
    Mathematically, that's true. However, your computer is not a mathematician. The standard sqrt() in <cmath> or <math.h> only yields a valid value if it's argument is non-negative (>= 0).

    If you want to handle cases where b*b-4a*c < 0 you need to write your program so it (effectively) handles complex values. Or, more simply, use the std::complex<double> type; in that case, you will not have to worry about the check (as std::sqrt works with the std::complex types).

  14. #14
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Quote Originally Posted by Mario F.
    Well, they do twomers
    You get a std::domain_error otherwise

    EDIT: err... unless you mean they can be greater or equal to zero
    In practise, unfortunately, the roots don't seem to be real. Imaginary (as you'll see if you look closely in my post ), roots are very common, so much so, that you don't expect to find real ones. The bane of little i, or j as us elec engers call it

    EDIT - Mario, what're you doing up at 2:36 AM???

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,527
    Quote Originally Posted by twomers
    Imaginary (as you'll see if you look closely in my post ), roots are very common, so much so, that you don't expect to find real ones. The bane of little i, or j as us elec engers call it
    In my experience, complex roots (with both a real and imaginary component) are much more common than either real or imaginary roots.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21