2 ms·
> An option/maybe type would solve this much better. NaN's are already an option type, although implemented in hardware. The checking comes for free. > An exc
by WalterBright 4y ago
> An option/maybe type would solve this much better.
NaN's are already an option type, although implemented in hardware. The checking comes for free.
> An exception would be much better
You can configure the FPU to cause an Invalid Operation Exception, but I personally don't find that attractive.
- pwpwp 4y agoGood points!
- ratorx 4y agoThe missing bit is language tooling. The regular floating point API exposed by most languages don’t force handling of NaNs. The benefit of the option type is not necessarily just the extra value, but also the fact that the API that forces you to handle the None value. It’s the difference between null and Option. Even if the API was better, I think there’s value in expressing it as Option<FloatGuaranteedToNotBeNaN> which compiles down to using NaNs for the extra value to keep it similar to other Option specialisations and not have to remember about this special primitive type that has option built in.
- avgcorrection 4y agoYeah. You should be very explicit about it. Certainly not treat it like, “ooh, here are some free bits that I can use to tag things in ad hoc ways (like -1 for missing index)”. https://internals.rust-lang.org/t/pre-rfc-nonnan-type/8418 https://internals.rust-lang.org/t/pre-rfc-nonnan-type/8418
- jcparkyn 4y ago> NaN's are already an option type, although implemented in hardware The compromise with this is that it makes it impossible to represent a non-optional float, which leads to the same issues as null pointers in c++/java/etc. The impacts of NaN are almost certainly not as bad (in aggregate) as `null`, but it'd still be nice if more languages had ways to guarantee that certain numbers aren't NaN (e.g. with a richer set of number types).
- jcranmer 4y ago> The impacts of NaN are almost certainly not as bad (in aggregate) as `null`, but it'd still be nice if more languages had ways to guarantee that certain numbers aren't NaN (e.g. with a richer set of number types). The problem with that is that to guarantee arithmetic does not result in a NaN, you need to guarantee that 0 and infinity are not valid values, and those values can still arise from underflow/overflow of regular computation. Basically, there's no subset of floating-point numbers that forms a closed set under +, -, *, or / that doesn't include NaN. So you can define FiniteF32 (e.g.), but you can't really do anything with it without the result becoming a full-on float.
- omginternets 4y agoAs far as I'm aware, there's no equivalent to a stack trace with NaN, so finding the origin of a NaN can be extremely tedious.
- WalterBright 4y agoI've never found it to be particularly difficult. The extremely difficult problems to find are uninitialized data and threading bugs, mainly because they appear and disappear.