Even non-physical numbers are problematic to signal 'invalid'. I had a customer use -999 as a placeholder for 'invalid' data. Years later somebody made a higher level data product that averaged and combined that data with other products, without knowing to first remove those 'invalid' values. The resulting values were all now within physical limits, but very very wrong. The best solution is to use IEEE NaN https://en.wikipedia.org/wiki/NaN so that your code blows up if you don't explicitly check for it.
gizmo686|1 year ago
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:
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:
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.HdS84|1 year ago
addaon|1 year ago