Proposal: Hexadecimal floating point constants
Nick Bowler
nbowler at elliptictech.com
Fri Feb 19 14:48:48 EST 2010
I'd like to propose what I believe is a simple but valuable extension to
Haskell that I haven't seen proposed elsewhere.
C has something it calls hexadecimal floating constants, and it would be
very nice if Haskell had it too. For floating point systems where the
radix is a power of two (very common), they offer a means of clearly and
exactly specifying any finite floating point value.
For example, suppose we want to write the least positive value of an
IEEE754 double. With hexadecimal floating constants, we write
0x0.0000000000001p-1022 and a reader can immediately tell what this is.
One could equivalently write this in a normalized fashion as 0x1p-1074.
The exact representation of this value in base 10 is very, very long
(there are over 750 significant digits), but we can abuse roundoff to
write it succinctly as 5e-324, which is much less clear about which
value was intended.
Similarly, the greatest finite double value can be written as
0x1.fffffffffffffp+1023.
These constants have the form
0x[HH][.HHHHH]p[+/-]DDD
where we have the hexadecimal prefix 0x, zero or more hexadecimal
digits, and an optional fractional separator followed by zero or more
hexadecimal digits. There must be at least one hexadecimal digit.
Finally, there is a p followed by an (optionally signed) exponent
encoded in decimal.
The hexadecimal digits are interpreted in the obvious way and then
scaled by 2 raised to the given exponent.
Implementing this proposal would change the meaning of expressions like
let p4 = 5 in (+) 0x5p4
I don't believe anyone writes code like that, but it's resolved by
adding a space, as in
let p4 = 5 in (+) 0x5 p4
--
Nick Bowler, Elliptic Technologies (http://www.elliptictech.com/)
More information about the Haskell-prime
mailing list