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

Test for Subnormal Floating Point Numbers #920

Open
michael-roe opened this issue Dec 23, 2022 · 1 comment
Open

Test for Subnormal Floating Point Numbers #920

michael-roe opened this issue Dec 23, 2022 · 1 comment

Comments

@michael-roe
Copy link

michael-roe commented Dec 23, 2022

(Feature request)

It would be nice if the language had an isSubnormal function to test if the number is subnormal.

One of the reasons I want this is that I would like to be able to print out the value of a floating point number without doing a foreign function interface call into the C library's printf. A CakeML implementation of a function to print out a floating point number would case split on the different classes of floating point ... which means it would be nice if there was an easy way to tell if its subnormal.

So, ok, I can Double.toWord the floating point number, pull out the exponent field, and test if it's zero. But it would be nicer if I had access to the underlying architecture's instruction for doing this test.

fun nonNegativeFloatToString(x) =
  if isZero(x) then "0"
  else if isInfinite(x) then "infinity"
  else if isNaN(x) then "nan"
  (* ought to handle subnormal numbers here *)
  else "0x1." ^ digits(mantissa(x)) ^ "p" ^ Int.toString(exponent(x));

fun floatToString(x) =
  if isSignMinus(x) then "~" ^ nonNegativeFloatToString(Double.~ x) 
  else nonNegativeFloatToString(x);

@michael-roe
Copy link
Author

Testing for the other classes of floating point is easy:

fun isZero (x) = Double.= x zero;

fun isInfinite(x) = Double.= x negInf orelse Double.= x posInf;

fun isNaN(x) = not (Double.= x x);

fun isSignMinus(x) = Word64.toIntSigned(Double.toWord(x)) < 0;

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