473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
3 2488
horace1
1,510 Expert 1GB
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
floofy
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
9,065 Expert Mod 8TB
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

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

Similar topics

2
by: Erich Reisenhofer | last post by:
Hello! Which operatiaon causes -1.#IND ? I use complex numbers. Thank you! Erich
2
by: Ken Lindner | last post by:
I have a need to become familiar with SQL Server 2000 for work. Needless to say I am new to SQL Server any version, but not IT in general. My employer has provided me with the SQL Server 2000...
383
by: John Bailo | last post by:
The war of the OSes was won a long time ago. Unix has always been, and will continue to be, the Server OS in the form of Linux. Microsoft struggled mightily to win that battle -- creating a...
19
by: Alex Vinokur | last post by:
Is there any tool to count C-program lines except comments? Thanks, ===================================== Alex Vinokur mailto:alexvn@connect.to http://mathforum.org/library/view/10978.html...
9
by: cw bebop | last post by:
Hi all Using Visual Studio C# Have a string string st = "Hi, these pretzels are making me thirsty; drink this tea. Run like heck." ******
3
by: Chris Paul | last post by:
I'm having trouble with PHP & PostgreSQL/OpenLDAP/Apache on Windows. I've set this up countless times on BSD (piece of cake) but I'm trying to do this on Windows now so that my developer can work...
5
by: awasthi.ashish | last post by:
Hi friends, I am writing a simple program to convert an IP address from a hexadecimal form to dotted-notation form. However, for some reason I am having problem getting it to work.Here it is: ...
9
by: ehabaziz2001 | last post by:
I am facing that error message with no idea WHY the reason ? "Abnormal program termination" E:\programs\c_lang\iti01\tc201\ch06\ownarr01o01 Enter a number : 25 More numbers (y/n)? y...
87
by: pereges | last post by:
I have a C program which I created on Windows machine. I have compiled and executed the program on windows machine and it gives me the consistent output every time i run it. for eg. input a = 2,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.