C++ float number to nan

10,599

Solution 1

Taken from wikipedia -> special values -> nan

  • 0/0
  • ∞×0
  • sqrt(−1)
  • in general "invalid operations" (I am not sure wether there are not more than the three above)

Looking at you code: infinity times 0 is possible, is it?

edit:

  • 0 <= s <= +inf

  • 1 <= m <= +inf

s / m:

  • +inf / +inf does indeed make minus NaN (I tested it)

I think that's the only thing that makes a NaN.

Solution 2

If you can keep x between 0 and FLT_MAX (3.40E+38 in my case), your gp function will not return NaN.

enter image description here

Solution 3

You say in a comment that you only use *, +, -.

[Edit: you've since said that you also use pow and division, which introduce some extra ways to get NaN. For example if the parameter x is a large negative value then pow(e,-x) is infinity, so you can easily end up computing infinity/infinity, which is another NaN]

So, if you have IEEE floating-point then assuming this summary is correct, the only ways you can generate NaN are:

  1. Generate a positive or negative infinity by going out of range,
  2. Multiply it by zero.

or:

  1. Generate a positive and a negative infinity,
  2. Add them (or equivalently, subtract two infinities of the same sign).

So if you check for and catch infinities, you don't have to worry about NaNs as well. That said, the usual way is to let such values propagate as quiet NaNs, and check at the end.

For C++ implementations using non-IEEE arithmetic, I'm not sure what the rules are when a NaN is permitted. I could look them up in the standard, but then again so could you ;-)

Solution 4

sqrt(-1)

give you NaN, for example. http://www.gnu.org/s/libc/manual/html_node/Infinity-and-NaN.html

Share:
10,599
AliBZ
Author by

AliBZ

Updated on June 09, 2022

Comments

  • AliBZ
    AliBZ almost 2 years

    I want to know what makes a float number nan in c++. I am using a large dataset and it is really hard to trace. I want to know the ways of changing a float number to nan to reduce bug possibilities.

    I found the code that causes the nan problem. I found that s/m is nan in some cases. But I don't know how to solve it.

    float gp(float x){
    float e = 2.71828183;
    x *= -1;
    float s = pow(e,x);
    float m = (1 + pow(e,x)) * (1 + pow(e,x));  
    return s / m;}
    
    • Konrad Rudolph
      Konrad Rudolph about 13 years
      Do you have any unchecked casts in your code? I’m almost certain that you cannot get a NaN if you only use +, – and * on well-defined (= properly initialised, non-NaN) values.
    • AliBZ
      AliBZ about 13 years
      I posted some of my codes here. Do you think that it will cause a problem?
    • Ronny Brendel
      Ronny Brendel about 13 years
      infinity times zero (as stated in my answer) is possible
    • AliBZ
      AliBZ about 13 years
      I edited the question. Could you plz take a look at it?
  • AliBZ
    AliBZ about 13 years
    Doesn't the variable overflow if it gets too big !?
  • AliBZ
    AliBZ about 13 years
    I used float for my variables. Can I use bigger floats? like double float !?
  • Mark.Ablov
    Mark.Ablov about 13 years
    you can check when float became NaN by using function isnan() and post operation here.
  • Michael Borgwardt
    Michael Borgwardt about 13 years
    "too big" or "too small" should lead to infinities or zeroes, not NaN
  • Ronny Brendel
    Ronny Brendel about 13 years
    by the way that function could be more efficient, if you used s instead of calculating the same power 3 times.
  • sergico
    sergico about 13 years
    I think that if you divede by zero a finite number you also get a NaN (not only 0/0)