18
acnair
6y

Like WTF!

"the value of NaN is result of an operation that cannot produce a normal result. NaN is not equal to any value, including itself."
The funny or wierd part (maybe just for me):
"You can detect NaN with the isNaN(number) function".

Like whaaat? 😶

Comments
  • 7
    You're lucky that you're using a language that has only one kind of NaN! :)
  • 2
    Don't divide by zero.
  • 0
    @stacked i just started using JS it's that important or was that sarcasm? 🤓
  • 21
    I wrote a long comment explaining why NaN is a number, why it inherits from Number instead of returning something that doesn't, why the interpreter team made this decision, problems other approaches would introduce, examples of said problems in other languages, etc.

    Then devRant crashed and I lost it.
    I am not retyping all of that. 😡
  • 3
    Sudo @Root. Please once more? For all those who are in your help? 🙈🤓
  • 0
  • 1
    @acnair no sarcasm: in certain languages/frameworks NaNs can have different types (signaling, quiet), a sign and a payload
  • 0
    NaN is a number.
    But a number is only NaN if it's NaN. 😯
  • 1
    Look at how numbers in that case floating point numbers are represented in binary.
  • 2
    I'm not really sure what's so confusing? It just means if you do number stuff on non numbers you get told it's not a number.

    If you find yourself in that situation then you should rethink what you're doing

    @Root TL;DR IEEE756
  • 0
    @spongessuck so NaN is looks like any ordinary number? 😬
  • 0
    @inaba so, are you saying that in the place where we have to pass the argument do we have to pass the number or the function which returns a number?
  • 1
    NaN != NaN;
    But
    String(NaN)=="NaN"
  • 1
    @Root Here, have a hug for your miserable destiny 🤗
  • 0
    I just used -0.0F in my thesis. Till this time I thought for floats the binary -0 is reserved for NaN.
  • 1
    @acnair Not entirely sure what you mean? You get NaN places like Number("1a"), 1/"1a", 1-"1a", etc. Basically places where you're doing number stuff on non-numbers.

    Though one annoying thing is that parseInt has PHP-like behaviour, where it will only return NaN if the first character isn't a number. IE parseInt("1a") and parseInt("a1") produces different things, sadly. (parseFloat does the same)
  • 2
    It's js, you expect any kind of logical thinking under the hood?
  • 3
    @Deadmau5 it is perfectly logical as it is. `NaN` is a special data type. It has a unique sequence of 0 and 1, it implements an `eq` class, that always returns false, and so on. It does make sense. (Not a huge js fan here either)

    @WIPocket this also makes sense, the `eq` class of it allways returns false, but there is a `toString` property, which returns "NaN"

    I mean, come on guys. I love to bash JS, but not for this...

    For further reference on floating point arithmetric: https://en.m.wikipedia.org/wiki/...

    If you want to bash js, bash it for `[1,2,3,10].sort();` which produces `[1,10,2,3]` (which kind of makes sence, as it just performs an lexicographical sort, and doesn't care if you throw in numbers and strings or just numbers in there (it first casts all values to strings...), but still is not what I would expect from it...)
  • 3
    Systems aspect:
    Floating-point formats (in binary) consist of 3 parts: sign (1 bit), exponent (~2 bytes), and mantissa (everything else)
    Infinity is when the exponent is all 1s and the mantissa is 0. The sign bit indicates positive (0) or negative (1) infinity.
    NaN is when the exponent is is all 1s but the mantissa is nonzero. The value of the mantissa is the 'payload' being talked about. I'm not sure about signaling and non-signaling NaN, but it depends on the payload.
  • 3
    Not a 🔢
  • 1
  • 1
    @Wack too much to gulp for a noobie noob. I will save these information. It would come in handy I guess.
  • 1
    @anArchLinuxer damn. Thats cool. I never thought about it like that.
  • 2
    @acnair no offence ment. Sorry if it came over harsh (:

    I guess the best part of being a dev, is that you never stop learning. Good luck on your journey!
  • 1
    @Wack none taken.

    That's what I am planning to do! 😁
Add Comment