You can check for NaN
only via Double.isNan()
:
if (Double.isNaN(sim)) {
// is Nan
} else {
// not nan
}
On first thoughts that is not intiutive, you have to know it. The reason behind it, is that this is specified by IEEE Floating Point standard.
You can argument:
NaN
is not a number, it can be anything but not a number, so two NaN
cannot be compared to be equal.
For example = 0.0 / 0.0 is NaN; and sqrt(-1) is NaN, too. But that values cannot be compared to be the same.
Additional reading: http://en.wikipedia.org/wiki/NaN