When I execute the problem, the results often end up as "-nan' or "nan" I am new to programming so this is a bit confusing. Could use some help
`
#include <iostream>
#include <cmath>
using namespace std;
//Quadratic Equation
int main() {
//Variables
double a,b,c,n,z,x1,x2,r;
//Input
cout<<"Quadartic Equation Calculator"<<endl<<endl;
cout<<"What is you 'a' value?: "<<endl;
cin>>a;
cout<<"What is your 'b' value?: "<<endl;
cin>>b;
cout<<"What is your 'c' value?: "<<endl;
cin>>c;
//calculation
n = -b;
z = sqrt((b*b)-(4*a*c));
r= 2*a;
x1 = (n (z))/r;
x2 = (n-(z))/r;
//output
cout<<"Your Solutions are: "<< x1 << " or "<< x2;
return 0;
}
`
CodePudding user response:
std::sqrt()
in the <cmath>
header expects its argument to be a positive number, in case it isn't, it will return nan
which is what you are facing. It also raises the FE_INVALID
floating point exception which you can verify after calling the std::sqrt()
function using std::fetestexcept(FE_INVALID)
.
In actual math, square roots of negative numbers result in complex numbers. Ordinary floating-point types like double
which usually follow the IEEE-754 standard are not capable of representing complex numbers. For that, you need to use something like std::complex<double>
which is actually capable of storing complex numbers inside it.
Below is your code modified to utilize std::complex<double>
and will properly print the real and imaginary part separately for square roots of negative real numbers instead of simply returning nan
as it did before.
#include <iostream>
#include <complex>
using namespace std;
double abs_zero(double const x) {
return x == 0 ? 0 : x;
}
int main() {
// Variables
complex<double> a, b, c, n, z, x1, x2, r;
// Input
cout << "Quadartic Equation Calculator" << endl << endl;
cout << "What is your 'a' value?: " << endl;
cin >> a;
cout << "What is your 'b' value?: " << endl;
cin >> b;
cout << "What is your 'c' value?: " << endl;
cin >> c;
// Calculation
n = -b;
z = sqrt((b * b) - (complex<double>(4, 0) * a * c));
r = complex<double>(2, 0) * a;
x1 = (n z) / r;
x2 = (n - z) / r;
x1 = complex<double>(abs_zero(real(x1)), abs_zero(imag(x1)));
x2 = complex<double>(abs_zero(real(x2)), abs_zero(imag(x2)));
// Output
if (imag(x1) == 0)
cout << "Your Solutions are: " << real(x1) << " or "<< real(x2) << endl;
else
cout << "Your Solutions are: " << x1 << " or " << x2 << endl;
}
In case you'd like to try it out yourself, here you are:
CodePudding user response:
"Often" end up as nan or -nan? That's expected behaviour. The quadratic formula gives you the roots of the equation, where the graph intersects the x-axis. Not all quadratic equations intersect the x-axis.
When you input a quadratic that doesn't intersect the x-axis into the quadratic formula, you end up with a negative number in the square-root of the equation, which leads to an imaginary number, which cannot be represented by the float
/double
data type, and thus you get "nan" or "-nan".
CodePudding user response:
NAN is in short for "Not a Number" is an exception which usually occurs in the cases when a number can’t be represented. Its probably that sqrt((bxb)-(4ac)) is causing the error as for some inputs the square root of that input would be undefined. For example in case of a = 5, b = 4 and c = 3, sqrt((bxb)-(4ac)) is undefined as it is sqrt. of negative number.