you are viewing a single comment's thread
view the rest of the comments
[–] 64 points 1 day ago (22 children)

I think this, and the fact that NaN == NaN is false, make perfect sense.
NaN is the result of mathematically undefined behavior. You’re not going to get a NaN outside of a number context, so tying it together in the Number scope is fine.

  • source
  • hideshow 22 child comments
  • [–] 5 points 22 hours ago (5 children)

    Same reason NULL != NULL. It makes sense if you understand how these values work.

  • source
  • parent
  • hideshow 5 child comments
  • [–] 14 points 22 hours ago (4 children)
  • [–] 9 points 22 hours ago* (2 children)
  • [–] 14 points 21 hours ago (1 child)

    Ah, my brain was in C mode, where NULL is functionally 0. This is about the concept, which doesn't apply equally to all languages.

  • source
  • parent
  • hideshow 1 child comment
  • [–] 5 points 21 hours ago* (last edited 20 hours ago)

    Yep. There are some languages where this applies, because it’s a mathematical concept. The point is it’s the same ‘no value’ concept, which isn’t shared by implemented the same way by all languages.

    C is strongly typed, and uses pointers, so NULL sort of intrinsically has a value in C.

    edit: my wording was a bit off

  • source
  • parent
  • [–] -3 points 1 day ago (14 children)

    The result of mathematically undefined behaviour should be an error, but sure, go ahead and return Keleven. 1/0 = keleven fuck it.

  • source
  • parent
  • hideshow 14 child comments
  • [–] 31 points 23 hours ago

    NaN is one of the ways IEEE maths signals an erroneous calculation when the only outbound communication channel is the returned IEEE value. Reading the history and rationale of the floating-point standard will likely convince you this was and probably is a good compromise.

    Because IEEE values often have hardware acceleration that is a significant factor in performance, languages rarely (if ever) choose to map all IEEE error values to language-specific error reporting (e.g. a thrown exception or error-tracking algebraic data type).

    JS is hardly an outlier here.

    I do think it probably violates the principle of least surprise, but I don't think I understand the scale of "momentum" change that would need to take place across society (industry, academia, etc.) to align it with naive expectations.

  • source
  • parent
  • [–] 17 points 1 day ago (4 children)

    NaN is a error and it’s your responsibility to handle it gracefully. No need to halt the entire program when you fuck up some maths.

  • source
  • parent
  • hideshow 4 child comments
  • [–] 12 points 1 day ago (3 children)

    NaN is a error

    Error.isError(NaN)
    // False
    
  • source
  • parent
  • hideshow 3 child comments
  • [–] 14 points 23 hours ago*

    nan is a ieee754-defined error. what javascript calls errors these days (it tends to change a lot) has little to nothing to do with that. it's on every developer to familiarise themself with ieee754, it's one of the most important documents in the profession.

    while nan is an invalid floating-point number, it is a valid value for a float-typed field. making it into an error is like adding a bit labelled "crash" to every number in your program.

  • source
  • parent
  • [–] 7 points 1 day ago* (7 children)

    No, it should not be an error. If you're making a calculator and your user enters 1/0, your program should not have the propensity to crash; the result should simply be NaN and you should choose how you display that result to your user.

  • source
  • parent
  • hideshow 7 child comments
  • [–] 6 points 23 hours ago (4 children)

    You talk like error handling isn't a thing.

  • source
  • parent
  • hideshow 4 child comments
  • [–] 2 points 22 hours ago* (3 children)

    fun fact: getting NaN from a numeric calculation is the error that you handle. if this wasn't obvious, please don't write code for anyone but yourself. hope this helps.

  • source
  • parent
  • hideshow 3 child comments
  • [–] 7 points 19 hours ago

    if this wasn’t obvious,

    If this wasn't obvious, be glad of the chance to learn.

    If we all stopped as soon as something simple wasn't obvious, nobody would learn and nobody would do anything.

    What a terrible reply to someone. It cuts off curiosity and says "You are too stupid to do anything."

    Yet I bet there was a time that even with that very specific thing, you didn't think of it that way.

  • source
  • parent
  • [–] 4 points 19 hours ago (1 child)
  • [–] 1 point 16 hours ago

    fault v. error v. failure

    In common language they can be synonyms, but often aren't in places (like computing) where there's a useful difference. A page fault isn't an error; a handled error doesn't result in a failure; fault-tolerant systems fail less often than their components fault.

  • source
  • parent
  • load more comments (-1 replies)