Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
RoyalIcing opened this issue Oct 9, 2024 · 0 comments
Open

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

RoyalIcing opened this issue Oct 9, 2024 · 0 comments

Comments

@RoyalIcing
Copy link
Owner

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant