Assign a non-Number to a Double Variable?

I am writing a math program, using variables of type double, and had initialized all variables to 0.0.

I now realize that not all results will be valid.

Is there a way to explicitly assign a variable of type double a non-numeric value, for example, "NaN", "Undefined", or "Unassigned" or something like that?

That way, when I read through the printout of results, I will realize the "NaN" results indicate a valid solution was not found. Whereas a 0.0 might not stand out.

I'd hate to have to go back and delete the initialization, and then re-assign 998 values just for the sake of 2 non-solutions.

Can this be done?
Thanks for the information. Sounds easy enough.

So, to assign this value to a variable, I assume it is as simple as the following example:

1
2
3
4
5
6
7
8
9
10
11
12
double a, b, c;

a = 3.14158;
b = 0.0;

if (b != 0){
  c = a/b;
}
else {
  cout << "Attempted divide by zero.\n";
  c = std::numeric_limits<double>::quiet_NaN();
}


Is this feature part of the C++ standard, or is it specific to Microsoft? I would like my code to be as close to standards as possible.
The above is standard C++. http://www.cplusplus.com/reference/limits/numeric_limits/. I've tested the quiet_NAN with g++
Last edited on
Topic archived. No new replies allowed.