[Haskell-cafe] floating point operations and representation

Don Stewart dons at galois.com
Mon Mar 17 13:28:10 EDT 2008


daveroundy:
> 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.

It's a no-op (from the core I was looking at), and then you're not worrying about coercing newtypes.

-- Don


More information about the Haskell-Cafe mailing list