Mathjs: NaN != NaN ?

Created on 20 Jun 2016  路  18Comments  路  Source: josdejong/mathjs

Is it possible to check if a result is NaN?
Something like
a/b == NaN ? "not a number" : "number"
Unfortunately, the expression "NaN == NaN" evaluates to false.

Most helpful comment

This is because NaN == NaN is always false in Javascript.

@josdejong I suggest we add an isNaN function.

All 18 comments

This is because NaN == NaN is always false in Javascript.

@josdejong I suggest we add an isNaN function.

Thinking outside of the JavaScript box, what would be desired behavior? support NaN == NaN unlike JavaScript? Creating a function isNaN would indeed be easy (you can also add it yourself using math.import).

I'm not sure about changing the behavior of NaN == NaN but isNaN() can be implemented nonetheless.

I guess, there are certainly reasons to make NaN != NaN in JavaScript. Even if it's a little confusing.
So, a function like isNaN() would be a good choice in my opinion.

@josdejong I'm going to make a pull request if you're not already working on it.

Current idea:

  • complex is NaN if both real and imaginary part are NaN
  • Units are NaN if their value is NaN
  • Fraction are never NaN
  • For arrays and matrices it behaves like isNegative

Sounds good to me, although if a Fraction is 0/0 should that also be NaN?

@ericman314 Yes I missed this one. I had only considered dividing nonzero numbers by 0.

Update: Or not, because it can't happen because fraction.js throws an error in this case.

Makes sense, lets go for the isNaN. I've just merged the PR of @FSMaxB, thanks Max.

That's awesome!
Thanks a lot.

That's more like a javascript issue, isn't it?
There are quite reliable ways in javascript:
MDN suggests using Number.isNaN. I use that, with the suggested polyfill no.1 (IE and Android still lacks it).
However, note that the global isNaN should be avoided (unsafe because it converts some non-NaN values to NaN)

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN

@balagge I already implemented an 'isNaN' function for mathjs using Number.isNaN for regular numbers, see #674. It will be included in the next release of mathjs.

That's more like a javascript issue, isn't it?

This is my feeling too, that it's a JavaScript quirk. I can't come up with reasons why NaN should have a special treatment. I just would like to be able to use == to test for NaN, like 0/0 == NaN.

Does anyone have good arguments against making == work for comparisons of NaN? If not I will update equality functions to support NaN.

Another commen about this: As far as I know this strange comparison behavior is specified in IEEE 754 that defines how floating point numbers are supposed to work. This also means that processor instructions working on floating point show this behavior and it is also mandated by ISO C standards. (meaning this is not a quirk specific to JavaScript).

I think the reasoning behind it is that NaN is supposed to be propagated through the entire calculation to indicate that something went wrong instead of just producing a subtly incorrect result. And if you have an algorithm that does a comparison at some point, having two NaN's compare equal is almot certainly incorrect.

But it definitely is annoying when explicitly checking for NaN.

if you have an algorithm that does a comparison at some point, having two NaN's compare equal is almot certainly incorrect.

That's a very good point. Wanting NaN == NaN return true is like answering "yes" to the question _"Ball A is not red, and ball B is not red. Do ball A and B have the same color?"_

So maybe it is not such a good idea after all.

Naan is incomparable, especially with butter.

I would also vote for having NaN == NaN return false.

This is definately the less confusing option as it is what everyone else does.

Agree. Let's keep it as it is and close this issue: NaN == NaN returning false. Thanks for all the inputs

Was this page helpful?
0 / 5 - 0 ratings