RFC: Fixing floating point conversions.

Nick Bowler nbowler at elliptictech.com
Thu Feb 25 11:59:05 EST 2010


On 17:30 Thu 25 Feb     , Christian Maeder wrote:
> Nick Bowler schrieb:
> > *** Idea #2 ***
> > 
> > Similar to #1, except using a "generic" type instead of Double.
> > 
> > Define a new type, call it FloatConvert, which represents "rational plus
> > other values".  Something along the lines of:
> > 
> >   data FloatConvert
> >       = FCZero Bool       -- Signed zero
> >       | FCInfinity Bool   -- Signed infinity
> >       | FCNaN Integer     -- Generic NaN
> >       | FCFinite Rational -- Finite, non-zero value
> 
> interesting. What is the Integer in FCNaN for?

Many floating point formats have multiple NaNs.  In the IEEE 754 binary
formats, a NaN is specified by a maximum exponent and *any* non-zero
significand.  The extra bits are sometimes used for diagnostic
information.  There are signaling and quiet NaNs, and they have a sign
bit.  IEEE 754 recommends that operations involving NaNs preserve as
much of this information as possible.

I chose Integer since it can encode all of this information.  It is
desirable for conversions from a type to itself to be the identity
function, even in the presence of multiple NaNs.  I'm sure many other
encodings are workable.

> >   * While the free-form encoding of NaN values will allow conversion
> >     from a type to itself to be the identity function, it may make
> >     it tricky to perform the "ideal" conversion between different
> >     types.
> 
> I don't understand this last point about "free-form encoding of NaN"

It's free-form in that, as I specified it, it's up to the particular
RealFloat instance to decide how the Integer is used.  This might make
conversions which preserve, say, signaling NaNs trickier to implement.

> I would come up with a data type like:
> 
>   data ExtNum a
>     = NegativeZero
>     | NaN
>     | Infinity Bool
>     | Num a
> 
> add instances for the classes, Eq, Ord, Num, ....
> (depending on "a" that must be at least from the class Num for "0")
> 
> and use "ExtNum Rational" for floating point conversions.

-- 
Nick Bowler, Elliptic Technologies (http://www.elliptictech.com/)


More information about the Haskell-prime mailing list