nan in c++ for beginner -
#include <iostream> #include <conio.h> #include <math.h> using namespace std; int main () { float xstart = -2, xfinal = 2, h = 0.5; float t, y; cout << "x start = -2" << endl; cout << "x final = 2" << endl; cout << "step = 0.5" << endl << endl; ( float x = xstart; x <= xfinal; x+= h) { t= sqrt(pow(sin(x),2)) / sqrt(x - 4); y= sqrt(2 * t + x); cout << x << " | " << t << " | " << y << endl; } cout << endl; } this outputs t , y variables nan. out of ideas on how fix it. code example of problem, no need else fix nans.
t= sqrt(pow(sin(x),2)) / sqrt(x - 4); in c++, square root of negative number defined nan, "not number". since variable x iterates in range [-2, 2], you're never going have not nan, means calculations using arrive @ nan.
if you're expecting sqrt return complex numbers, you'll need make use of std::complex , various math functions.
Comments
Post a Comment