[Haskell-cafe] ANN: convertible (first release)

wren ng thornton wren at freegeek.org
Tue Jan 27 21:41:30 EST 2009


John Goerzen wrote:
> Hi folks,
> 
> I have uploaded a new package to Haskell: convertible.  At its heart,
> it's a very simple typeclass that's designed to enable a reasonable
> default conversion between two different types without having to
> remember a bunch of functions.

I once again point out that realToFrac is *wrong* for converting from 
Float or Double.

     > realToFrac (1/0::Float) ::Double
     3.402823669209385e38
     >
     > realToFrac (0/0::Float) ::Double
     -5.104235503814077e38

     > realToFrac (0/0::Double) ::Float
     -Infinity

     > realToFrac (1/0::Float) ::Rational
     340282366920938463463374607431768211456%1
     >
     > realToFrac (0/0::Float) ::Rational
     (-510423550381407695195061911147652317184)%1

     > realToFrac (1/0::Double) ::Rational
179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216%1
     >
     > realToFrac (0/0::Double) ::Rational
(-269653970229347386159395778618353710042696546841345985910145121736599013708251444699062715983611304031680170819807090036488184653221624933739271145959211186566651840137298227914453329401869141179179624428127508653257226023513694322210869665811240855745025766026879447359920868907719574457253034494436336205824)%1


These exceptional values are not uncommon and should be dealt with 
correctly. The code to do this is already written in 
logfloat:Data.Number.Transfinite[1], simply use the realToFrac method of 
the RealToFrac class instead of the Prelude's version which is broken.

Since there is no total conversion from Float/Double to Rational, you 
can use the isInfinite and isNaN members of the Transfinite class to 
capture exceptional values as errors.


[1] http://hackage.haskell.org/cgi-bin/hackage-scripts/package/logfloat

-- 
Live well,
~wren


More information about the Haskell-Cafe mailing list