Forgetting to handling NaN and infinities is a common problem when working with floating point numbers. In order to make it easier to handle all cases, we should allow for these unboxed values:
@unboxed
type t =
| @as(nan) NaN
| @as(positiveInfinity) PositiveInfinity
| @as(negativeInfinity) NegativeInfinity
| Finite(float)
// example usage
let value = switch value {
| NaN => raise(...)
| PositiveInfinity => Core.Float.Constants.maxValue
| NegativeInfinity => Core.Float.Constants.minValue
| Finite(value) => value
}
Considering finite floats can already be used unboxed, this seems like a logical addition.
There could be complexity due to compiling to isNaN
and isFinite
instead of a simple number comparison, but the unboxed JSON type already uses Array.isArray
, so it should be doable.