How to Test to See If a Double Is Equal to Nan

How do you test to see if a double is equal to NaN?

Use the static Double.isNaN(double) method, or your Double's .isNaN() method.

// 1. static method
if (Double.isNaN(doubleValue)) {
...
}
// 2. object's method
if (doubleObject.isNaN()) {
...
}

Simply doing:

if (var == Double.NaN) {
...
}

is not sufficient due to how the IEEE standard for NaN and floating point numbers is defined.

Checking for NaN and using it in an If

There are static methods Float.isNaN(float) and Double.isNaN(double) that you can use.

double x = ... // whatever calculation you do

if (Double.isNaN(x)) {
...
}

Checking if a double (or float) is NaN in C++

According to the IEEE standard, NaN values have the odd property that comparisons involving them are always false. That is, for a float f, f != f will be true only if f is NaN.

Note that, as some comments below have pointed out, not all compilers respect this when optimizing code.

For any compiler which claims to use IEEE floating point, this trick should work. But I can't guarantee that it will work in practice. Check with your compiler, if in doubt.

Shortest way of checking if Double is NaN

As MSDN says, NaN means that result is undefined. With infinities result is defined:

A method or operator returns NaN when the result of an operation is
undefined. For example, the result of dividing zero by zero is NaN, as
the following example shows. (But note that dividing a non-zero number
by zero returns either PositiveInfinity or NegativeInfinity, depending
on the sign of the divisor.)

So, it's not good idea to tread infinities as NaN. You can write extension method to check if value is not NaN or infinity:

// Or IsNanOrInfinity
public static bool HasValue(this double value)
{
return !Double.IsNaN(value) && !Double.IsInfinity(value);
}

How can I check for NaN values?

Use math.isnan:

>>> import math
>>> x = float('nan')
>>> math.isnan(x)
True

Check NAN values java

Depending on the type of your array, use Double.isNaN() or Float.isNaN().

How to declare NaN for a if statement?

You're asking the same question as this older QA posting: How do you test to see if a double is equal to NaN? (I've marked this as a duplicate of that).

In short: By design, NaN cannot be meaningfully compared to any other value, (so foo == NaN won't work for your purposes). This is because of NaN-propagation.

Instead, to test for NaN, use Double.isNaN( value ) or Float.isNaN( value ).

unittest - how to assert if the two possibly NaN values are equal

You could use:

numpy.testing.assert_equal(v1, v2)

From docs:

This function handles NaN comparisons as if NaN was a “normal” number.
That is, no assertion is raised if both objects have NaNs in the same
positions. This is in contrast to the IEEE standard on NaNs, which
says that NaN compared to anything must return False.

It throws AssertionError when the values are not equal and it should work fine with pytest, but it may not be a good fit for unittest tests.

Another option is:

numpy.isclose(v1, v2, equal_nan=True)

but obviously it's a replacement for math.isclose, not for ==.

How to check for inf (and | or) NaN in a double variable

  1. When using scanf() double should be read using %lf, not %f. %f will convert the input into a 32-bit float, so the first 32 bits of your variables will be filled with some invalid data, and the last 32 bits will be left as garbage.

  2. Yes. #include <limits>, then std::numeric_limits<double>::quiet_NaN(). Some compilers (e.g. gcc) also provides the NAN macro in <cmath>.

  3. There is no NaN or infinity for integer types. Divide-by-zero for integer will cause an exception (SIGFPE).

  4. #include <cmath>, then std::isinf(x). Use std::isfinite(x) to ensure x is not NaN or Infinity.

Comparing Double.NaN with itself

The reason for the difference is simple, if not obvious.

If you use the equality operator ==, then you're using the IEEE test for equality.

If you're using the Equals(object) method, then you have to maintain the contract of object.Equals(object). When you implement this method (and the corresponding GetHashCode method), you have to maintain that contract, which is different from the IEEE behaviour.

If the Equals contract was not upheld, then the behaviour of hash tables would break.

var map = new Dictionary<double,string>();
map[double.NaN] = "NaN";
var s = map[double.NaN];

If !double.NaN.Equals(double.NaN), you'd never get your value out of the dictionary!

If the previous sentence does not make sense, then understand that the mechanics of hashing (used in Dictionary<T,U>, HashSet<T>, etc) use both the object.Equals(object) and object.GetHashCode() methods extensively, and rely upon guarantees of their behaviour.



Related Topics



Leave a reply



Submit