10 ms·
NansAreNumbers – An esoteric data type built of NaNs
- RhysU 5y agoYou had me at NaN.
- tabtab 5y agoI watched "The Flying NaN" as a kid.
- longemen3000 5y agonice!. one thing about NaN is that, because the specification only mentions the exponent part, you have all the mantissa bits free for use, this is the idea behind signalling NaNs, that if you return a NaN for an operation, that value should have some helpful debug information
- hinoki 5y agoAlso, NaN boxing. Which is (was?) used by JavaScript Core (Safari’s JS engine). All values are stored as a double; true, false, null, undefined have different NaN values. NaN even has enough spare bits to store Object pointers. Short strings are also stored packed into NaN bits.
- cormacrelf 5y agoHopefully not as an actual double, right? Not that many floating point registers compared to integer on most archs.
- hinoki 5y agoI think you can use general purpose registers so long as you don’t actually need to do IEEE 754 math. I’m not clear on the details of how the byte code/JIT deals with it, my only experience is with hire values are stored in the heap.
- saagarjha 5y agoYes, they’re stored in a union and passed around as whatever is convenient.
- Tuna-Fish 5y agoThere are an equal amount of vector (also used for FP) regs as there are gprs on x86_64 and on Aarch64. Since those registers are rarely used in normal general purpose code, moving some values over there in principle saves register space. (x87 fp is obsolete and should never be used for anything.)
- KMag 5y agoI'm not sure the latency on moving data between SSE and GP registers on modern x86 processors. If I remember correctly, late 1990s x86 was notoriously slow at moving data between GP registers and the x87 fp stack. I could certainly believe that on some architectures, moving data between vector registers and GP registers is slower than an L1 cache access.
- saagarjha 5y agoIt’s still in use, plus Mozilla uses it too.
- an1sotropy 5y agoWhat has always annoyed me is that the spec never says, AFAIK, if the 23rd bit should be ON vs OFF for a signalling vs quiet NaN, which means that you can't portably set a value to a quiet NaN at compile-time. SGI/Irix machines in the 2000s, for example, had perfectly good floating point but had the 23rd being OFF for a quiet NaN, while other machines have it ON for quiet NaN. And this is independent of endien-ness: these SGIs were big endien just like other big-endian machines that made quiet NaN differently.
- junon 5y agoAll but the first one (MSB) which, while not specified, is sometimes used by systems as a flag for loud errors vs. quiet errors.
- ffhhj 5y agoIn JavaScript: (!NaN) // true (NaN == false) // false
- dTal 5y agoI think that says more about JavaScript than NaN. Also JavaScript: "10"+1 => "101" "10"-1 => 9
- mlindner 5y agoThe hell?
- varanauskas 5y agoString addition = concatenation String subtraction = ??? => Number subtraction
- david_allison 5y agotypeof(NaN) // 'number'
- dylan604 5y agoyes. it is nota number. the result of taking the square root of a negative result from dividing by zero.
- nawgz 5y agoIs this interesting? It's true for undefined, null, empty string, 0, maybe empty arrays sometimes, ...
- nayuki 5y agoYes, what you presented are WTFs. But the biggest WTF is that in every language, NaN == NaN yields false.
- dTal 5y ago
- excitednumber 5y agoYou go away. You go away right now and you think about what you’ve done.
- dylan604 5y agoIf they didn't do it, someone else might. Isn't that how everything gets justified?
- SiempreViernes 5y agoIt sort of works as an argument if they also supply a "an we do it in this more positive way than others would" bit to the statment. But that's usually left out in silent acknowledgement that the person speaking actually is acting unethically because they don't provide any positive effect other parties wouldn't.
- an_d_rew 5y agoObligatory xkcd: https://xkcd.com/242/ https://xkcd.com/242/
- Waterluvian 5y agoHow do you represent negative NaN in IEEE 754?
- jitl 5y agoIt’s much more interesting to use the mantissa bits to store data than using a NaN as a very big bit. From nanbox [0] README: > IEEE 754 encodes 32-bit floating points with 1 bit for the sign, 8 bits for the exponent, and 23 bits for the number part (mantissa). For the specific case of NaN (e.g. the result of dividing 0 by 0, or the square root of a negative number), the spec encodes this as 11111111 in the exponent. The sign and the mantissa can be anything, and the spec suggests this can be used for "diagnostic information". One/some of these bits is/are commonly used to indicate a quiet NaN (qNaN) which could be expected vs. a signaling NaN (sNaN) which could be unexpected and should trigger an halting exception. This behavior is however sometimes different on different hardware. > 23 bits isn't much space, but conveniently the max size of a Unicode codepoint is 0x10FFFF, making it a 22-bit charset. When you encode your strings in UTF-32, you're only going to be using 22 of those bits, so masking the top portion with the NaN signature makes all of your characters NaNs. [0]: https://github.com/philihp/nanbox https://github.com/philihp/nanbox
- PeterisP 5y agoThis is just begging for a malware command&control system for SCADA controllers based on embedding commands and responses in the raw float values of some sensor data streams..
- helloooooooo 5y agoReminds me of the work done by Tom7 for Sigbovik https://youtu.be/5TFDG-y-EHs https://youtu.be/5TFDG-y-EHs
- vincent-manis 5y agoA statement I once heard, attributed to Christopher Strachey: “Merely because you can push a pea up a mountain with your nose doesn't mean you should.” (I heard this from Joe Stoy, who worked with Strachey, so I suspect it's authentic.)
- jheriko 5y agowhat about the payload bits?
- cm-t 5y agoIn the readme, at the end, there is this code sample: # [[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, (...) nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]] After reading this snippet, you, too, might have for the day, or week(s) the soung stick in your head. NyaN. sorry, not sorry.