When we have a float we never know if it’s valid or NaN (not-a-number).
This makes math easy, as if you multiply or do other operations on a NaN the result will be NaN.
But sometimes we want to know that it is valid. To do this we could add special types that would then assert when receiving to check it matches.
defw set_speed(speed: F32.Finite) do
@speed = speed
end
F32.Finite
F32.Infinite
F32.NaN
F32.Normalized
- And same for
F64
ChatGPT wrote this code to check whether a bitstring is NaN:
defmodule FloatChecker do
def is_nan(<<_sign::1, 0xFF::8, mantissa::23-little>>) when mantissa != 0 do
true
end
def is_nan(_), do: false
end
When we have a float we never know if it’s valid or NaN (not-a-number).
This makes math easy, as if you multiply or do other operations on a NaN the result will be NaN.
But sometimes we want to know that it is valid. To do this we could add special types that would then assert when receiving to check it matches.
F32.FiniteF32.InfiniteF32.NaNF32.NormalizedF64ChatGPT wrote this code to check whether a bitstring is NaN: