[Haskell-cafe] floating point operations and representation

David Roundy daveroundy at gmail.com
Mon Mar 17 12:59:09 EDT 2008


On Wed, Mar 12, 2008 at 9:27 PM, Don Stewart <dons at galois.com> wrote:
>  You could consider binding directly to the C functions, if needed,
>
>     {-# OPTIONS -fffi -#include "math.h" #-}
>
>     import Foreign.C.Types
>
>     foreign import ccall unsafe "math.h log10"
>         c_log10 :: CDouble -> CDouble
>
>     log10 :: Double -> Double
>     log10 x = realToFrac (c_log10 (realToFrac x))

Actually, you could almost certainly just use

foreign import ccall unsafe "math.h log10" log10 :: Double -> Double

since in ghc CDouble and Double are identical.

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.

David


More information about the Haskell-Cafe mailing list