You cin a value to the variable f. f could be something like:
pow(5.957,-X*X*X*X)-cos(X*X) +20*sin(X*X)
or some other complicated math function. I also tried to declare it directly as such.
f = pow(5.957,-X*X*X*X)-cos(X*X) +20*sin(X*X);
std::cout << "Bisection method on function, f = " << f << " "<<std::endl;
The output is on both MinGW and VS2010:
f = nan
How do I get cout to print f on the screen?
I declared f as such
double F(double X) { double f; f = pow(5.957,-X*X*X*X)-cos(X*X) +20*sin(X*X); return f; }
I’m basically writing a C++ program to perform different numerical methods for finding roots on the function: bisection, Monte Carlo, Newtwon’s method etc. Those work, but I want to display the function to the user after he types it in, and it just shows up as NaN.
From the looks of it, you are trying to define
fas an actual mathematics function (in the direct way of doing it). That definitely won’t work.But if you have a string, then
fwill output a string, ignoring the mathematics function.Remember, unlike other programming languages, C++ is very…temperamental.
You can’t assign a character to an
intand vice versa. Butstringcan hold anything because it is only a piece of text.Try using
getline…just wait a sec and I’ll go get my book..Edit: Okay. If you use
getline (cin, foo);, wherefoois defined asstring fooand you include<string>, this should work. Is that what your coding originally looked like?Edit2: You should still be able to use
streamto convert from a string back and forth…but I’d need somebody more experienced than me to confirm that.