[Haskell-cafe] floating point operations and representation

Jed Brown jed at 59A2.org
Mon Mar 17 13:25:53 EDT 2008


On 17 Mar 2008, bulat.ziganshin at gmail.com wrote:

> Hello David,
>
> Monday, March 17, 2008, 7:59:09 PM, you wrote:
>
>>> foreign import ccall unsafe "math.h log10"
>>> c_log10 :: CDouble -> CDouble
>>>
>>> log10 :: Double -> Double
>>> log10 x = realToFrac (c_log10 (realToFrac x))
>
>> It's a bit sloppier, but shouldn't cause any trouble.  And I've no
>> idea how realToFrac is implemented, but would worry about it behaving
>> oddly... for instance when given NaNs.
>
> it should be nop (no operation) in such cases

A related issue:

  http://hackage.haskell.org/trac/ghc/ticket/2110


Presumably everyone is aware of

  decodeFloat :: (RealFloat a) => a -> (Integer, Int)

which really is a canonical representation of a floating point number.

As for realToFrac, this really isn't okay:

  *GHCi> 0/0
  NaN
  *GHCi> realToFrac (0/0)
  -Infinity

Also, this one might surprise a few people.

  *GHCi> realToFrac (realToFrac 0.2 :: Ratio Int)
  -Infinity
  *GHCi> realToFrac 0.2 :: Ratio Int
  (-858993459)%0

Jed
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20080317/cf8afe52/attachment.bin


More information about the Haskell-Cafe mailing list