Skip to content

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

Description

@patrickgwsmith

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions