• by axblount on 4/26/2025, 2:33:18 PM

    This is usually called NaN-boxing and is often used to implement dynamic languages.

    https://piotrduperas.com/posts/nan-boxing

  • by dzaima on 4/26/2025, 3:33:26 PM

    This doesn't work on Firefox, as it normalizes NaNs as they're extracted from ArrayBuffers. Presumably because SpiderMonkey uses NaN-boxing itself, and thus just doesn't have any way to represent actual non-canonical NaN floats.

  • by o11c on 4/26/2025, 5:35:06 PM

    Note that there are a lot of exciting new float-related functions in C23, many related to NaN handling, signed zeros, or precision in edge cases. Some have been in glibc for years; others not yet.

    The API for `getpayload`/`setpayload`/`setpayloadsig` looks a little funny but it's easy enough to wrap (just consider the edge cases; in particular remember that whether 0 is valid for quiet or signaling NaNs is platform-dependent).

    Finally we have a reliable `roundeven`, and the convenience of directly calling `nextup`/`nextdown` (but note that you'll still only visit one of the zeros).

    The new `fmaximum` family is confusing, but I think I have it clear:

      without 'imum' (legacy): prefer non-NaN
      with 'imum': zeros distinguished (-0 < +0)
      with '_num': prefer a non-NaN, also signal if applicable
      with '_mag': compare as if fabs first
    
    `totalorder` has been often-desired; the other new comparisons not so much.

    `rootn` and `compoundn` are surprisingly tricky to implement yourself. I still have one testcase I'm not sure why I have to hand-patch, and I'm not sure which implementation choice keeps the best precision (to say nothing of correct rounding mode).

  • by vitaut on 4/26/2025, 3:50:10 PM

  • by scorchingjello on 4/26/2025, 4:00:56 PM

    Since the first time I had Indian food sometime in 1987 I have always called naan “not a number bread” and no one has ever laughed. I feel like i may have found my people.

  • by clhodapp on 4/26/2025, 7:38:11 PM

    I guess I just don't get it because the before & after cases don't seem to be showing remotely comparable use cases.

    In the "before" use-cases, the programmer has accidentally written a bug and gets back a single NaN from a logical operation they performed as a result.

    In the "after" cases, the programmer already has some data and explicitly decides to convert it to a NaN encoding. But instead of actually getting back a NaN that is secretly carrying their data, they actually get a whole array of NaN's, which duck type differently, and thus are likely to disappear into another NaN or an undefined if they are propagated through an API.

    Like.. I get that the whole thing is supposed to be humorously absurd but... It just doesn't land unless there's something technical I'm missing to connect up the pieces.

  • by amelius on 4/26/2025, 3:24:30 PM

    I'm curious why we have not-a-number, but not not-a-string, not-a-boolean, not-an-enum, and not-a-mycustomtype.

  • by dunham on 4/26/2025, 3:03:25 PM

    I learned about this when trying to decode data from Firefox IndexedDB. (I was extracting Tana data.) Their structured clone data format uses nan-boxing for serialization.

  • by yapyap on 4/27/2025, 10:23:04 AM

    ‘Stuff your nan’ is this intentional? From a UK perspective this sounds wild

  • by haxiomic on 4/26/2025, 3:41:55 PM

    That's curious! Does anyone know why the spec was designed to allow so many possible NaN bit patterns? Seems like just one would do!

  • by AaronAPU on 4/26/2025, 3:16:52 PM

    Reminds me of using the highest bit(s) of 64-bit ints to stuff auxiliary data into lockfree algorithms. So long as you’re aware of your OS environment you can enable some efficiencies you couldn’t otherwise.

  • by carterschonwald on 4/26/2025, 4:40:24 PM

    The nan bits were intended to help provide more informative float error diagnostics at the language implementation level for non crashing code. I wonder if I’ll ever get to exploring it myself.

  • by roywiggins on 4/26/2025, 7:45:44 PM

    Four NaNs? Four? That's insane.

    https://m.youtube.com/watch?v=feJlRDLX0iQ

  • by gitroom on 4/27/2025, 12:32:18 PM

    well i totally relate to calling naan not-a-number bread, it's been stuck in my head ever since i learned about NaNs tbh

  • by johnklos on 4/26/2025, 5:15:24 PM

    It's a shame this won't work on VAX.