2 {-# OPTIONS -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
6 -- Copyright : (c) The University of Glasgow 1994-2002
7 -- License : see libraries/base/LICENSE
9 -- Maintainer : cvs-ghc@haskell.org
10 -- Stability : internal
11 -- Portability : non-portable (GHC Extensions)
13 -- The types 'Float' and 'Double', and the classes 'Floating' and 'RealFloat'.
15 -----------------------------------------------------------------------------
17 #include "ieee-flpt.h"
19 module GHC.Float( module GHC.Float, Float#, Double# ) where
34 %*********************************************************
36 \subsection{Standard numeric classes}
38 %*********************************************************
41 class (Fractional a) => Floating a where
43 exp, log, sqrt :: a -> a
44 (**), logBase :: a -> a -> a
45 sin, cos, tan :: a -> a
46 asin, acos, atan :: a -> a
47 sinh, cosh, tanh :: a -> a
48 asinh, acosh, atanh :: a -> a
50 x ** y = exp (log x * y)
51 logBase x y = log y / log x
54 tanh x = sinh x / cosh x
56 class (RealFrac a, Floating a) => RealFloat a where
57 floatRadix :: a -> Integer
58 floatDigits :: a -> Int
59 floatRange :: a -> (Int,Int)
60 decodeFloat :: a -> (Integer,Int)
61 encodeFloat :: Integer -> Int -> a
64 scaleFloat :: Int -> a -> a
65 isNaN, isInfinite, isDenormalized, isNegativeZero, isIEEE
70 exponent x = if m == 0 then 0 else n + floatDigits x
71 where (m,n) = decodeFloat x
73 significand x = encodeFloat m (negate (floatDigits x))
74 where (m,_) = decodeFloat x
76 scaleFloat k x = encodeFloat m (n+k)
77 where (m,n) = decodeFloat x
81 | x == 0 && y > 0 = pi/2
82 | x < 0 && y > 0 = pi + atan (y/x)
84 (x < 0 && isNegativeZero y) ||
85 (isNegativeZero x && isNegativeZero y)
87 | y == 0 && (x < 0 || isNegativeZero x)
88 = pi -- must be after the previous test on zero y
89 | x==0 && y==0 = y -- must be after the other double zero tests
90 | otherwise = x + y -- x or y is a NaN, return a NaN (via +)
94 %*********************************************************
96 \subsection{Type @Integer@, @Float@, @Double@}
98 %*********************************************************
101 -- | Single-precision floating point numbers.
102 data Float = F# Float#
104 -- | Double-precision floating point numbers.
105 data Double = D# Double#
109 %*********************************************************
111 \subsection{Type @Float@}
113 %*********************************************************
116 instance Eq Float where
117 (F# x) == (F# y) = x `eqFloat#` y
119 instance Ord Float where
120 (F# x) `compare` (F# y) | x `ltFloat#` y = LT
121 | x `eqFloat#` y = EQ
124 (F# x) < (F# y) = x `ltFloat#` y
125 (F# x) <= (F# y) = x `leFloat#` y
126 (F# x) >= (F# y) = x `geFloat#` y
127 (F# x) > (F# y) = x `gtFloat#` y
129 instance Num Float where
130 (+) x y = plusFloat x y
131 (-) x y = minusFloat x y
132 negate x = negateFloat x
133 (*) x y = timesFloat x y
135 | otherwise = negateFloat x
136 signum x | x == 0.0 = 0
138 | otherwise = negate 1
140 {-# INLINE fromInteger #-}
141 fromInteger n = encodeFloat n 0
142 -- It's important that encodeFloat inlines here, and that
143 -- fromInteger in turn inlines,
144 -- so that if fromInteger is applied to an (S# i) the right thing happens
146 instance Real Float where
147 toRational x = (m%1)*(b%1)^^n
148 where (m,n) = decodeFloat x
151 instance Fractional Float where
152 (/) x y = divideFloat x y
153 fromRational x = fromRat x
156 {-# RULES "truncate/Float->Int" truncate = float2Int #-}
157 instance RealFrac Float where
159 {-# SPECIALIZE properFraction :: Float -> (Int, Float) #-}
160 {-# SPECIALIZE round :: Float -> Int #-}
162 {-# SPECIALIZE properFraction :: Float -> (Integer, Float) #-}
163 {-# SPECIALIZE round :: Float -> Integer #-}
165 -- ceiling, floor, and truncate are all small
166 {-# INLINE ceiling #-}
168 {-# INLINE truncate #-}
171 = case (decodeFloat x) of { (m,n) ->
172 let b = floatRadix x in
174 (fromInteger m * fromInteger b ^ n, 0.0)
176 case (quotRem m (b^(negate n))) of { (w,r) ->
177 (fromInteger w, encodeFloat r n)
181 truncate x = case properFraction x of
184 round x = case properFraction x of
186 m = if r < 0.0 then n - 1 else n + 1
187 half_down = abs r - 0.5
189 case (compare half_down 0.0) of
191 EQ -> if even n then n else m
194 ceiling x = case properFraction x of
195 (n,r) -> if r > 0.0 then n + 1 else n
197 floor x = case properFraction x of
198 (n,r) -> if r < 0.0 then n - 1 else n
200 instance Floating Float where
201 pi = 3.141592653589793238
214 (**) x y = powerFloat x y
215 logBase x y = log y / log x
217 asinh x = log (x + sqrt (1.0+x*x))
218 acosh x = log (x + (x+1.0) * sqrt ((x-1.0)/(x+1.0)))
219 atanh x = log ((x+1.0) / sqrt (1.0-x*x))
221 instance RealFloat Float where
222 floatRadix _ = FLT_RADIX -- from float.h
223 floatDigits _ = FLT_MANT_DIG -- ditto
224 floatRange _ = (FLT_MIN_EXP, FLT_MAX_EXP) -- ditto
227 = case decodeFloat# f# of
228 (# exp#, s#, d# #) -> (J# s# d#, I# exp#)
230 encodeFloat (S# i) j = int_encodeFloat# i j
231 encodeFloat (J# s# d#) e = encodeFloat# s# d# e
233 exponent x = case decodeFloat x of
234 (m,n) -> if m == 0 then 0 else n + floatDigits x
236 significand x = case decodeFloat x of
237 (m,_) -> encodeFloat m (negate (floatDigits x))
239 scaleFloat k x = case decodeFloat x of
240 (m,n) -> encodeFloat m (n+k)
241 isNaN x = 0 /= isFloatNaN x
242 isInfinite x = 0 /= isFloatInfinite x
243 isDenormalized x = 0 /= isFloatDenormalized x
244 isNegativeZero x = 0 /= isFloatNegativeZero x
247 instance Show Float where
248 showsPrec x = showSigned showFloat x
249 showList = showList__ (showsPrec 0)
252 %*********************************************************
254 \subsection{Type @Double@}
256 %*********************************************************
259 instance Eq Double where
260 (D# x) == (D# y) = x ==## y
262 instance Ord Double where
263 (D# x) `compare` (D# y) | x <## y = LT
267 (D# x) < (D# y) = x <## y
268 (D# x) <= (D# y) = x <=## y
269 (D# x) >= (D# y) = x >=## y
270 (D# x) > (D# y) = x >## y
272 instance Num Double where
273 (+) x y = plusDouble x y
274 (-) x y = minusDouble x y
275 negate x = negateDouble x
276 (*) x y = timesDouble x y
278 | otherwise = negateDouble x
279 signum x | x == 0.0 = 0
281 | otherwise = negate 1
283 {-# INLINE fromInteger #-}
284 -- See comments with Num Float
285 fromInteger (S# i#) = case (int2Double# i#) of { d# -> D# d# }
286 fromInteger (J# s# d#) = encodeDouble# s# d# 0
289 instance Real Double where
290 toRational x = (m%1)*(b%1)^^n
291 where (m,n) = decodeFloat x
294 instance Fractional Double where
295 (/) x y = divideDouble x y
296 fromRational x = fromRat x
299 instance Floating Double where
300 pi = 3.141592653589793238
303 sqrt x = sqrtDouble x
307 asin x = asinDouble x
308 acos x = acosDouble x
309 atan x = atanDouble x
310 sinh x = sinhDouble x
311 cosh x = coshDouble x
312 tanh x = tanhDouble x
313 (**) x y = powerDouble x y
314 logBase x y = log y / log x
316 asinh x = log (x + sqrt (1.0+x*x))
317 acosh x = log (x + (x+1.0) * sqrt ((x-1.0)/(x+1.0)))
318 atanh x = log ((x+1.0) / sqrt (1.0-x*x))
320 {-# RULES "truncate/Double->Int" truncate = double2Int #-}
321 instance RealFrac Double where
323 {-# SPECIALIZE properFraction :: Double -> (Int, Double) #-}
324 {-# SPECIALIZE round :: Double -> Int #-}
326 {-# SPECIALIZE properFraction :: Double -> (Integer, Double) #-}
327 {-# SPECIALIZE round :: Double -> Integer #-}
329 -- ceiling, floor, and truncate are all small
330 {-# INLINE ceiling #-}
332 {-# INLINE truncate #-}
335 = case (decodeFloat x) of { (m,n) ->
336 let b = floatRadix x in
338 (fromInteger m * fromInteger b ^ n, 0.0)
340 case (quotRem m (b^(negate n))) of { (w,r) ->
341 (fromInteger w, encodeFloat r n)
345 truncate x = case properFraction x of
348 round x = case properFraction x of
350 m = if r < 0.0 then n - 1 else n + 1
351 half_down = abs r - 0.5
353 case (compare half_down 0.0) of
355 EQ -> if even n then n else m
358 ceiling x = case properFraction x of
359 (n,r) -> if r > 0.0 then n + 1 else n
361 floor x = case properFraction x of
362 (n,r) -> if r < 0.0 then n - 1 else n
364 instance RealFloat Double where
365 floatRadix _ = FLT_RADIX -- from float.h
366 floatDigits _ = DBL_MANT_DIG -- ditto
367 floatRange _ = (DBL_MIN_EXP, DBL_MAX_EXP) -- ditto
370 = case decodeDouble# x# of
371 (# exp#, s#, d# #) -> (J# s# d#, I# exp#)
373 encodeFloat (S# i) j = int_encodeDouble# i j
374 encodeFloat (J# s# d#) e = encodeDouble# s# d# e
376 exponent x = case decodeFloat x of
377 (m,n) -> if m == 0 then 0 else n + floatDigits x
379 significand x = case decodeFloat x of
380 (m,_) -> encodeFloat m (negate (floatDigits x))
382 scaleFloat k x = case decodeFloat x of
383 (m,n) -> encodeFloat m (n+k)
385 isNaN x = 0 /= isDoubleNaN x
386 isInfinite x = 0 /= isDoubleInfinite x
387 isDenormalized x = 0 /= isDoubleDenormalized x
388 isNegativeZero x = 0 /= isDoubleNegativeZero x
391 instance Show Double where
392 showsPrec x = showSigned showFloat x
393 showList = showList__ (showsPrec 0)
396 %*********************************************************
398 \subsection{@Enum@ instances}
400 %*********************************************************
402 The @Enum@ instances for Floats and Doubles are slightly unusual.
403 The @toEnum@ function truncates numbers to Int. The definitions
404 of @enumFrom@ and @enumFromThen@ allow floats to be used in arithmetic
405 series: [0,0.1 .. 1.0]. However, roundoff errors make these somewhat
406 dubious. This example may have either 10 or 11 elements, depending on
407 how 0.1 is represented.
409 NOTE: The instances for Float and Double do not make use of the default
410 methods for @enumFromTo@ and @enumFromThenTo@, as these rely on there being
411 a `non-lossy' conversion to and from Ints. Instead we make use of the
412 1.2 default methods (back in the days when Enum had Ord as a superclass)
413 for these (@numericEnumFromTo@ and @numericEnumFromThenTo@ below.)
416 instance Enum Float where
420 fromEnum = fromInteger . truncate -- may overflow
421 enumFrom = numericEnumFrom
422 enumFromTo = numericEnumFromTo
423 enumFromThen = numericEnumFromThen
424 enumFromThenTo = numericEnumFromThenTo
426 instance Enum Double where
430 fromEnum = fromInteger . truncate -- may overflow
431 enumFrom = numericEnumFrom
432 enumFromTo = numericEnumFromTo
433 enumFromThen = numericEnumFromThen
434 enumFromThenTo = numericEnumFromThenTo
438 %*********************************************************
440 \subsection{Printing floating point}
442 %*********************************************************
446 showFloat :: (RealFloat a) => a -> ShowS
447 showFloat x = showString (formatRealFloat FFGeneric Nothing x)
449 -- These are the format types. This type is not exported.
451 data FFFormat = FFExponent | FFFixed | FFGeneric
453 formatRealFloat :: (RealFloat a) => FFFormat -> Maybe Int -> a -> String
454 formatRealFloat fmt decs x
456 | isInfinite x = if x < 0 then "-Infinity" else "Infinity"
457 | x < 0 || isNegativeZero x = '-':doFmt fmt (floatToDigits (toInteger base) (-x))
458 | otherwise = doFmt fmt (floatToDigits (toInteger base) x)
462 doFmt format (is, e) =
463 let ds = map intToDigit is in
466 doFmt (if e < 0 || e > 7 then FFExponent else FFFixed)
471 let show_e' = show (e-1) in
474 [d] -> d : ".0e" ++ show_e'
475 (d:ds') -> d : '.' : ds' ++ "e" ++ show_e'
477 let dec' = max dec 1 in
479 [0] -> '0' :'.' : take dec' (repeat '0') ++ "e0"
482 (ei,is') = roundTo base (dec'+1) is
483 (d:ds') = map intToDigit (if ei > 0 then init is' else is')
485 d:'.':ds' ++ 'e':show (e-1+ei)
488 mk0 ls = case ls of { "" -> "0" ; _ -> ls}
492 | e <= 0 -> "0." ++ replicate (-e) '0' ++ ds
495 f 0 s rs = mk0 (reverse s) ++ '.':mk0 rs
496 f n s "" = f (n-1) ('0':s) ""
497 f n s (r:rs) = f (n-1) (r:s) rs
501 let dec' = max dec 0 in
504 (ei,is') = roundTo base (dec' + e) is
505 (ls,rs) = splitAt (e+ei) (map intToDigit is')
507 mk0 ls ++ (if null rs then "" else '.':rs)
510 (ei,is') = roundTo base dec' (replicate (-e) 0 ++ is)
511 d:ds' = map intToDigit (if ei > 0 then is' else 0:is')
513 d : (if null ds' then "" else '.':ds')
516 roundTo :: Int -> Int -> [Int] -> (Int,[Int])
524 f n [] = (0, replicate n 0)
525 f 0 (x:_) = (if x >= b2 then 1 else 0, [])
527 | i' == base = (1,0:ds)
528 | otherwise = (0,i':ds)
533 -- Based on "Printing Floating-Point Numbers Quickly and Accurately"
534 -- by R.G. Burger and R.K. Dybvig in PLDI 96.
535 -- This version uses a much slower logarithm estimator. It should be improved.
537 -- | @floatToDigits@ takes a base and a non-negative RealFloat number,
538 -- and returns a list of digits and an exponent.
539 -- In particular, if x>=0, and
542 -- floatToDigits base x = ([d1,d2,...,dn], e)
549 -- (2) x = 0.d1d2...dn * (base**e)
551 -- (3) 0 <= di <= base-1
553 floatToDigits :: (RealFloat a) => Integer -> a -> ([Int], Int)
554 floatToDigits _ 0 = ([0], 0)
555 floatToDigits base x =
557 (f0, e0) = decodeFloat x
558 (minExp0, _) = floatRange x
561 minExp = minExp0 - p -- the real minimum exponent
562 -- Haskell requires that f be adjusted so denormalized numbers
563 -- will have an impossibly low exponent. Adjust for this.
565 let n = minExp - e0 in
566 if n > 0 then (f0 `div` (b^n), e0+n) else (f0, e0)
571 (f*be*b*2, 2*b, be*b, b)
575 if e > minExp && f == b^(p-1) then
576 (f*b*2, b^(-e+1)*2, b, 1)
578 (f*2, b^(-e)*2, 1, 1)
582 if b == 2 && base == 10 then
583 -- logBase 10 2 is slightly bigger than 3/10 so
584 -- the following will err on the low side. Ignoring
585 -- the fraction will make it err even more.
586 -- Haskell promises that p-1 <= logBase b f < p.
587 (p - 1 + e0) * 3 `div` 10
589 ceiling ((log (fromInteger (f+1)) +
590 fromInteger (int2Integer e) * log (fromInteger b)) /
591 log (fromInteger base))
592 --WAS: fromInt e * log (fromInteger b))
596 if r + mUp <= expt base n * s then n else fixup (n+1)
598 if expt base (-n) * (r + mUp) <= s then n else fixup (n+1)
602 gen ds rn sN mUpN mDnN =
604 (dn, rn') = (rn * base) `divMod` sN
608 case (rn' < mDnN', rn' + mUpN' > sN) of
609 (True, False) -> dn : ds
610 (False, True) -> dn+1 : ds
611 (True, True) -> if rn' * 2 < sN then dn : ds else dn+1 : ds
612 (False, False) -> gen (dn:ds) rn' sN mUpN' mDnN'
616 gen [] r (s * expt base k) mUp mDn
618 let bk = expt base (-k) in
619 gen [] (r * bk) s (mUp * bk) (mDn * bk)
621 (map fromIntegral (reverse rds), k)
626 %*********************************************************
628 \subsection{Converting from a Rational to a RealFloat
630 %*********************************************************
632 [In response to a request for documentation of how fromRational works,
633 Joe Fasel writes:] A quite reasonable request! This code was added to
634 the Prelude just before the 1.2 release, when Lennart, working with an
635 early version of hbi, noticed that (read . show) was not the identity
636 for floating-point numbers. (There was a one-bit error about half the
637 time.) The original version of the conversion function was in fact
638 simply a floating-point divide, as you suggest above. The new version
639 is, I grant you, somewhat denser.
641 Unfortunately, Joe's code doesn't work! Here's an example:
643 main = putStr (shows (1.82173691287639817263897126389712638972163e-300::Double) "\n")
648 1.8217369128763981e-300
653 fromRat :: (RealFloat a) => Rational -> a
657 -- If the exponent of the nearest floating-point number to x
658 -- is e, then the significand is the integer nearest xb^(-e),
659 -- where b is the floating-point radix. We start with a good
660 -- guess for e, and if it is correct, the exponent of the
661 -- floating-point number we construct will again be e. If
662 -- not, one more iteration is needed.
664 f e = if e' == e then y else f e'
665 where y = encodeFloat (round (x * (1 % b)^^e)) e
666 (_,e') = decodeFloat y
669 -- We obtain a trial exponent by doing a floating-point
670 -- division of x's numerator by its denominator. The
671 -- result of this division may not itself be the ultimate
672 -- result, because of an accumulation of three rounding
675 (s,e) = decodeFloat (fromInteger (numerator x) `asTypeOf` x'
676 / fromInteger (denominator x))
679 Now, here's Lennart's code (which works)
682 {-# SPECIALISE fromRat :: Rational -> Double,
683 Rational -> Float #-}
684 fromRat :: (RealFloat a) => Rational -> a
686 -- Deal with special cases first, delegating the real work to fromRat'
687 fromRat (n :% 0) | n > 0 = 1/0 -- +Infinity
688 | n == 0 = 0/0 -- NaN
689 | n < 0 = -1/0 -- -Infinity
691 fromRat (n :% d) | n > 0 = fromRat' (n :% d)
692 | n == 0 = encodeFloat 0 0 -- Zero
693 | n < 0 = - fromRat' ((-n) :% d)
695 -- Conversion process:
696 -- Scale the rational number by the RealFloat base until
697 -- it lies in the range of the mantissa (as used by decodeFloat/encodeFloat).
698 -- Then round the rational to an Integer and encode it with the exponent
699 -- that we got from the scaling.
700 -- To speed up the scaling process we compute the log2 of the number to get
701 -- a first guess of the exponent.
703 fromRat' :: (RealFloat a) => Rational -> a
704 -- Invariant: argument is strictly positive
706 where b = floatRadix r
708 (minExp0, _) = floatRange r
709 minExp = minExp0 - p -- the real minimum exponent
710 xMin = toRational (expt b (p-1))
711 xMax = toRational (expt b p)
712 p0 = (integerLogBase b (numerator x) - integerLogBase b (denominator x) - p) `max` minExp
713 f = if p0 < 0 then 1 % expt b (-p0) else expt b p0 % 1
714 (x', p') = scaleRat (toRational b) minExp xMin xMax p0 (x / f)
715 r = encodeFloat (round x') p'
717 -- Scale x until xMin <= x < xMax, or p (the exponent) <= minExp.
718 scaleRat :: Rational -> Int -> Rational -> Rational -> Int -> Rational -> (Rational, Int)
719 scaleRat b minExp xMin xMax p x
720 | p <= minExp = (x, p)
721 | x >= xMax = scaleRat b minExp xMin xMax (p+1) (x/b)
722 | x < xMin = scaleRat b minExp xMin xMax (p-1) (x*b)
725 -- Exponentiation with a cache for the most common numbers.
726 minExpt, maxExpt :: Int
730 expt :: Integer -> Int -> Integer
732 if base == 2 && n >= minExpt && n <= maxExpt then
737 expts :: Array Int Integer
738 expts = array (minExpt,maxExpt) [(n,2^n) | n <- [minExpt .. maxExpt]]
740 -- Compute the (floor of the) log of i in base b.
741 -- Simplest way would be just divide i by b until it's smaller then b, but that would
742 -- be very slow! We are just slightly more clever.
743 integerLogBase :: Integer -> Integer -> Int
746 | otherwise = doDiv (i `div` (b^l)) l
748 -- Try squaring the base first to cut down the number of divisions.
749 l = 2 * integerLogBase (b*b) i
751 doDiv :: Integer -> Int -> Int
754 | otherwise = doDiv (x `div` b) (y+1)
759 %*********************************************************
761 \subsection{Floating point numeric primops}
763 %*********************************************************
765 Definitions of the boxed PrimOps; these will be
766 used in the case of partial applications, etc.
769 plusFloat, minusFloat, timesFloat, divideFloat :: Float -> Float -> Float
770 plusFloat (F# x) (F# y) = F# (plusFloat# x y)
771 minusFloat (F# x) (F# y) = F# (minusFloat# x y)
772 timesFloat (F# x) (F# y) = F# (timesFloat# x y)
773 divideFloat (F# x) (F# y) = F# (divideFloat# x y)
775 negateFloat :: Float -> Float
776 negateFloat (F# x) = F# (negateFloat# x)
778 gtFloat, geFloat, eqFloat, neFloat, ltFloat, leFloat :: Float -> Float -> Bool
779 gtFloat (F# x) (F# y) = gtFloat# x y
780 geFloat (F# x) (F# y) = geFloat# x y
781 eqFloat (F# x) (F# y) = eqFloat# x y
782 neFloat (F# x) (F# y) = neFloat# x y
783 ltFloat (F# x) (F# y) = ltFloat# x y
784 leFloat (F# x) (F# y) = leFloat# x y
786 float2Int :: Float -> Int
787 float2Int (F# x) = I# (float2Int# x)
789 int2Float :: Int -> Float
790 int2Float (I# x) = F# (int2Float# x)
792 expFloat, logFloat, sqrtFloat :: Float -> Float
793 sinFloat, cosFloat, tanFloat :: Float -> Float
794 asinFloat, acosFloat, atanFloat :: Float -> Float
795 sinhFloat, coshFloat, tanhFloat :: Float -> Float
796 expFloat (F# x) = F# (expFloat# x)
797 logFloat (F# x) = F# (logFloat# x)
798 sqrtFloat (F# x) = F# (sqrtFloat# x)
799 sinFloat (F# x) = F# (sinFloat# x)
800 cosFloat (F# x) = F# (cosFloat# x)
801 tanFloat (F# x) = F# (tanFloat# x)
802 asinFloat (F# x) = F# (asinFloat# x)
803 acosFloat (F# x) = F# (acosFloat# x)
804 atanFloat (F# x) = F# (atanFloat# x)
805 sinhFloat (F# x) = F# (sinhFloat# x)
806 coshFloat (F# x) = F# (coshFloat# x)
807 tanhFloat (F# x) = F# (tanhFloat# x)
809 powerFloat :: Float -> Float -> Float
810 powerFloat (F# x) (F# y) = F# (powerFloat# x y)
812 -- definitions of the boxed PrimOps; these will be
813 -- used in the case of partial applications, etc.
815 plusDouble, minusDouble, timesDouble, divideDouble :: Double -> Double -> Double
816 plusDouble (D# x) (D# y) = D# (x +## y)
817 minusDouble (D# x) (D# y) = D# (x -## y)
818 timesDouble (D# x) (D# y) = D# (x *## y)
819 divideDouble (D# x) (D# y) = D# (x /## y)
821 negateDouble :: Double -> Double
822 negateDouble (D# x) = D# (negateDouble# x)
824 gtDouble, geDouble, eqDouble, neDouble, leDouble, ltDouble :: Double -> Double -> Bool
825 gtDouble (D# x) (D# y) = x >## y
826 geDouble (D# x) (D# y) = x >=## y
827 eqDouble (D# x) (D# y) = x ==## y
828 neDouble (D# x) (D# y) = x /=## y
829 ltDouble (D# x) (D# y) = x <## y
830 leDouble (D# x) (D# y) = x <=## y
832 double2Int :: Double -> Int
833 double2Int (D# x) = I# (double2Int# x)
835 int2Double :: Int -> Double
836 int2Double (I# x) = D# (int2Double# x)
838 double2Float :: Double -> Float
839 double2Float (D# x) = F# (double2Float# x)
841 float2Double :: Float -> Double
842 float2Double (F# x) = D# (float2Double# x)
844 expDouble, logDouble, sqrtDouble :: Double -> Double
845 sinDouble, cosDouble, tanDouble :: Double -> Double
846 asinDouble, acosDouble, atanDouble :: Double -> Double
847 sinhDouble, coshDouble, tanhDouble :: Double -> Double
848 expDouble (D# x) = D# (expDouble# x)
849 logDouble (D# x) = D# (logDouble# x)
850 sqrtDouble (D# x) = D# (sqrtDouble# x)
851 sinDouble (D# x) = D# (sinDouble# x)
852 cosDouble (D# x) = D# (cosDouble# x)
853 tanDouble (D# x) = D# (tanDouble# x)
854 asinDouble (D# x) = D# (asinDouble# x)
855 acosDouble (D# x) = D# (acosDouble# x)
856 atanDouble (D# x) = D# (atanDouble# x)
857 sinhDouble (D# x) = D# (sinhDouble# x)
858 coshDouble (D# x) = D# (coshDouble# x)
859 tanhDouble (D# x) = D# (tanhDouble# x)
861 powerDouble :: Double -> Double -> Double
862 powerDouble (D# x) (D# y) = D# (x **## y)
866 foreign import ccall unsafe "__encodeFloat"
867 encodeFloat# :: Int# -> ByteArray# -> Int -> Float
868 foreign import ccall unsafe "__int_encodeFloat"
869 int_encodeFloat# :: Int# -> Int -> Float
872 foreign import ccall unsafe "isFloatNaN" isFloatNaN :: Float -> Int
873 foreign import ccall unsafe "isFloatInfinite" isFloatInfinite :: Float -> Int
874 foreign import ccall unsafe "isFloatDenormalized" isFloatDenormalized :: Float -> Int
875 foreign import ccall unsafe "isFloatNegativeZero" isFloatNegativeZero :: Float -> Int
878 foreign import ccall unsafe "__encodeDouble"
879 encodeDouble# :: Int# -> ByteArray# -> Int -> Double
880 foreign import ccall unsafe "__int_encodeDouble"
881 int_encodeDouble# :: Int# -> Int -> Double
883 foreign import ccall unsafe "isDoubleNaN" isDoubleNaN :: Double -> Int
884 foreign import ccall unsafe "isDoubleInfinite" isDoubleInfinite :: Double -> Int
885 foreign import ccall unsafe "isDoubleDenormalized" isDoubleDenormalized :: Double -> Int
886 foreign import ccall unsafe "isDoubleNegativeZero" isDoubleNegativeZero :: Double -> Int
889 %*********************************************************
891 \subsection{Coercion rules}
893 %*********************************************************
897 "fromIntegral/Int->Float" fromIntegral = int2Float
898 "fromIntegral/Int->Double" fromIntegral = int2Double
899 "realToFrac/Float->Float" realToFrac = id :: Float -> Float
900 "realToFrac/Float->Double" realToFrac = float2Double
901 "realToFrac/Double->Float" realToFrac = double2Float
902 "realToFrac/Double->Double" realToFrac = id :: Double -> Double