From a2e28b2483c0fce746f3468e65807a810f143f04 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Wed, 18 Nov 2009 23:24:43 -0500 Subject: [PATCH] double: Simplify foreign calls. --- Data/Floating/Double.hs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Data/Floating/Double.hs b/Data/Floating/Double.hs index d16e1c5..e612532 100644 --- a/Data/Floating/Double.hs +++ b/Data/Floating/Double.hs @@ -23,13 +23,20 @@ foreign import ccall unsafe "double_to_string" double_to_string :: CString -> CDouble -> IO CInt foreign import ccall unsafe "double_signum" double_signum :: CDouble -> CDouble -foreign import ccall unsafe "fabs" - c_abs :: CDouble -> CDouble foreign import ccall unsafe "strtod" c_strtod :: CString -> Ptr CString -> IO CDouble + +foreign import ccall unsafe "fabs" + c_fabs :: CDouble -> CDouble foreign import ccall unsafe "copysign" c_copysign :: CDouble -> CDouble -> CDouble +libmDouble :: (CDouble -> CDouble) -> Double -> Double +libmDouble f a = toFloating $ f (toFloating a) + +libmDouble2 :: (CDouble -> CDouble -> CDouble) -> Double -> Double -> Double +libmDouble2 f a b = toFloating $ f (toFloating a) (toFloating b) + instance Show Double where show x = unsafePerformIO $ do size <- double_to_string nullPtr (toFloating x) @@ -55,9 +62,9 @@ instance Num Double where (D# x) - (D# y) = D# (x -## y) (D# x) * (D# y) = D# (x *## y) negate (D# x) = D# (negateDouble# x) - signum = toFloating . double_signum . toFloating - abs = toFloating . c_abs . toFloating fromInteger x = D# (doubleFromInteger x) + signum = libmDouble double_signum + abs = libmDouble c_fabs instance Fractional Double where (D# x) / (D# y) = D# (x /## y) @@ -67,4 +74,4 @@ instance Fractional Double where instance Floating Double where (D# x) ** (D# y) = D# (x **## y) - copysign x y = toFloating $ c_copysign (toFloating x) (toFloating y) + copysign = libmDouble2 c_copysign -- 2.11.4.GIT