Orb icon indicating copy to clipboard operation
Orb copied to clipboard

Add F32.NaN, F32.Finite, and F32.Infinite subtypes

Open RoyalIcing opened this issue 1 year ago • 0 comments

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

RoyalIcing avatar Oct 09 '24 12:10 RoyalIcing