1 % ------------------------------------------------------------------------------
2 % $Id: Float.lhs,v 1.5 2002/02/27 14:33:09 simonmar Exp $
4 % (c) The University of Glasgow, 1994-2000
7 \section[GHC.Num]{Module @GHC.Num@}
20 {-# OPTIONS -fno-implicit-prelude #-}
22 #include "ieee-flpt.h"
24 module GHC.Float( module GHC.Float, Float#, Double# ) where
39 %*********************************************************
41 \subsection{Standard numeric classes}
43 %*********************************************************
46 class (Fractional a) => Floating a where
48 exp, log, sqrt :: a -> a
49 (**), logBase :: a -> a -> a
50 sin, cos, tan :: a -> a
51 asin, acos, atan :: a -> a
52 sinh, cosh, tanh :: a -> a
53 asinh, acosh, atanh :: a -> a
55 x ** y = exp (log x * y)
56 logBase x y = log y / log x
59 tanh x = sinh x / cosh x
61 class (RealFrac a, Floating a) => RealFloat a where
62 floatRadix :: a -> Integer
63 floatDigits :: a -> Int
64 floatRange :: a -> (Int,Int)
65 decodeFloat :: a -> (Integer,Int)
66 encodeFloat :: Integer -> Int -> a
69 scaleFloat :: Int -> a -> a
70 isNaN, isInfinite, isDenormalized, isNegativeZero, isIEEE
75 exponent x = if m == 0 then 0 else n + floatDigits x
76 where (m,n) = decodeFloat x
78 significand x = encodeFloat m (negate (floatDigits x))
79 where (m,_) = decodeFloat x
81 scaleFloat k x = encodeFloat m (n+k)
82 where (m,n) = decodeFloat x
86 | x == 0 && y > 0 = pi/2
87 | x < 0 && y > 0 = pi + atan (y/x)
89 (x < 0 && isNegativeZero y) ||
90 (isNegativeZero x && isNegativeZero y)
92 | y == 0 && (x < 0 || isNegativeZero x)
93 = pi -- must be after the previous test on zero y
94 | x==0 && y==0 = y -- must be after the other double zero tests
95 | otherwise = x + y -- x or y is a NaN, return a NaN (via +)
99 %*********************************************************
101 \subsection{Type @Integer@, @Float@, @Double@}
103 %*********************************************************
106 data Float = F# Float#
107 data Double = D# Double#
109 instance CCallable Float
110 instance CReturnable Float
112 instance CCallable Double
113 instance CReturnable Double
117 %*********************************************************
119 \subsection{Type @Float@}
121 %*********************************************************
124 instance Eq Float where
125 (F# x) == (F# y) = x `eqFloat#` y
127 instance Ord Float where
128 (F# x) `compare` (F# y) | x `ltFloat#` y = LT
129 | x `eqFloat#` y = EQ
132 (F# x) < (F# y) = x `ltFloat#` y
133 (F# x) <= (F# y) = x `leFloat#` y
134 (F# x) >= (F# y) = x `geFloat#` y
135 (F# x) > (F# y) = x `gtFloat#` y
137 instance Num Float where
138 (+) x y = plusFloat x y
139 (-) x y = minusFloat x y
140 negate x = negateFloat x
141 (*) x y = timesFloat x y
143 | otherwise = negateFloat x
144 signum x | x == 0.0 = 0
146 | otherwise = negate 1
148 {-# INLINE fromInteger #-}
149 fromInteger n = encodeFloat n 0
150 -- It's important that encodeFloat inlines here, and that
151 -- fromInteger in turn inlines,
152 -- so that if fromInteger is applied to an (S# i) the right thing happens
154 instance Real Float where
155 toRational x = (m%1)*(b%1)^^n
156 where (m,n) = decodeFloat x
159 instance Fractional Float where
160 (/) x y = divideFloat x y
161 fromRational x = fromRat x
164 {-# RULES "truncate/Float->Int" truncate = float2Int #-}
165 instance RealFrac Float where
167 {-# SPECIALIZE properFraction :: Float -> (Int, Float) #-}
168 {-# SPECIALIZE round :: Float -> Int #-}
169 {-# SPECIALIZE ceiling :: Float -> Int #-}
170 {-# SPECIALIZE floor :: Float -> Int #-}
172 {-# SPECIALIZE properFraction :: Float -> (Integer, Float) #-}
173 {-# SPECIALIZE truncate :: Float -> Integer #-}
174 {-# SPECIALIZE round :: Float -> Integer #-}
175 {-# SPECIALIZE ceiling :: Float -> Integer #-}
176 {-# SPECIALIZE floor :: Float -> Integer #-}
179 = case (decodeFloat x) of { (m,n) ->
180 let b = floatRadix x in
182 (fromInteger m * fromInteger b ^ n, 0.0)
184 case (quotRem m (b^(negate n))) of { (w,r) ->
185 (fromInteger w, encodeFloat r n)
189 truncate x = case properFraction x of
192 round x = case properFraction x of
194 m = if r < 0.0 then n - 1 else n + 1
195 half_down = abs r - 0.5
197 case (compare half_down 0.0) of
199 EQ -> if even n then n else m
202 ceiling x = case properFraction x of
203 (n,r) -> if r > 0.0 then n + 1 else n
205 floor x = case properFraction x of
206 (n,r) -> if r < 0.0 then n - 1 else n
208 instance Floating Float where
209 pi = 3.141592653589793238
222 (**) x y = powerFloat x y
223 logBase x y = log y / log x
225 asinh x = log (x + sqrt (1.0+x*x))
226 acosh x = log (x + (x+1.0) * sqrt ((x-1.0)/(x+1.0)))
227 atanh x = log ((x+1.0) / sqrt (1.0-x*x))
229 instance RealFloat Float where
230 floatRadix _ = FLT_RADIX -- from float.h
231 floatDigits _ = FLT_MANT_DIG -- ditto
232 floatRange _ = (FLT_MIN_EXP, FLT_MAX_EXP) -- ditto
235 = case decodeFloat# f# of
236 (# exp#, s#, d# #) -> (J# s# d#, I# exp#)
238 encodeFloat (S# i) j = int_encodeFloat# i j
239 encodeFloat (J# s# d#) e = encodeFloat# s# d# e
241 exponent x = case decodeFloat x of
242 (m,n) -> if m == 0 then 0 else n + floatDigits x
244 significand x = case decodeFloat x of
245 (m,_) -> encodeFloat m (negate (floatDigits x))
247 scaleFloat k x = case decodeFloat x of
248 (m,n) -> encodeFloat m (n+k)
249 isNaN x = 0 /= isFloatNaN x
250 isInfinite x = 0 /= isFloatInfinite x
251 isDenormalized x = 0 /= isFloatDenormalized x
252 isNegativeZero x = 0 /= isFloatNegativeZero x
255 instance Show Float where
256 showsPrec x = showSigned showFloat x
257 showList = showList__ (showsPrec 0)
260 %*********************************************************
262 \subsection{Type @Double@}
264 %*********************************************************
267 instance Eq Double where
268 (D# x) == (D# y) = x ==## y
270 instance Ord Double where
271 (D# x) `compare` (D# y) | x <## y = LT
275 (D# x) < (D# y) = x <## y
276 (D# x) <= (D# y) = x <=## y
277 (D# x) >= (D# y) = x >=## y
278 (D# x) > (D# y) = x >## y
280 instance Num Double where
281 (+) x y = plusDouble x y
282 (-) x y = minusDouble x y
283 negate x = negateDouble x
284 (*) x y = timesDouble x y
286 | otherwise = negateDouble x
287 signum x | x == 0.0 = 0
289 | otherwise = negate 1
291 {-# INLINE fromInteger #-}
292 -- See comments with Num Float
293 fromInteger (S# i#) = case (int2Double# i#) of { d# -> D# d# }
294 fromInteger (J# s# d#) = encodeDouble# s# d# 0
297 instance Real Double where
298 toRational x = (m%1)*(b%1)^^n
299 where (m,n) = decodeFloat x
302 instance Fractional Double where
303 (/) x y = divideDouble x y
304 fromRational x = fromRat x
307 instance Floating Double where
308 pi = 3.141592653589793238
311 sqrt x = sqrtDouble x
315 asin x = asinDouble x
316 acos x = acosDouble x
317 atan x = atanDouble x
318 sinh x = sinhDouble x
319 cosh x = coshDouble x
320 tanh x = tanhDouble x
321 (**) x y = powerDouble x y
322 logBase x y = log y / log x
324 asinh x = log (x + sqrt (1.0+x*x))
325 acosh x = log (x + (x+1.0) * sqrt ((x-1.0)/(x+1.0)))
326 atanh x = log ((x+1.0) / sqrt (1.0-x*x))
328 {-# RULES "truncate/Double->Int" truncate = double2Int #-}
329 instance RealFrac Double where
331 {-# SPECIALIZE properFraction :: Double -> (Int, Double) #-}
332 {-# SPECIALIZE round :: Double -> Int #-}
333 {-# SPECIALIZE ceiling :: Double -> Int #-}
334 {-# SPECIALIZE floor :: Double -> Int #-}
336 {-# SPECIALIZE properFraction :: Double -> (Integer, Double) #-}
337 {-# SPECIALIZE truncate :: Double -> Integer #-}
338 {-# SPECIALIZE round :: Double -> Integer #-}
339 {-# SPECIALIZE ceiling :: Double -> Integer #-}
340 {-# SPECIALIZE floor :: Double -> Integer #-}
343 = case (decodeFloat x) of { (m,n) ->
344 let b = floatRadix x in
346 (fromInteger m * fromInteger b ^ n, 0.0)
348 case (quotRem m (b^(negate n))) of { (w,r) ->
349 (fromInteger w, encodeFloat r n)
353 truncate x = case properFraction x of
356 round x = case properFraction x of
358 m = if r < 0.0 then n - 1 else n + 1
359 half_down = abs r - 0.5
361 case (compare half_down 0.0) of
363 EQ -> if even n then n else m
366 ceiling x = case properFraction x of
367 (n,r) -> if r > 0.0 then n + 1 else n
369 floor x = case properFraction x of
370 (n,r) -> if r < 0.0 then n - 1 else n
372 instance RealFloat Double where
373 floatRadix _ = FLT_RADIX -- from float.h
374 floatDigits _ = DBL_MANT_DIG -- ditto
375 floatRange _ = (DBL_MIN_EXP, DBL_MAX_EXP) -- ditto
378 = case decodeDouble# x# of
379 (# exp#, s#, d# #) -> (J# s# d#, I# exp#)
381 encodeFloat (S# i) j = int_encodeDouble# i j
382 encodeFloat (J# s# d#) e = encodeDouble# s# d# e
384 exponent x = case decodeFloat x of
385 (m,n) -> if m == 0 then 0 else n + floatDigits x
387 significand x = case decodeFloat x of
388 (m,_) -> encodeFloat m (negate (floatDigits x))
390 scaleFloat k x = case decodeFloat x of
391 (m,n) -> encodeFloat m (n+k)
393 isNaN x = 0 /= isDoubleNaN x
394 isInfinite x = 0 /= isDoubleInfinite x
395 isDenormalized x = 0 /= isDoubleDenormalized x
396 isNegativeZero x = 0 /= isDoubleNegativeZero x
399 instance Show Double where
400 showsPrec x = showSigned showFloat x
401 showList = showList__ (showsPrec 0)
404 %*********************************************************
406 \subsection{@Enum@ instances}
408 %*********************************************************
410 The @Enum@ instances for Floats and Doubles are slightly unusual.
411 The @toEnum@ function truncates numbers to Int. The definitions
412 of @enumFrom@ and @enumFromThen@ allow floats to be used in arithmetic
413 series: [0,0.1 .. 1.0]. However, roundoff errors make these somewhat
414 dubious. This example may have either 10 or 11 elements, depending on
415 how 0.1 is represented.
417 NOTE: The instances for Float and Double do not make use of the default
418 methods for @enumFromTo@ and @enumFromThenTo@, as these rely on there being
419 a `non-lossy' conversion to and from Ints. Instead we make use of the
420 1.2 default methods (back in the days when Enum had Ord as a superclass)
421 for these (@numericEnumFromTo@ and @numericEnumFromThenTo@ below.)
424 instance Enum Float where
428 fromEnum = fromInteger . truncate -- may overflow
429 enumFrom = numericEnumFrom
430 enumFromTo = numericEnumFromTo
431 enumFromThen = numericEnumFromThen
432 enumFromThenTo = numericEnumFromThenTo
434 instance Enum Double where
438 fromEnum = fromInteger . truncate -- may overflow
439 enumFrom = numericEnumFrom
440 enumFromTo = numericEnumFromTo
441 enumFromThen = numericEnumFromThen
442 enumFromThenTo = numericEnumFromThenTo
446 %*********************************************************
448 \subsection{Printing floating point}
450 %*********************************************************
454 showFloat :: (RealFloat a) => a -> ShowS
455 showFloat x = showString (formatRealFloat FFGeneric Nothing x)
457 -- These are the format types. This type is not exported.
459 data FFFormat = FFExponent | FFFixed | FFGeneric
461 formatRealFloat :: (RealFloat a) => FFFormat -> Maybe Int -> a -> String
462 formatRealFloat fmt decs x
464 | isInfinite x = if x < 0 then "-Infinity" else "Infinity"
465 | x < 0 || isNegativeZero x = '-':doFmt fmt (floatToDigits (toInteger base) (-x))
466 | otherwise = doFmt fmt (floatToDigits (toInteger base) x)
470 doFmt format (is, e) =
471 let ds = map intToDigit is in
474 doFmt (if e < 0 || e > 7 then FFExponent else FFFixed)
479 let show_e' = show (e-1) in
482 [d] -> d : ".0e" ++ show_e'
483 (d:ds') -> d : '.' : ds' ++ "e" ++ show_e'
485 let dec' = max dec 1 in
487 [0] -> '0' :'.' : take dec' (repeat '0') ++ "e0"
490 (ei,is') = roundTo base (dec'+1) is
491 (d:ds') = map intToDigit (if ei > 0 then init is' else is')
493 d:'.':ds' ++ 'e':show (e-1+ei)
496 mk0 ls = case ls of { "" -> "0" ; _ -> ls}
500 | e <= 0 -> "0." ++ replicate (-e) '0' ++ ds
503 f 0 s rs = mk0 (reverse s) ++ '.':mk0 rs
504 f n s "" = f (n-1) ('0':s) ""
505 f n s (r:rs) = f (n-1) (r:s) rs
509 let dec' = max dec 0 in
512 (ei,is') = roundTo base (dec' + e) is
513 (ls,rs) = splitAt (e+ei) (map intToDigit is')
515 mk0 ls ++ (if null rs then "" else '.':rs)
518 (ei,is') = roundTo base dec' (replicate (-e) 0 ++ is)
519 d:ds' = map intToDigit (if ei > 0 then is' else 0:is')
521 d : (if null ds' then "" else '.':ds')
524 roundTo :: Int -> Int -> [Int] -> (Int,[Int])
532 f n [] = (0, replicate n 0)
533 f 0 (x:_) = (if x >= b2 then 1 else 0, [])
535 | i' == base = (1,0:ds)
536 | otherwise = (0,i':ds)
541 -- Based on "Printing Floating-Point Numbers Quickly and Accurately"
542 -- by R.G. Burger and R.K. Dybvig in PLDI 96.
543 -- This version uses a much slower logarithm estimator. It should be improved.
545 -- floatToDigits takes a base and a non-negative RealFloat number,
546 -- and returns a list of digits and an exponent.
547 -- In particular, if x>=0, and
548 -- floatToDigits base x = ([d1,d2,...,dn], e)
551 -- (b) x = 0.d1d2...dn * (base**e)
552 -- (c) 0 <= di <= base-1
554 floatToDigits :: (RealFloat a) => Integer -> a -> ([Int], Int)
555 floatToDigits _ 0 = ([0], 0)
556 floatToDigits base x =
558 (f0, e0) = decodeFloat x
559 (minExp0, _) = floatRange x
562 minExp = minExp0 - p -- the real minimum exponent
563 -- Haskell requires that f be adjusted so denormalized numbers
564 -- will have an impossibly low exponent. Adjust for this.
566 let n = minExp - e0 in
567 if n > 0 then (f0 `div` (b^n), e0+n) else (f0, e0)
572 (f*be*b*2, 2*b, be*b, b)
576 if e > minExp && f == b^(p-1) then
577 (f*b*2, b^(-e+1)*2, b, 1)
579 (f*2, b^(-e)*2, 1, 1)
583 if b == 2 && base == 10 then
584 -- logBase 10 2 is slightly bigger than 3/10 so
585 -- the following will err on the low side. Ignoring
586 -- the fraction will make it err even more.
587 -- Haskell promises that p-1 <= logBase b f < p.
588 (p - 1 + e0) * 3 `div` 10
590 ceiling ((log (fromInteger (f+1)) +
591 fromInteger (int2Integer e) * log (fromInteger b)) /
592 log (fromInteger base))
593 --WAS: fromInt e * log (fromInteger b))
597 if r + mUp <= expt base n * s then n else fixup (n+1)
599 if expt base (-n) * (r + mUp) <= s then n else fixup (n+1)
603 gen ds rn sN mUpN mDnN =
605 (dn, rn') = (rn * base) `divMod` sN
609 case (rn' < mDnN', rn' + mUpN' > sN) of
610 (True, False) -> dn : ds
611 (False, True) -> dn+1 : ds
612 (True, True) -> if rn' * 2 < sN then dn : ds else dn+1 : ds
613 (False, False) -> gen (dn:ds) rn' sN mUpN' mDnN'
617 gen [] r (s * expt base k) mUp mDn
619 let bk = expt base (-k) in
620 gen [] (r * bk) s (mUp * bk) (mDn * bk)
622 (map fromIntegral (reverse rds), k)
627 %*********************************************************
629 \subsection{Converting from a Rational to a RealFloat
631 %*********************************************************
633 [In response to a request for documentation of how fromRational works,
634 Joe Fasel writes:] A quite reasonable request! This code was added to
635 the Prelude just before the 1.2 release, when Lennart, working with an
636 early version of hbi, noticed that (read . show) was not the identity
637 for floating-point numbers. (There was a one-bit error about half the
638 time.) The original version of the conversion function was in fact
639 simply a floating-point divide, as you suggest above. The new version
640 is, I grant you, somewhat denser.
642 Unfortunately, Joe's code doesn't work! Here's an example:
644 main = putStr (shows (1.82173691287639817263897126389712638972163e-300::Double) "\n")
649 1.8217369128763981e-300
654 fromRat :: (RealFloat a) => Rational -> a
658 -- If the exponent of the nearest floating-point number to x
659 -- is e, then the significand is the integer nearest xb^(-e),
660 -- where b is the floating-point radix. We start with a good
661 -- guess for e, and if it is correct, the exponent of the
662 -- floating-point number we construct will again be e. If
663 -- not, one more iteration is needed.
665 f e = if e' == e then y else f e'
666 where y = encodeFloat (round (x * (1 % b)^^e)) e
667 (_,e') = decodeFloat y
670 -- We obtain a trial exponent by doing a floating-point
671 -- division of x's numerator by its denominator. The
672 -- result of this division may not itself be the ultimate
673 -- result, because of an accumulation of three rounding
676 (s,e) = decodeFloat (fromInteger (numerator x) `asTypeOf` x'
677 / fromInteger (denominator x))
680 Now, here's Lennart's code (which works)
683 {-# SPECIALISE fromRat ::
685 Rational -> Float #-}
686 fromRat :: (RealFloat a) => Rational -> a
688 | x == 0 = encodeFloat 0 0 -- Handle exceptional cases
689 | x < 0 = - fromRat' (-x) -- first.
690 | otherwise = fromRat' x
692 -- Conversion process:
693 -- Scale the rational number by the RealFloat base until
694 -- it lies in the range of the mantissa (as used by decodeFloat/encodeFloat).
695 -- Then round the rational to an Integer and encode it with the exponent
696 -- that we got from the scaling.
697 -- To speed up the scaling process we compute the log2 of the number to get
698 -- a first guess of the exponent.
700 fromRat' :: (RealFloat a) => Rational -> a
702 where b = floatRadix r
704 (minExp0, _) = floatRange r
705 minExp = minExp0 - p -- the real minimum exponent
706 xMin = toRational (expt b (p-1))
707 xMax = toRational (expt b p)
708 p0 = (integerLogBase b (numerator x) - integerLogBase b (denominator x) - p) `max` minExp
709 f = if p0 < 0 then 1 % expt b (-p0) else expt b p0 % 1
710 (x', p') = scaleRat (toRational b) minExp xMin xMax p0 (x / f)
711 r = encodeFloat (round x') p'
713 -- Scale x until xMin <= x < xMax, or p (the exponent) <= minExp.
714 scaleRat :: Rational -> Int -> Rational -> Rational -> Int -> Rational -> (Rational, Int)
715 scaleRat b minExp xMin xMax p x
716 | p <= minExp = (x, p)
717 | x >= xMax = scaleRat b minExp xMin xMax (p+1) (x/b)
718 | x < xMin = scaleRat b minExp xMin xMax (p-1) (x*b)
721 -- Exponentiation with a cache for the most common numbers.
722 minExpt, maxExpt :: Int
726 expt :: Integer -> Int -> Integer
728 if base == 2 && n >= minExpt && n <= maxExpt then
733 expts :: Array Int Integer
734 expts = array (minExpt,maxExpt) [(n,2^n) | n <- [minExpt .. maxExpt]]
736 -- Compute the (floor of the) log of i in base b.
737 -- Simplest way would be just divide i by b until it's smaller then b, but that would
738 -- be very slow! We are just slightly more clever.
739 integerLogBase :: Integer -> Integer -> Int
742 | otherwise = doDiv (i `div` (b^l)) l
744 -- Try squaring the base first to cut down the number of divisions.
745 l = 2 * integerLogBase (b*b) i
747 doDiv :: Integer -> Int -> Int
750 | otherwise = doDiv (x `div` b) (y+1)
755 %*********************************************************
757 \subsection{Floating point numeric primops}
759 %*********************************************************
761 Definitions of the boxed PrimOps; these will be
762 used in the case of partial applications, etc.
765 plusFloat, minusFloat, timesFloat, divideFloat :: Float -> Float -> Float
766 plusFloat (F# x) (F# y) = F# (plusFloat# x y)
767 minusFloat (F# x) (F# y) = F# (minusFloat# x y)
768 timesFloat (F# x) (F# y) = F# (timesFloat# x y)
769 divideFloat (F# x) (F# y) = F# (divideFloat# x y)
771 negateFloat :: Float -> Float
772 negateFloat (F# x) = F# (negateFloat# x)
774 gtFloat, geFloat, eqFloat, neFloat, ltFloat, leFloat :: Float -> Float -> Bool
775 gtFloat (F# x) (F# y) = gtFloat# x y
776 geFloat (F# x) (F# y) = geFloat# x y
777 eqFloat (F# x) (F# y) = eqFloat# x y
778 neFloat (F# x) (F# y) = neFloat# x y
779 ltFloat (F# x) (F# y) = ltFloat# x y
780 leFloat (F# x) (F# y) = leFloat# x y
782 float2Int :: Float -> Int
783 float2Int (F# x) = I# (float2Int# x)
785 int2Float :: Int -> Float
786 int2Float (I# x) = F# (int2Float# x)
788 expFloat, logFloat, sqrtFloat :: Float -> Float
789 sinFloat, cosFloat, tanFloat :: Float -> Float
790 asinFloat, acosFloat, atanFloat :: Float -> Float
791 sinhFloat, coshFloat, tanhFloat :: Float -> Float
792 expFloat (F# x) = F# (expFloat# x)
793 logFloat (F# x) = F# (logFloat# x)
794 sqrtFloat (F# x) = F# (sqrtFloat# x)
795 sinFloat (F# x) = F# (sinFloat# x)
796 cosFloat (F# x) = F# (cosFloat# x)
797 tanFloat (F# x) = F# (tanFloat# x)
798 asinFloat (F# x) = F# (asinFloat# x)
799 acosFloat (F# x) = F# (acosFloat# x)
800 atanFloat (F# x) = F# (atanFloat# x)
801 sinhFloat (F# x) = F# (sinhFloat# x)
802 coshFloat (F# x) = F# (coshFloat# x)
803 tanhFloat (F# x) = F# (tanhFloat# x)
805 powerFloat :: Float -> Float -> Float
806 powerFloat (F# x) (F# y) = F# (powerFloat# x y)
808 -- definitions of the boxed PrimOps; these will be
809 -- used in the case of partial applications, etc.
811 plusDouble, minusDouble, timesDouble, divideDouble :: Double -> Double -> Double
812 plusDouble (D# x) (D# y) = D# (x +## y)
813 minusDouble (D# x) (D# y) = D# (x -## y)
814 timesDouble (D# x) (D# y) = D# (x *## y)
815 divideDouble (D# x) (D# y) = D# (x /## y)
817 negateDouble :: Double -> Double
818 negateDouble (D# x) = D# (negateDouble# x)
820 gtDouble, geDouble, eqDouble, neDouble, leDouble, ltDouble :: Double -> Double -> Bool
821 gtDouble (D# x) (D# y) = x >## y
822 geDouble (D# x) (D# y) = x >=## y
823 eqDouble (D# x) (D# y) = x ==## y
824 neDouble (D# x) (D# y) = x /=## y
825 ltDouble (D# x) (D# y) = x <## y
826 leDouble (D# x) (D# y) = x <=## y
828 double2Int :: Double -> Int
829 double2Int (D# x) = I# (double2Int# x)
831 int2Double :: Int -> Double
832 int2Double (I# x) = D# (int2Double# x)
834 double2Float :: Double -> Float
835 double2Float (D# x) = F# (double2Float# x)
837 float2Double :: Float -> Double
838 float2Double (F# x) = D# (float2Double# x)
840 expDouble, logDouble, sqrtDouble :: Double -> Double
841 sinDouble, cosDouble, tanDouble :: Double -> Double
842 asinDouble, acosDouble, atanDouble :: Double -> Double
843 sinhDouble, coshDouble, tanhDouble :: Double -> Double
844 expDouble (D# x) = D# (expDouble# x)
845 logDouble (D# x) = D# (logDouble# x)
846 sqrtDouble (D# x) = D# (sqrtDouble# x)
847 sinDouble (D# x) = D# (sinDouble# x)
848 cosDouble (D# x) = D# (cosDouble# x)
849 tanDouble (D# x) = D# (tanDouble# x)
850 asinDouble (D# x) = D# (asinDouble# x)
851 acosDouble (D# x) = D# (acosDouble# x)
852 atanDouble (D# x) = D# (atanDouble# x)
853 sinhDouble (D# x) = D# (sinhDouble# x)
854 coshDouble (D# x) = D# (coshDouble# x)
855 tanhDouble (D# x) = D# (tanhDouble# x)
857 powerDouble :: Double -> Double -> Double
858 powerDouble (D# x) (D# y) = D# (x **## y)
862 foreign import ccall unsafe "__encodeFloat"
863 encodeFloat# :: Int# -> ByteArray# -> Int -> Float
864 foreign import ccall unsafe "__int_encodeFloat"
865 int_encodeFloat# :: Int# -> Int -> Float
868 foreign import ccall unsafe "isFloatNaN" isFloatNaN :: Float -> Int
869 foreign import ccall unsafe "isFloatInfinite" isFloatInfinite :: Float -> Int
870 foreign import ccall unsafe "isFloatDenormalized" isFloatDenormalized :: Float -> Int
871 foreign import ccall unsafe "isFloatNegativeZero" isFloatNegativeZero :: Float -> Int
874 foreign import ccall unsafe "__encodeDouble"
875 encodeDouble# :: Int# -> ByteArray# -> Int -> Double
876 foreign import ccall unsafe "__int_encodeDouble"
877 int_encodeDouble# :: Int# -> Int -> Double
879 foreign import ccall unsafe "isDoubleNaN" isDoubleNaN :: Double -> Int
880 foreign import ccall unsafe "isDoubleInfinite" isDoubleInfinite :: Double -> Int
881 foreign import ccall unsafe "isDoubleDenormalized" isDoubleDenormalized :: Double -> Int
882 foreign import ccall unsafe "isDoubleNegativeZero" isDoubleNegativeZero :: Double -> Int
885 %*********************************************************
887 \subsection{Coercion rules}
889 %*********************************************************
893 "fromIntegral/Int->Float" fromIntegral = int2Float
894 "fromIntegral/Int->Double" fromIntegral = int2Double
895 "realToFrac/Float->Float" realToFrac = id :: Float -> Float
896 "realToFrac/Float->Double" realToFrac = float2Double
897 "realToFrac/Double->Float" realToFrac = double2Float
898 "realToFrac/Double->Double" realToFrac = id :: Double -> Double