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!
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); }
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.
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 );
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
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)?
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
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; }
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
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 imaginationOriginally 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
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.
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).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 itOriginally 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.Originally Posted by twomers