5
10Dev
3y

Typescript is amazing, but this little snippet

NaN === NaN // false

Drives me fucking crazy

Comments
  • 9
    That has nothing to do with typescript and everything to do with the RFC floating point standard.
  • 5
    Not really typescript, that's just javascript implementing not-a-number per IEEE 754.

    Basically, it's designed to fail gracefully and propagate the fact that it is not a number in floating point arithmetic. 754 6.2 defines how operations works, but is paywalled. There's much discussion about it if you want to google.

    Basically the rule for NaN is propagate NaN as the result of any arithmetic operation involving NaN and make it falsy in case of boolean operations (basically the same behavior as constant functions in a derivative). This way it preserved it's NaN-ness without blowing up computation. It can make these assessments safely because the class of the values in the presented value were outside the set of values that are recognizable as numbers.

    It's not perfect, but at least it's a predictable standard.
  • 3
    @24th-Dragon
    Gotta pay for it somehow. The internet is a freeloader culture without limits. They're for open standards, just not historically paying for the time, hosting or expenses of the people that create and maintain them.

    So businesses can pony up large sums to get access and fund operations. Does this hurt the little guy? Maybe, maybe not. The drafts are still public.
  • 3
    @SortOfTested A funding model where the standards have to be paywalled still seems fundamentally broken. The point of a standard is that it's better than writing your own solution because everyone knows it. If your own solution is publicly accessible while the standard isn't (and it isn't an interop standard that users don't care about), this advantage is lost.
  • 0
    @homo-lorens
    It's an older model, if you feel strongly about it, replace them and put them out of business.
  • 0
    Even if it wasn’t because of the floating point standard, it would be because of JavaScript, not TypeScript. Because it needs to be compatible with JS.

    There are other popular strange behaving comparison operations in JS which should also be present in TS.
Add Comment