TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Stuffed-Na(a)N: stuff your NaNs

156 pointsby dgroshev20 days ago

15 comments

axblount20 days ago
This is usually called NaN-boxing and is often used to implement dynamic languages.<p><a href="https:&#x2F;&#x2F;piotrduperas.com&#x2F;posts&#x2F;nan-boxing" rel="nofollow">https:&#x2F;&#x2F;piotrduperas.com&#x2F;posts&#x2F;nan-boxing</a>
评论 #43804923 未加载
评论 #43810588 未加载
评论 #43808865 未加载
评论 #43810253 未加载
评论 #43804453 未加载
dzaima20 days ago
This doesn&#x27;t work on Firefox, as it normalizes NaNs as they&#x27;re extracted from ArrayBuffers. Presumably because SpiderMonkey uses NaN-boxing itself, and thus just doesn&#x27;t have any way to represent actual non-canonical NaN floats.
评论 #43807659 未加载
评论 #43804800 未加载
o11c20 days ago
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.<p>The API for `getpayload`&#x2F;`setpayload`&#x2F;`setpayloadsig` looks a little funny but it&#x27;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).<p>Finally we have a reliable `roundeven`, and the convenience of directly calling `nextup`&#x2F;`nextdown` (but note that you&#x27;ll still only visit one of the zeros).<p>The new `fmaximum` family is confusing, but I think I have it clear:<p><pre><code> without &#x27;imum&#x27; (legacy): prefer non-NaN with &#x27;imum&#x27;: zeros distinguished (-0 &lt; +0) with &#x27;_num&#x27;: prefer a non-NaN, also signal if applicable with &#x27;_mag&#x27;: compare as if fabs first </code></pre> `totalorder` has been often-desired; the other new comparisons not so much.<p>`rootn` and `compoundn` are surprisingly tricky to implement yourself. I still have one testcase I&#x27;m not sure why I have to hand-patch, and I&#x27;m not sure which implementation choice keeps the best precision (to say nothing of correct rounding mode).
评论 #43805869 未加载
vitaut20 days ago
I made a garlic nan: <a href="https:&#x2F;&#x2F;www.godbolt.org&#x2F;z&#x2F;enjv1c7Tf" rel="nofollow">https:&#x2F;&#x2F;www.godbolt.org&#x2F;z&#x2F;enjv1c7Tf</a>
评论 #43804640 未加载
scorchingjello20 days ago
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.
评论 #43805527 未加载
clhodapp20 days ago
I guess I just don&#x27;t get it because the before &amp; after cases don&#x27;t seem to be showing remotely comparable use cases.<p>In the &quot;before&quot; use-cases, the programmer has accidentally written a bug and gets back a single NaN from a logical operation they performed as a result.<p>In the &quot;after&quot; 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&#x27;s, which duck type differently, and thus are likely to disappear into another NaN or an undefined if they are propagated through an API.<p>Like.. I get that the whole thing is supposed to be humorously absurd but... It just doesn&#x27;t land unless there&#x27;s something technical I&#x27;m missing to connect up the pieces.
评论 #43806923 未加载
amelius20 days ago
I&#x27;m curious why we have not-a-number, but not not-a-string, not-a-boolean, not-an-enum, and not-a-mycustomtype.
评论 #43804481 未加载
评论 #43804467 未加载
评论 #43804532 未加载
评论 #43804461 未加载
评论 #43804941 未加载
评论 #43807938 未加载
评论 #43804754 未加载
评论 #43804459 未加载
评论 #43808530 未加载
dunham20 days ago
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.
yapyap20 days ago
‘Stuff your nan’ is this intentional? From a UK perspective this sounds wild
评论 #43810878 未加载
haxiomic20 days ago
That&#x27;s curious! Does anyone know why the spec was designed to allow so many possible NaN bit patterns? Seems like just one would do!
评论 #43804603 未加载
评论 #43807005 未加载
AaronAPU20 days ago
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.
评论 #43804412 未加载
carterschonwald20 days ago
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.
roywiggins20 days ago
Four NaNs? Four? That&#x27;s insane.<p><a href="https:&#x2F;&#x2F;m.youtube.com&#x2F;watch?v=feJlRDLX0iQ" rel="nofollow">https:&#x2F;&#x2F;m.youtube.com&#x2F;watch?v=feJlRDLX0iQ</a>
gitroom19 days ago
well i totally relate to calling naan not-a-number bread, it&#x27;s been stuck in my head ever since i learned about NaNs tbh
johnklos20 days ago
It&#x27;s a shame this won&#x27;t work on VAX.