[swift-evolution] [pitch] Comparison Reform
michel.fortin at michelf.ca
Wed Apr 26 14:14:32 CDT 2017
Just throwing a idea that came to me while reading this discussion (hopefully not duplicating something that was already suggested)...
We could introduce `FiniteFloat` and `FiniteDouble` types that would disallow NaN and infinity. Computations creating NaN or infinity would trap with these types, mimicking what happens with integers in Swift. Converting a `Double` to `FiniteDouble` would be achieved through a `finiteValue` property like this:
var a = 1.0
var b = a.finiteValue!
where the `finiteValue` property returns a `FiniteDouble?` that is `nil` for NaN or infinity values. The finite type variant can conform to Equatable with no issue. The regular one that allows NaNs and infinity would then have less pressure to conform to Equatable. Just request the finite value whenever you need it:
dict[a.finiteValue!] = "a"
I understand that this goes a bit beyond solving the problem with comparison which is caused by NaNs and not by infinity values. But it's quite easy to explain what "finite" means and it follows how integers works. It also has the interesting property of guarantying a finite value for APIs that needs that, which I believe is more frequent than APIs that require only a non-NaN value. Hence why I'm suggesting FiniteDouble and not NonNaNDouble.
More information about the swift-evolution