|
|
|
|
|
by gizmo686
690 days ago
|
|
NaN is a sentinel value, just as much as 2,147,483,647 is The only difference is that NaN is implemented in hardware. However, taking advantage of that requires using the hardware arithmetic that recognizes NaN, which restricts you to floating point numbers, and all the problems that introduces. If you have good language support and can afford the overhead, you want to replicate that behavior in the type system as some sort of tagged union: data SentinelInt32 = NAN | Int32
Or, more likely, using the equivalent of Optional<T> that is part of your languages standard library.Of course, this means boxing all of your numbers. You could also do something like: type SentinelInt32 = Int32
Then provide alternative arithmetic implementations that check for your Sentinel value(s) and propagate the appropriately. This avoids the memory overhead, but still adds in all the conditional overhead. |
|