Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V6038. Comparison with 'double.NaN'...
menu mobile close menu
Analyzing projects
Additional information
toggle menu Contents

V6038. Comparison with 'double.NaN' is meaningless. Use 'double.isNaN()' method instead.

May 08 2018

The analyzer detected that a variable of type float or double is compared with a Float.NaN or Double.NaN value. As stated in the documentation (15.21.1), if two Double.NaN values are tested for equality by using the '==' operator, the result is false. So, no matter what value of 'double' type is compared with Double.NaN, the result is always false.

Consider the following example:

void Func(double d) {
  if (d == Double.NaN) {
    ....
  }
}

It's incorrect to test the value for NaN using operators '==' and '!= '. Instead, method Float.isNaN() or Double.isNaN() should be used. The fixed version of the code:

void Func(double d) {
  if (Double.isNaN(d)) {
    ....
  }
}

This diagnostic rule is classified as: