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!
Printable View
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!
EDIT: removed the std namespace qualifier from sqrt. Force of habit, I guessCode:#include <cmath>
int main() {
double result;
result = sqrt(9);
}
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();
}
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.Code:d = b*b - 4 * a * c;
cout<< ( -b+sqrt(d) ) / ( 2 * a );
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();
}
Are the numbers you're using to test it actually valid (i.e. is b^2-4ac > 0)?
Some suggestions:
- Read the link that indigo0086 posted.
- Study the output of the following code:
Code:#include <iostream>
#include <cmath>
int main()
{
std::cout << sqrt(-1.0) << std::endl;
return 0;
}
Thanks guys. I realized that "d" just had to be positive or 0! Just had to type in the right numbers ! :)
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
They don't have to be greater than zero! Use your imaginationQuote:
Originally Posted by XSquared
Well, they do twomers :)
You get a std::domain_error otherwise
EDIT: err... unless you mean they can be greater or equal to zero
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).Quote:
Originally Posted by twomers
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).
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 itQuote:
Originally Posted by Mario F.
EDIT - Mario, what're you doing up at 2:36 AM???
In my experience, complex roots (with both a real and imaginary component) are much more common than either real or imaginary roots.Quote:
Originally Posted by twomers
I see I need to drag-select the whole of your post everytime you say something that just doesn't sound right :p
I'm coding. What else? Best time of day... or night. Everything is quiet and nobody is bothering me. Even the gay troll decided to give it a rest.
Just hit ctrl-a and then you can see all invisible text throughout the whole page. :)Quote:
I see I need to drag-select the whole of your post everytime you say something that just doesn't sound right :p
No way. Y'all dun have ta pay fer his sins :)Quote:
Originally Posted by joeprogrammer