cakeml
cakeml copied to clipboard
Test for Subnormal Floating Point Numbers
(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);
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;