Hey everyone,
I was writing a program that solves a quadratic equation when I ran into a weird error. When entering certain values, the answer to the quadratic equation would be shown as "-1.#IND" or "1.IND". I noticed that the problem only arose when I used this line in my source:
sq = sqrt ((bb) - (4a*c));
Instead of this:
sq = sqrt (pow(b,2) - (4ac));
It seems that the "pow" function is required to make this program work. Does anyone know what "1.#IND" is?
Thanks,
Evan
BTW, here is my working source (ignore the "sig" function; that is my signature function.):
include <iostream>
include <string>
using namespace std;
int main () {
double a, b, c, x, x2, sq;
a = b = c = x = 0;
The same runtime library also helpfully provides the _isnan function which checks to see if a number is of this type. So you could modify your result display code thus:
if(_isnan(sq))
{
cout << "No real roots" << endl;
} else {
cout << "Here is the first answer: " << x << endl;
cout << "Here is the other answer: " << x2 << endl;
}
Hope this helps.
Ian
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I think this #IND thing happens when the chosen coefficients lead to calculating the square root of a negative number. Which is not valid when working with "reals".
May be you have mistaken something in the line :
sq = sqrt (pow(b,2) - (4ac));
Or you need to work with complex numbers (I've never done it in C, so I can't help there)
In order to make your code compile, I had to add #include <cmath>. And also, b*b or pow(b,2) gives the same results.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hey everyone,
I was writing a program that solves a quadratic equation when I ran into a weird error. When entering certain values, the answer to the quadratic equation would be shown as "-1.#IND" or "1.IND". I noticed that the problem only arose when I used this line in my source:
sq = sqrt ((bb) - (4a*c));
Instead of this:
sq = sqrt (pow(b,2) - (4ac));
It seems that the "pow" function is required to make this program work. Does anyone know what "1.#IND" is?
Thanks,
Evan
BTW, here is my working source (ignore the "sig" function; that is my signature function.):
include <iostream>
include <string>
using namespace std;
int main () {
double a, b, c, x, x2, sq;
a = b = c = x = 0;
}
1.#IND is an "indefinite", which means what it says - you are trying to take the real root of a negative number, which is undefined.
When you use the sqrt function, the compiler calls the MSVCRT runtime library, which is documented at http://msdn.microsoft.com/library/en-us/vclib/html/_crt_sqrt.asp
The same runtime library also helpfully provides the _isnan function which checks to see if a number is of this type. So you could modify your result display code thus:
if(_isnan(sq))
{
cout << "No real roots" << endl;
} else {
cout << "Here is the first answer: " << x << endl;
cout << "Here is the other answer: " << x2 << endl;
}
Hope this helps.
Ian
I think this #IND thing happens when the chosen coefficients lead to calculating the square root of a negative number. Which is not valid when working with "reals".
May be you have mistaken something in the line :
sq = sqrt (pow(b,2) - (4ac));
Or you need to work with complex numbers (I've never done it in C, so I can't help there)
In order to make your code compile, I had to add #include <cmath>. And also, b*b or pow(b,2) gives the same results.