1. ## 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. Code:
```#include <cmath>

int main() {
double result;

result = sqrt(9);
}```
EDIT: removed the std namespace qualifier from sqrt. Force of habit, I guess

3. 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. 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.

5. 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();
}```

6. Are the numbers you're using to test it actually valid (i.e. is b^2-4ac > 0)?

7. Some suggestions:
2. Study the output of the following code:
Code:
```#include <iostream>
#include <cmath>

int main()
{
std::cout << sqrt(-1.0) << std::endl;
return 0;
}```

8. Thanks guys. I realized that "d" just had to be positive or 0! Just had to type in the right numbers !

9. 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```

10. 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

11. Well, they do twomers
You get a std::domain_error otherwise

EDIT: err... unless you mean they can be greater or equal to zero

12. 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).

13. 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???

14. 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.