• b3nsn0w@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    6 days ago

    duh, it’s part of the IEEE 754 standard. every language that handles floating point numbers has NaN handling, and in every language NaN is a float. the only reason this is brought up as a javascript thing all the time is that js doesn’t have separate float and int types (aside from special structures like typed arrays and dataviews), it just defaults to float64 for simplicity and calls it number, which lines up with the “NaN is a number? nani???” joke.

    you don’t like it? consult the sorry excuse of thinking sand in the device you’re reading this on, because this is etched straight into it. every FPU capable of handling float32 and float64 numbers knows what NaN is and handles it the exact same way.

    this is one of the many things that sound unintuitive about programming but if you bother to understand the context you realise that if it wasn’t this way, it would be way worse. one of the most important skills in our field is to understand computers for what they are, rather than be puzzled that they aren’t what you thought they were, and it especially comes out in edge cases like this.

      • arty@feddit.org
        link
        fedilink
        English
        arrow-up
        0
        ·
        5 days ago
        $ python 
        Python 3.14.4 (main, Aug 20 2026, 10:41:58) [GCC 15.2.0] on linux
        Type "help", "copyright", "credits" or "license" for more information.
        >>> type(float('nan'))
        <class 'float'>
        
        • alsimoneau@lemmy.ca
          link
          fedilink
          arrow-up
          0
          ·
          4 days ago

          That never arises in normal use though. I didn’t even know it existed since anything that would yield one raises an exception instead.

          • arty@feddit.org
            link
            fedilink
            English
            arrow-up
            0
            ·
            4 days ago

            But the OP complains specifically about typeof NaN, and not about Math.sqrt(-1) // NaN

            I know this doesn’t count as “normal use”, but in Python you get it e.g. via math.inf * 0.

    • aksdb@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      6 days ago

      To add to this: the state of being not a number is only relevant to a number. Everything that’s already not a number doesn’t need a specific value to represent this state. It’s actually quite logical.

      Apparently one of the few examples of JS not being weird.

  • palordrolap@fedia.io
    link
    fedilink
    arrow-up
    0
    ·
    6 days ago

    Infinity is also a number in JavaScript / IEEE number formats, but you’ll get a lot of grumbling from mathematicians with that sort of assertion.

  • ThunderComplex@lemmy.today
    link
    fedilink
    arrow-up
    0
    ·
    6 days ago

    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.

    • LillyPip@lemmy.ca
      link
      fedilink
      English
      arrow-up
      0
      ·
      6 days ago

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

    • threeonefour@piefed.ca
      link
      fedilink
      English
      arrow-up
      0
      ·
      6 days ago

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

      • bss03@infosec.pub
        link
        fedilink
        English
        arrow-up
        0
        ·
        6 days 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.

      • Lupie@sopuli.xyz
        link
        fedilink
        arrow-up
        0
        ·
        6 days 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.

          • bss03@infosec.pub
            link
            fedilink
            English
            arrow-up
            0
            ·
            6 days 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.

          • anewfox@lemmy.zip
            link
            fedilink
            arrow-up
            0
            ·
            6 days ago

            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.

            • 🌞 Alexander Daychilde 🌞@lemmy.world
              link
              fedilink
              English
              arrow-up
              0
              ·
              6 days 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.

      • ThunderComplex@lemmy.today
        link
        fedilink
        arrow-up
        0
        ·
        6 days ago

        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.

          • lime!@feddit.nu
            link
            fedilink
            arrow-up
            0
            ·
            6 days 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.