3 ms·
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 amelius 2y ago
I'm curious why we have not-a-number, but not not-a-string, not-a-boolean, not-an-enum, and not-a-mycustomtype.
- ok_computer 2y agoBecause representing infinity is not possible outside of symbolic logic and isn’t encodable in floats. I think it is a simple numerical reason and not a deeper computer reason.
- Sharlin 2y agoWell, infinity is totally representable with IEEE 754 floats. For example 1.0/0.0 == +inf, -1.0/0.0 == -inf, but 0.0/0.0 == NaN.
- amelius 2y agoA smart compiler should be able to figure out a better value for 0/0, depending on context. For example: for i in range(0, 10): print(i/0.0) In this case it should probably print +inf when i == 0. But: for i in range(-10, 10): print(i/0.0) Now it is not clear, but at least we know it's an infinity so perhaps we need a special value +-inf. And: for i in range(-10, 10): print(i/i) In this case, the value for 0/0 can be 1.
- Sharlin 2y agoWell, it could, but that would be against the spec. The hardware implements IEEE 754, most languages guarantee IEEE 754, and transforming code so that 0.0/0.0 doesn't result in NaN would be invalid.
- ok_computer 2y agoI'm incorrect. there's a spec discussed here https://www.gnu.org/software/libc/manual/html_node/Infinity-and-NaN.html https://www.gnu.org/software/libc/manual/html_node/Infinity-...
- IncreasePosts 2y agoSome common numeric operations can result in non-numbers(eg division by zero - Nan or infinity). Are there any common string operations with similar behavior?
- masfuerte 2y agoOut of range substring? Some languages throw an error, others return an empty string. You could return a propagating NaS instead. I don't know what you'd use it for.
- cluckindan 2y agoCharset translation. Unicode’s � is basically a symbol for not-a-char.
- Sharlin 2y agoBecause IEEE 754 creators wanted to signal non-trapping error conditions for mathematically undefined operations, and they had a plenty of bit patterns to spare. Apparently back in the 70s and 80s in many cases it was preferable for a computation to go through and produce NaNs rather than trapping instantly when executing an undefined operation. I'm not quite sure what the reasoning was exactly.
- wbl 2y agoIn early FP machines the floating point processor could not take a trap at a faulting instruction precisely: it could only go bad things. Furthermore for programmers and hardware it can be very expensive. Rather than go through a loop and filter NaN out of results it becomes trap every time and resume and is a pain.
- IshKebab 2y agoIt avoids traps which are really inconvenient.
- dzaima 2y agoBecause NaNs come from a standardized hardware-supported type, whereas the rest of those are largely language-specific (and you could consider null/nil as a "not-a-*" type for those in applicable languages; and there are languages which disallow NaN floats too, which completes all combinations). Itanium had a bit for "not a thing" for integers (and perhaps some older hardware from around the time floats started being a thing had similar things), so the idea of hardware support for not-a-* isn't exclusive to floats, but evidently this hasn't caught on; generally it's messy because it needs a bit pattern to yoink, but many types already use all possible ones (whereas floats already needed to chop out some for infinities).
- Sharlin 2y agoYou can encode not-a-bool, not-a-(utf-8)-string and not-an-enum using one of the invalid bit patterns – that's exactly what the Rust compiler can do with its "niche optimization": https://www.0xatticus.com/posts/understanding_rust_niche/ https://www.0xatticus.com/posts/understanding_rust_niche/
- usefulcat 2y agoC++ has std::optional for exactly that purpose.
- mathgradthrow 2y agoNana basically means that floating point arithmetic is predicting that your mathematical expression is an "indeterminate form", as in the thing you learn in calculus.
- sgerenser 2y agoNot-a-boolean would be something like the much maligned tri-state bool pattern: https://thedailywtf.com/articles/What_Is_Truth_0x3f_ https://thedailywtf.com/articles/What_Is_Truth_0x3f_
- stevage 2y agoYou missed not-a-NaN.
- indigoabstract 2y agoAlso GNU, since it's not-a-unix.