Menu

1.#IND as an output

2005-02-24
2012-09-26
  • Nobody/Anonymous

    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;

    cout &lt;&lt; &quot;Enter a value for coefficient \&quot;a\&quot;: &gt; &quot;;
    cin &gt;&gt; a;
    cout &lt;&lt; &quot;Enter a value for coefficient \&quot;b\&quot;: &gt; &quot;;
    cin &gt;&gt; b;
    cout &lt;&lt; &quot;Enter a value for coefficient \&quot;c\&quot;: &gt; &quot;;
    cin &gt;&gt; c;
    
    sq = sqrt (pow(b,2) - (4*a*c));
    
    x = (-b + sq) / 2 * a;
    x2 = (-b - sq) / 2* a;
    
    cout &lt;&lt; &quot;Here is the first answer: &quot; &lt;&lt; x &lt;&lt; endl;
    cout &lt;&lt; &quot;Here is the other answer: &quot; &lt;&lt; x2 &lt;&lt; endl;
    
    cin.get ();
    cin.get ();
    return 0;
    

    }

     
    • Ian Walker

      Ian Walker - 2005-02-24

      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

       
    • Stepho

      Stepho - 2005-02-24

      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.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.