### NaN in Program

This is supposed to be capable of solving quadratic equations, but it sometimes returns NaN. Such as when you would enter 3x^2 + 4x + 7. It returns NaN. I am not understanding why it returns NaN to me as the answer for the equation. If someone could please explain why it returns this to me and if there is a solution, please try and state it simply for me.

 ``123456789101112131415161718192021`` `````` cout << "\n+----Quadratic Equation Solution----+\n" << "| Enter the Coefficient on x^2 |\n" << " "; double avalue, bvalue, cvalue, b2, ac4, b2ac4, negbplus, negbminus, sqrb2ac4, twoa, plusover2a, minusover2a; cin >> avalue; cout << "| Enter the Coefficient on x |\n" << " "; cin >> bvalue; cout << "| Enter the Third Term |\n" << " "; cin >> cvalue; ac4 = avalue * cvalue * 4; b2 = pow(bvalue, 2); b2ac4 = b2 - ac4; sqrb2ac4 = sqrt(b2ac4); negbplus = -bvalue + sqrb2ac4; negbminus = -bvalue - sqrb2ac4; twoa = 2 * avalue; plusover2a = negbplus / twoa; minusover2a = negbminus / twoa; cout << "The Values of X are " << setprecision(4) << plusover2a << " and " << setprecision(4) << minusover2a << endl;``````

Also, the reason my variables are difficult to understand is because it is part of a larger code in a calculator I am creating.
closed account (D80DSL3A)
The example you gave has complex roots. ie. b2a4c < 0.
This causes a problem when you try to take a square root.

Maybe introduce a `bool rootsAreComplex;` variable to watch for this?
 ``123456`` ``````bool rootsAreComplex = false; if( b2a4c < 0.0 ) { rootsAreComplex = true; b2a4c *= -1.0;// make it positive so you can take a square root. }``````

rootAreComplex will affect how you display the roots.
I do not yet know the command `bool`. Could you please explain its usem
`bool` is not a command it is a type for boolean
you can use `int` instead and instead of `false` and `1` instead of `true`
there is another way of resolving the problem:
you can use the <complex> library http://www.cplusplus.com/reference/complex/

or you can create your own complex class
I have attempted to use the complex class, but I am unsure if I am using it correctly. I was setting an `if` statement so if b2ac4 is greater than 0, then it will use real, and if it is less than 0, then it will use imag. Every time i try to compile it, it fails to compile at the if statement saying that `>` is not an operator.
 ``12345678910111213`` `````` b2ac4 = b2 - ac4; if(b2ac4 > 0) //refuses to compile past this point { sqrb2ac4real = sqrt(b2ac4.real()); negbplusrl = -bvalue + sqrb2ac4real; negbminusrl = -bvalue - sqrb2ac4real; twoa = 2 * avalue; plusover2a = negbplusrl / twoa; minusover2a = negbminusrl / twoa; cout << " The Values of X are " << setprecision(4) << plusover2a << " and " << setprecision(4) << minusover2a << endl; }``````
Last edited on
I was capable of finding a way of solving the quadratic problems without using the header `<complex>`. I would like to thank you anyways for your attempt of helping me with my problem. Also, I was not fully understanding of `bool` so i did not even attempt to place it into my code. This is what I am now using:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````if (algselection==4) { cout << "\n+----Quadratic Equation Solution----+\n" << "| Enter the Coefficient on x^2 |\n" << " "; double avalue, bvalue, cvalue, b2, ac4, b2ac4, b2ac4topos, bover2a, b2ac4over2a, negbplus, negbminus, sqrb2ac4, twoa, plusover2a, minusover2a; cin >> avalue; cout << "| Enter the Coefficient on x |\n" << " "; cin >> bvalue; cout << "| Enter the Third Term |\n" << " "; cin >> cvalue; ac4 = avalue * cvalue * 4; b2 = pow(bvalue, 2); b2ac4 = b2 - ac4; if(b2ac4 >= 0) { sqrb2ac4 = sqrt(b2ac4); negbplus = -bvalue + sqrb2ac4; negbminus = -bvalue - sqrb2ac4; twoa = 2 * avalue; plusover2a = negbplus / twoa; minusover2a = negbminus / twoa; cout << "| The Values of X are |\n" << " " << setprecision(4) << plusover2a << "\n " << setprecision(4) << minusover2a << "\n+-----------------------------------+" << endl; } if(b2ac4 < 0) { b2ac4topos = b2ac4 * -1; sqrb2ac4 = sqrt(b2ac4topos); twoa = 2 * avalue; bover2a = -bvalue / twoa; b2ac4over2a = sqrb2ac4 / twoa; cout << "| The Values of X are |\n " << setprecision(4) << bover2a << " +/- " << setprecision(4) << b2ac4over2a << "i\n" << "+-----------------------------------+" << endl; }``````
Topic archived. No new replies allowed.