• 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.

      • bss03 ( bss03@infosec.pub ) 
        link
        fedilink
        English
        arrow-up
        26
        ·
        1 day 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.

          • lime! ( lime@feddit.nu ) 
            link
            fedilink
            arrow-up
            13
            ·
            1 day 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.

      • Lupie ( Lupie@sopuli.xyz ) 
        link
        fedilink
        arrow-up
        11
        ·
        1 day ago

        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.