56 comments

log in to comment.

  • @ThunderComplex@lemmy.today · 4d 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 · 4d ago

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

      • @SpaceNoodle@lemmy.world · 4d ago

        That doesn’t smell right.

        • @LillyPip@lemmy.ca · 4d ago · edited

          richiban.uk/2016/12/21/does-null-equal-null/

          ISNULL may be true, whilst NULL != NULL. NULL is not a value.

          • @SpaceNoodle@lemmy.world · 4d ago

            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.

            • @LillyPip@lemmy.ca · 4d ago · edited

              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

        • @djmikeale@feddit.dk low rep · 4d ago · edited

          You can kinda think of it as null meaning undefined. In this case, “undefined does not equal undefined” seems easier for me to wrap my head around

    • @asdfasdfasdf@lemmy.world · 4d ago

      [deleted]

    • @VAK@lemmy.world · 4d ago

      It feels silly that a high level language has NaN at all

    • @threeonefour@piefed.ca · 4d 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 · 4d 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.

      • @ThunderComplex@lemmy.today · 4d 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.

        • @threeonefour@piefed.ca · 4d ago

          NaN is a error

          Error.isError(NaN)
          // False
          
          • @lime@feddit.nu · 4d ago · edited

            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.

          • @anewfox@lemmy.zip · 4d ago

            please do everyone a favor and stop writing code.

            • @daychilde@lemmy.world · 4d ago

              Please do everyone an actual favor and stop doing this. You are harmful and toxic.

      • @Lupie@sopuli.xyz · 4d ago · edited

        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.

        • @treadful@lemmy.zip · 4d ago

          You talk like error handling isn’t a thing.

          • @anewfox@lemmy.zip · 4d ago · edited

            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.

            • @daychilde@lemmy.world · 4d 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.

            • @lemonsorrel@lemmy.world · 4d ago

              newcomers to shitting on javascript sometimes learn to blame the tool

            • @treadful@lemmy.zip · 4d ago

              NaN is a primitive value, not an Error.

              >> typeof 1 / "a"
              "number" 
              
        • @daychilde@lemmy.world · 4d ago

          error ≠ crash

          • @bss03@infosec.pub · 4d 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.

  • @bss03@infosec.pub · 4d ago

    www.destroyallsoftware.com/talks/wat is much better. This post is pretty low-effort.

    • @lena@gregtech.euOP · 4d ago

      I love that talk.

      And yes, this post is low-effort, but so are most posts on here. Just a bunch of screenshots from various microblogging platforms reposted from Reddit.

    • @Aedis@lemmy.world · 4d ago

      Came in here to make sure this is posted.

  • @b3nsn0w@lemmy.world · 4d 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.

    • @aksdb@lemmy.world · 4d ago · edited

      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.

      • @Heavybell@lemmy.world · 3d ago

        It is easier to understand the non-weirdness of it in a strongly typed language, IMO. But yes.

    • @alsimoneau@lemmy.ca · 4d ago

      Counter point: Python.

      • @arty@feddit.org · 4d 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'>
        
    • Excellent, now do nulls!

  • @Jankatarch@lemmy.world · 4d ago · edited

    > “Not a Number”
    > Look inside.
    > number

  • @Quetzalcutlass@lemmy.world · 4d ago · edited

    It’s of type Numbn’t.

  • @palordrolap@fedia.io · 4d 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.

    • @anton@lemmy.blahaj.zone · 4d ago

      Once you accept that each float represent a range of reals most design decisions make sense.
      Infinity is just the name given to values that are too large to represent otherwise and negative zero includes negative values too close to zero.

      What mathematics probably hate most about floats is that rounding breaks commutativity.

      • @bss03@infosec.pub · 3d ago

        What mathematics probably hate most about floats is that rounding breaks commutativity.

        Actually, the maths people in my circles tend to complain more about how the normal floating-point equality test doesn’t define an equivalence relation, but really any of the violations of field properties can attract complaints.

        • @anton@lemmy.blahaj.zone · 3d ago · edited

          When writing my comment I had successfully repressed that fact in my mind. That’s obviously worse.

    • @PattyMcB@lemmy.world · 4d ago

      NaN indeed!

  • @bigmclargehuge@lemmy.zip · 4d ago

    The way I think about it: number is a js type. A floating point double, in other languages.

    NaN is a state that a number type can be in, representing a situation where the outcome of the numeric operation cannot be determined.

    Anywho, also yea ….JavaScript is goofy

  • @Colloidal@programming.dev · 4d ago

    It’s clearly bread.

  • Technically everything is a number when you get low enough

  • @TrickDacy@lemmy.world · 4d ago

    JavaScript really isn’t as bad as people want it to be

  • this makes sense

    NaN is a “this was meant to be a number but it somehow isnt”

  • Stop calling it “NaN bread” – NaN is fucking bread!

  • @General_Effort@lemmy.world · 4d ago

    It is the number that must not be; that cannot be. These 3 letters are all that stand between you and the ultimate cosmic horror.

    It is not for nothing that the sender of eight is also known as the soul-render. Which is why a sensible wizard will never mention the number if they can avoid it. “Or you’ll be eight alive.”

  • @SrMono@feddit.org · 4d ago
  • @baramintal@lemmy.org · 4d ago

    The beauty of JavaScript.

    You probably haven’t encountered truthsy and falsy values yet?

  • @Everyday0764@lemmy.zip · 3d ago

    there is a game / challenge for this

  • @GreenKnight23@lemmy.world · 3d ago

    yup. that’s JavaScript.

  • @Kolanaki@pawb.social · 3d ago

    I’ve never had number naan before… Is it any good? 🤔