RFC: Fixing floating point conversions.

Christian Maeder Christian.Maeder at dfki.de
Thu Feb 25 11:30:50 EST 2010


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?

> Add two new methods to the RealFloat class:
> 
>   toFloatConvert   :: RealFloat a => a -> FloatConvert
>   fromFloatConvert :: RealFloat a => FloatConvert -> a
> 
> and a function:
> 
>   toFloating :: (RealFloat a, RealFloat b) => a -> b
>   toFloating = fromFloatConvert . toFloatConvert
> 
> Advantages:
>   * No extensions (other than this one) beyond Haskell 98 are required.
>   * Simple to define instances, exactly two functions per floating type.
>   * Easy to add floating types to the language, and easy for users to
>     define their own in libraries.
> 
> Disadvantages:
>   * A data type whose sole purpose is to convert floating types seems
>     like a wart.
>   * 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"

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.

Cheers Christian


More information about the Haskell-prime mailing list