By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,694 Members | 1,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,694 IT Pros & Developers. It's quick & easy.

Windows program gives me -1.#IND as a number result. What the heck is that?

P: 6
Hello all!
First I would like to thank all of those who helped me regarding my first assignment...I'm much better with my semicolons now.

A couple assignments later...I'm having problems again. I'm supposed to create a very simple C++ program calculating the answers to a quadratic equation in form a(x^2) + b(x) + c. I thought i had no problems typing the code, but when I test 1, 2 and 3 for a, b, and c variables respectively, I get "-1.#IND" as my answer when it should be -1. What's going on??

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std; 
  5.  
  6. int main()
  7. {
  8.   double a = 0;
  9.   double b = 0;
  10.   double c = 0;
  11.  
  12.   cout << "The current value of a is "<<a << endl;
  13.   cout << "The current value of b is "<<b << endl;
  14.   cout << "The current value of c is "<<c << endl;
  15.  
  16.   cout << "Enter the coefficients a, b, and c for the quadratic equation in the form"
  17.        << endl;
  18.   cout << "a(x^2) + b(x) + c" << endl;
  19.   cout << "For example, if the equation is 2(x^2) + 3(x) + 4" << endl;
  20.   cout << "Enter as 2.0 3.0 4.0" << endl;
  21.  
  22.   cin >> a >> b >> c; 
  23.  
  24.   cout << "Your expression is "<<a<<"(x^2) + "<<b<<"x + "<<c<< endl;
  25.  
  26.   double factor = a * c;
  27.  
  28.   double x1 = 1;
  29.   double x2 = ((-1*b) - sqrt(pow(b,2.0) - 4*a*c))/(2*a);
  30.  
  31.   cout << x1 << endl;
  32.   cout << x2 << endl;
  33.  
  34.  
  35.   system("PAUSE");
  36.   return 0;
  37. }
I'm focussing on the x2 equation here...x1 is just to test to see if the value pops up okay and it does. In the x2 equation, it seems that the 4*a*c is causing the -1.#IND I think...because when I take out the c in 4*a*c, a value does pop up instead of the -1.#IND thing. Whatever value I test, I get -1.#IND.

Help!!
Jan 29 '07 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 1,510
so long as you are dealing with real roots you only had one problem and that was you set the result of x1 to 1, e.g.
Expand|Select|Wrap|Line Numbers
  1.  double x1 = 1;
  2.   double x2 = ((-1*b) - sqrt(pow(b,2.0) - 4*a*c))/(2*a);
  3.  
I have fixed that
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <cmath>
  4.  
  5. using namespace std; 
  6.  
  7. int main()
  8. {
  9.   double a = 0;
  10.   double b = 0;
  11.   double c = 0;
  12.  
  13.   cout << "The current value of a is "<<a << endl;
  14.   cout << "The current value of b is "<<b << endl;
  15.   cout << "The current value of c is "<<c << endl;
  16.  
  17.   cout << "Enter the coefficients a, b, and c for the quadratic equation in the form"
  18.        << endl;
  19.   cout << "a(x^2) + b(x) + c" << endl;
  20.   cout << "For example, if the equation is 2(x^2) + 3(x) + 4" << endl;
  21.   cout << "Enter as 2.0 3.0 4.0" << endl;
  22.  
  23.   cin >> a >> b >> c; 
  24.  
  25.   cout << "Your expression is "<<a<<"(x^2) + "<<b<<"x + "<<c<< endl;
  26.  
  27.   double factor = a * c;
  28.  
  29.   double x1 = ((-1*b) + sqrt(pow(b,2.0) - 4*a*c))/(2*a); //** fixed
  30.   double x2 = ((-1*b) - sqrt(pow(b,2.0) - 4*a*c))/(2*a);
  31.  
  32.   cout << x1 << endl;
  33.   cout << x2 << endl;
  34.  
  35.  
  36.   system("PAUSE");
  37.   return 0;
  38. }
  39.  
if you try these values your program works
Expand|Select|Wrap|Line Numbers
  1.         a = 2.0     b = -6.0     c = 4.0    real roots = 2.0 and 1.0
  2.         a = 61.0    b = 159.0    c = 87.0   real roots = -0.781449 and -1.825108
  3.  
  4.  
however the values you used yeild complex roots
Expand|Select|Wrap|Line Numbers
  1.         a = 1.0     b = 2.0     c = 3.0  complex roots -1.0 +-j1.414
  2.  
when you have complex roots the expression (pow(b,2.0) - 4*a*c) is negative so when you take the square root you get an undefined value which gives you your "-1.#IND" output. If you are suposed to solve for complex roots you need to test for this situation and evaluate complex values
Jan 29 '07 #2

P: 6
Thank you so much for your help!! I didn't realize that I maybe shouldn't have used 1, 2, and 3.
Jan 29 '07 #3

Banfa
Expert Mod 5K+
P: 8,916
It is easy to test if the result will be complex, compare

pow(b,2.0) AND 4*a*c

if pow(b,2.0) > 4*a*c
   2 real roots

if pow(b,2.0) == 4*a*c
   1 real root

if pow(b,2.0) < 4*a*c
   2 complex roots


Also before performing the calculation it would be good to test that a != 0 otherwise you will get a divide by 0 error.


In most programming the code to handle the success case (when the program operates successfully as specified) is normally <60% of the code, sometimes <40%, these rest is all error handling conditions. However it is this error handling that most often gets leftout by inexperienced programmers.

This is also why programmes are twice as complex to write as they initially appear, because a lot of people initially forget to think about the error handling cases.
Jan 30 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.