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 data Float = F# Float#
102 data Double = D# Double#
104 instance CCallable Float
105 instance CReturnable Float
107 instance CCallable Double
108 instance CReturnable Double
112 %*********************************************************
114 \subsection{Type @Float@}
116 %*********************************************************
119 instance Eq Float where
120 (F# x) == (F# y) = x `eqFloat#` y
122 instance Ord Float where
123 (F# x) `compare` (F# y) | x `ltFloat#` y = LT
124 | x `eqFloat#` y = EQ
127 (F# x) < (F# y) = x `ltFloat#` y
128 (F# x) <= (F# y) = x `leFloat#` y
129 (F# x) >= (F# y) = x `geFloat#` y
130 (F# x) > (F# y) = x `gtFloat#` y
132 instance Num Float where
133 (+) x y = plusFloat x y
134 (-) x y = minusFloat x y
135 negate x = negateFloat x
136 (*) x y = timesFloat x y
138 | otherwise = negateFloat x
139 signum x | x == 0.0 = 0
141 | otherwise = negate 1
143 {-# INLINE fromInteger #-}
144 fromInteger n = encodeFloat n 0
145 -- It's important that encodeFloat inlines here, and that
146 -- fromInteger in turn inlines,
147 -- so that if fromInteger is applied to an (S# i) the right thing happens
149 instance Real Float where
150 toRational x = (m%1)*(b%1)^^n
151 where (m,n) = decodeFloat x
154 instance Fractional Float where
155 (/) x y = divideFloat x y
156 fromRational x = fromRat x
159 {-# RULES "truncate/Float->Int" truncate = float2Int #-}
160 instance RealFrac Float where
162 {-# SPECIALIZE properFraction :: Float -> (Int, Float) #-}
163 {-# SPECIALIZE round :: Float -> Int #-}
164 {-# SPECIALIZE ceiling :: Float -> Int #-}
165 {-# SPECIALIZE floor :: Float -> Int #-}
167 {-# SPECIALIZE properFraction :: Float -> (Integer, Float) #-}
168 {-# SPECIALIZE truncate :: Float -> Integer #-}
169 {-# SPECIALIZE round :: Float -> Integer #-}
170 {-# SPECIALIZE ceiling :: Float -> Integer #-}
171 {-# SPECIALIZE floor :: Float -> Integer #-}
174 = case (decodeFloat x) of { (m,n) ->
175 let b = floatRadix x in
177 (fromInteger m * fromInteger b ^ n, 0.0)
179 case (quotRem m (b^(negate n))) of { (w,r) ->
180 (fromInteger w, encodeFloat r n)
184 truncate x = case properFraction x of
187 round x = case properFraction x of
189 m = if r < 0.0 then n - 1 else n + 1
190 half_down = abs r - 0.5
192 case (compare half_down 0.0) of
194 EQ -> if even n then n else m
197 ceiling x = case properFraction x of
198 (n,r) -> if r > 0.0 then n + 1 else n
200 floor x = case properFraction x of
201 (n,r) -> if r < 0.0 then n - 1 else n
203 instance Floating Float where
204 pi = 3.141592653589793238
217 (**) x y = powerFloat x y
218 logBase x y = log y / log x
220 asinh x = log (x + sqrt (1.0+x*x))
221 acosh x = log (x + (x+1.0) * sqrt ((x-1.0)/(x+1.0)))
222 atanh x = log ((x+1.0) / sqrt (1.0-x*x))
224 instance RealFloat Float where
225 floatRadix _ = FLT_RADIX -- from float.h
226 floatDigits _ = FLT_MANT_DIG -- ditto
227 floatRange _ = (FLT_MIN_EXP, FLT_MAX_EXP) -- ditto
230 = case decodeFloat# f# of
231 (# exp#, s#, d# #) -> (J# s# d#, I# exp#)
233 encodeFloat (S# i) j = int_encodeFloat# i j
234 encodeFloat (J# s# d#) e = encodeFloat# s# d# e
236 exponent x = case decodeFloat x of
237 (m,n) -> if m == 0 then 0 else n + floatDigits x
239 significand x = case decodeFloat x of
240 (m,_) -> encodeFloat m (negate (floatDigits x))
242 scaleFloat k x = case decodeFloat x of
243 (m,n) -> encodeFloat m (n+k)
244 isNaN x = 0 /= isFloatNaN x
245 isInfinite x = 0 /= isFloatInfinite x
246 isDenormalized x = 0 /= isFloatDenormalized x
247 isNegativeZero x = 0 /= isFloatNegativeZero x
250 instance Show Float where
251 showsPrec x = showSigned showFloat x
252 showList = showList__ (showsPrec 0)
255 %*********************************************************
257 \subsection{Type @Double@}
259 %*********************************************************
262 instance Eq Double where
263 (D# x) == (D# y) = x ==## y
265 instance Ord Double where
266 (D# x) `compare` (D# y) | x <## y = LT
270 (D# x) < (D# y) = x <## y
271 (D# x) <= (D# y) = x <=## y
272 (D# x) >= (D# y) = x >=## y
273 (D# x) > (D# y) = x >## y
275 instance Num Double where
276 (+) x y = plusDouble x y
277 (-) x y = minusDouble x y
278 negate x = negateDouble x
279 (*) x y = timesDouble x y
281 | otherwise = negateDouble x
282 signum x | x == 0.0 = 0
284 | otherwise = negate 1
286 {-# INLINE fromInteger #-}
287 -- See comments with Num Float
288 fromInteger (S# i#) = case (int2Double# i#) of { d# -> D# d# }
289 fromInteger (J# s# d#) = encodeDouble# s# d# 0
292 instance Real Double where
293 toRational x = (m%1)*(b%1)^^n
294 where (m,n) = decodeFloat x
297 instance Fractional Double where
298 (/) x y = divideDouble x y
299 fromRational x = fromRat x
302 instance Floating Double where
303 pi = 3.141592653589793238
306 sqrt x = sqrtDouble x
310 asin x = asinDouble x
311 acos x = acosDouble x
312 atan x = atanDouble x
313 sinh x = sinhDouble x
314 cosh x = coshDouble x
315 tanh x = tanhDouble x
316 (**) x y = powerDouble x y
317 logBase x y = log y / log x
319 asinh x = log (x + sqrt (1.0+x*x))
320 acosh x = log (x + (x+1.0) * sqrt ((x-1.0)/(x+1.0)))
321 atanh x = log ((x+1.0) / sqrt (1.0-x*x))
323 {-# RULES "truncate/Double->Int" truncate = double2Int #-}
324 instance RealFrac Double where
326 {-# SPECIALIZE properFraction :: Double -> (Int, Double) #-}
327 {-# SPECIALIZE round :: Double -> Int #-}
328 {-# SPECIALIZE ceiling :: Double -> Int #-}
329 {-# SPECIALIZE floor :: Double -> Int #-}
331 {-# SPECIALIZE properFraction :: Double -> (Integer, Double) #-}
332 {-# SPECIALIZE truncate :: Double -> Integer #-}
333 {-# SPECIALIZE round :: Double -> Integer #-}
334 {-# SPECIALIZE ceiling :: Double -> Integer #-}
335 {-# SPECIALIZE floor :: Double -> Integer #-}
338 = case (decodeFloat x) of { (m,n) ->
339 let b = floatRadix x in
341 (fromInteger m * fromInteger b ^ n, 0.0)
343 case (quotRem m (b^(negate n))) of { (w,r) ->
344 (fromInteger w, encodeFloat r n)
348 truncate x = case properFraction x of
351 round x = case properFraction x of
353 m = if r < 0.0 then n - 1 else n + 1
354 half_down = abs r - 0.5
356 case (compare half_down 0.0) of
358 EQ -> if even n then n else m
361 ceiling x = case properFraction x of
362 (n,r) -> if r > 0.0 then n + 1 else n
364 floor x = case properFraction x of
365 (n,r) -> if r < 0.0 then n - 1 else n
367 instance RealFloat Double where
368 floatRadix _ = FLT_RADIX -- from float.h
369 floatDigits _ = DBL_MANT_DIG -- ditto
370 floatRange _ = (DBL_MIN_EXP, DBL_MAX_EXP) -- ditto
373 = case decodeDouble# x# of
374 (# exp#, s#, d# #) -> (J# s# d#, I# exp#)
376 encodeFloat (S# i) j = int_encodeDouble# i j
377 encodeFloat (J# s# d#) e = encodeDouble# s# d# e
379 exponent x = case decodeFloat x of
380 (m,n) -> if m == 0 then 0 else n + floatDigits x
382 significand x = case decodeFloat x of
383 (m,_) -> encodeFloat m (negate (floatDigits x))
385 scaleFloat k x = case decodeFloat x of
386 (m,n) -> encodeFloat m (n+k)
388 isNaN x = 0 /= isDoubleNaN x
389 isInfinite x = 0 /= isDoubleInfinite x
390 isDenormalized x = 0 /= isDoubleDenormalized x
391 isNegativeZero x = 0 /= isDoubleNegativeZero x
394 instance Show Double where
395 showsPrec x = showSigned showFloat x
396 showList = showList__ (showsPrec 0)
399 %*********************************************************
401 \subsection{@Enum@ instances}
403 %*********************************************************
405 The @Enum@ instances for Floats and Doubles are slightly unusual.
406 The @toEnum@ function truncates numbers to Int. The definitions
407 of @enumFrom@ and @enumFromThen@ allow floats to be used in arithmetic
408 series: [0,0.1 .. 1.0]. However, roundoff errors make these somewhat
409 dubious. This example may have either 10 or 11 elements, depending on
410 how 0.1 is represented.
412 NOTE: The instances for Float and Double do not make use of the default
413 methods for @enumFromTo@ and @enumFromThenTo@, as these rely on there being
414 a `non-lossy' conversion to and from Ints. Instead we make use of the
415 1.2 default methods (back in the days when Enum had Ord as a superclass)
416 for these (@numericEnumFromTo@ and @numericEnumFromThenTo@ below.)
419 instance Enum Float where
423 fromEnum = fromInteger . truncate -- may overflow
424 enumFrom = numericEnumFrom
425 enumFromTo = numericEnumFromTo
426 enumFromThen = numericEnumFromThen
427 enumFromThenTo = numericEnumFromThenTo
429 instance Enum Double where
433 fromEnum = fromInteger . truncate -- may overflow
434 enumFrom = numericEnumFrom
435 enumFromTo = numericEnumFromTo
436 enumFromThen = numericEnumFromThen
437 enumFromThenTo = numericEnumFromThenTo
441 %*********************************************************
443 \subsection{Printing floating point}
445 %*********************************************************
449 showFloat :: (RealFloat a) => a -> ShowS
450 showFloat x = showString (formatRealFloat FFGeneric Nothing x)
452 -- These are the format types. This type is not exported.
454 data FFFormat = FFExponent | FFFixed | FFGeneric
456 formatRealFloat :: (RealFloat a) => FFFormat -> Maybe Int -> a -> String
457 formatRealFloat fmt decs x
459 | isInfinite x = if x < 0 then "-Infinity" else "Infinity"
460 | x < 0 || isNegativeZero x = '-':doFmt fmt (floatToDigits (toInteger base) (-x))
461 | otherwise = doFmt fmt (floatToDigits (toInteger base) x)
465 doFmt format (is, e) =
466 let ds = map intToDigit is in
469 doFmt (if e < 0 || e > 7 then FFExponent else FFFixed)
474 let show_e' = show (e-1) in
477 [d] -> d : ".0e" ++ show_e'
478 (d:ds') -> d : '.' : ds' ++ "e" ++ show_e'
480 let dec' = max dec 1 in
482 [0] -> '0' :'.' : take dec' (repeat '0') ++ "e0"
485 (ei,is') = roundTo base (dec'+1) is
486 (d:ds') = map intToDigit (if ei > 0 then init is' else is')
488 d:'.':ds' ++ 'e':show (e-1+ei)
491 mk0 ls = case ls of { "" -> "0" ; _ -> ls}
495 | e <= 0 -> "0." ++ replicate (-e) '0' ++ ds
498 f 0 s rs = mk0 (reverse s) ++ '.':mk0 rs
499 f n s "" = f (n-1) ('0':s) ""
500 f n s (r:rs) = f (n-1) (r:s) rs
504 let dec' = max dec 0 in
507 (ei,is') = roundTo base (dec' + e) is
508 (ls,rs) = splitAt (e+ei) (map intToDigit is')
510 mk0 ls ++ (if null rs then "" else '.':rs)
513 (ei,is') = roundTo base dec' (replicate (-e) 0 ++ is)
514 d:ds' = map intToDigit (if ei > 0 then is' else 0:is')
516 d : (if null ds' then "" else '.':ds')
519 roundTo :: Int -> Int -> [Int] -> (Int,[Int])
527 f n [] = (0, replicate n 0)
528 f 0 (x:_) = (if x >= b2 then 1 else 0, [])
530 | i' == base = (1,0:ds)
531 | otherwise = (0,i':ds)
536 -- Based on "Printing Floating-Point Numbers Quickly and Accurately"
537 -- by R.G. Burger and R.K. Dybvig in PLDI 96.
538 -- This version uses a much slower logarithm estimator. It should be improved.
540 -- floatToDigits takes a base and a non-negative RealFloat number,
541 -- and returns a list of digits and an exponent.
542 -- In particular, if x>=0, and
543 -- floatToDigits base x = ([d1,d2,...,dn], e)
546 -- (b) x = 0.d1d2...dn * (base**e)
547 -- (c) 0 <= di <= base-1
549 floatToDigits :: (RealFloat a) => Integer -> a -> ([Int], Int)
550 floatToDigits _ 0 = ([0], 0)
551 floatToDigits base x =
553 (f0, e0) = decodeFloat x
554 (minExp0, _) = floatRange x
557 minExp = minExp0 - p -- the real minimum exponent
558 -- Haskell requires that f be adjusted so denormalized numbers
559 -- will have an impossibly low exponent. Adjust for this.
561 let n = minExp - e0 in
562 if n > 0 then (f0 `div` (b^n), e0+n) else (f0, e0)
567 (f*be*b*2, 2*b, be*b, b)
571 if e > minExp && f == b^(p-1) then
572 (f*b*2, b^(-e+1)*2, b, 1)
574 (f*2, b^(-e)*2, 1, 1)
578 if b == 2 && base == 10 then
579 -- logBase 10 2 is slightly bigger than 3/10 so
580 -- the following will err on the low side. Ignoring
581 -- the fraction will make it err even more.
582 -- Haskell promises that p-1 <= logBase b f < p.
583 (p - 1 + e0) * 3 `div` 10
585 ceiling ((log (fromInteger (f+1)) +
586 fromInteger (int2Integer e) * log (fromInteger b)) /
587 log (fromInteger base))
588 --WAS: fromInt e * log (fromInteger b))
592 if r + mUp <= expt base n * s then n else fixup (n+1)
594 if expt base (-n) * (r + mUp) <= s then n else fixup (n+1)
598 gen ds rn sN mUpN mDnN =
600 (dn, rn') = (rn * base) `divMod` sN
604 case (rn' < mDnN', rn' + mUpN' > sN) of
605 (True, False) -> dn : ds
606 (False, True) -> dn+1 : ds
607 (True, True) -> if rn' * 2 < sN then dn : ds else dn+1 : ds
608 (False, False) -> gen (dn:ds) rn' sN mUpN' mDnN'
612 gen [] r (s * expt base k) mUp mDn
614 let bk = expt base (-k) in
615 gen [] (r * bk) s (mUp * bk) (mDn * bk)
617 (map fromIntegral (reverse rds), k)
622 %*********************************************************
624 \subsection{Converting from a Rational to a RealFloat
626 %*********************************************************
628 [In response to a request for documentation of how fromRational works,
629 Joe Fasel writes:] A quite reasonable request! This code was added to
630 the Prelude just before the 1.2 release, when Lennart, working with an
631 early version of hbi, noticed that (read . show) was not the identity
632 for floating-point numbers. (There was a one-bit error about half the
633 time.) The original version of the conversion function was in fact
634 simply a floating-point divide, as you suggest above. The new version
635 is, I grant you, somewhat denser.
637 Unfortunately, Joe's code doesn't work! Here's an example:
639 main = putStr (shows (1.82173691287639817263897126389712638972163e-300::Double) "\n")
644 1.8217369128763981e-300
649 fromRat :: (RealFloat a) => Rational -> a
653 -- If the exponent of the nearest floating-point number to x
654 -- is e, then the significand is the integer nearest xb^(-e),
655 -- where b is the floating-point radix. We start with a good
656 -- guess for e, and if it is correct, the exponent of the
657 -- floating-point number we construct will again be e. If
658 -- not, one more iteration is needed.
660 f e = if e' == e then y else f e'
661 where y = encodeFloat (round (x * (1 % b)^^e)) e
662 (_,e') = decodeFloat y
665 -- We obtain a trial exponent by doing a floating-point
666 -- division of x's numerator by its denominator. The
667 -- result of this division may not itself be the ultimate
668 -- result, because of an accumulation of three rounding
671 (s,e) = decodeFloat (fromInteger (numerator x) `asTypeOf` x'
672 / fromInteger (denominator x))
675 Now, here's Lennart's code (which works)
678 {-# SPECIALISE fromRat ::
680 Rational -> Float #-}
681 fromRat :: (RealFloat a) => Rational -> a
683 | x == 0 = encodeFloat 0 0 -- Handle exceptional cases
684 | x < 0 = - fromRat' (-x) -- first.
685 | otherwise = fromRat' x
687 -- Conversion process:
688 -- Scale the rational number by the RealFloat base until
689 -- it lies in the range of the mantissa (as used by decodeFloat/encodeFloat).
690 -- Then round the rational to an Integer and encode it with the exponent
691 -- that we got from the scaling.
692 -- To speed up the scaling process we compute the log2 of the number to get
693 -- a first guess of the exponent.
695 fromRat' :: (RealFloat a) => Rational -> a
697 where b = floatRadix r
699 (minExp0, _) = floatRange r
700 minExp = minExp0 - p -- the real minimum exponent
701 xMin = toRational (expt b (p-1))
702 xMax = toRational (expt b p)
703 p0 = (integerLogBase b (numerator x) - integerLogBase b (denominator x) - p) `max` minExp
704 f = if p0 < 0 then 1 % expt b (-p0) else expt b p0 % 1
705 (x', p') = scaleRat (toRational b) minExp xMin xMax p0 (x / f)
706 r = encodeFloat (round x') p'
708 -- Scale x until xMin <= x < xMax, or p (the exponent) <= minExp.
709 scaleRat :: Rational -> Int -> Rational -> Rational -> Int -> Rational -> (Rational, Int)
710 scaleRat b minExp xMin xMax p x
711 | p <= minExp = (x, p)
712 | x >= xMax = scaleRat b minExp xMin xMax (p+1) (x/b)
713 | x < xMin = scaleRat b minExp xMin xMax (p-1) (x*b)
716 -- Exponentiation with a cache for the most common numbers.
717 minExpt, maxExpt :: Int
721 expt :: Integer -> Int -> Integer
723 if base == 2 && n >= minExpt && n <= maxExpt then
728 expts :: Array Int Integer
729 expts = array (minExpt,maxExpt) [(n,2^n) | n <- [minExpt .. maxExpt]]
731 -- Compute the (floor of the) log of i in base b.
732 -- Simplest way would be just divide i by b until it's smaller then b, but that would
733 -- be very slow! We are just slightly more clever.
734 integerLogBase :: Integer -> Integer -> Int
737 | otherwise = doDiv (i `div` (b^l)) l
739 -- Try squaring the base first to cut down the number of divisions.
740 l = 2 * integerLogBase (b*b) i
742 doDiv :: Integer -> Int -> Int
745 | otherwise = doDiv (x `div` b) (y+1)
750 %*********************************************************
752 \subsection{Floating point numeric primops}
754 %*********************************************************
756 Definitions of the boxed PrimOps; these will be
757 used in the case of partial applications, etc.
760 plusFloat, minusFloat, timesFloat, divideFloat :: Float -> Float -> Float
761 plusFloat (F# x) (F# y) = F# (plusFloat# x y)
762 minusFloat (F# x) (F# y) = F# (minusFloat# x y)
763 timesFloat (F# x) (F# y) = F# (timesFloat# x y)
764 divideFloat (F# x) (F# y) = F# (divideFloat# x y)
766 negateFloat :: Float -> Float
767 negateFloat (F# x) = F# (negateFloat# x)
769 gtFloat, geFloat, eqFloat, neFloat, ltFloat, leFloat :: Float -> Float -> Bool
770 gtFloat (F# x) (F# y) = gtFloat# x y
771 geFloat (F# x) (F# y) = geFloat# x y
772 eqFloat (F# x) (F# y) = eqFloat# x y
773 neFloat (F# x) (F# y) = neFloat# x y
774 ltFloat (F# x) (F# y) = ltFloat# x y
775 leFloat (F# x) (F# y) = leFloat# x y
777 float2Int :: Float -> Int
778 float2Int (F# x) = I# (float2Int# x)
780 int2Float :: Int -> Float
781 int2Float (I# x) = F# (int2Float# x)
783 expFloat, logFloat, sqrtFloat :: Float -> Float
784 sinFloat, cosFloat, tanFloat :: Float -> Float
785 asinFloat, acosFloat, atanFloat :: Float -> Float
786 sinhFloat, coshFloat, tanhFloat :: Float -> Float
787 expFloat (F# x) = F# (expFloat# x)
788 logFloat (F# x) = F# (logFloat# x)
789 sqrtFloat (F# x) = F# (sqrtFloat# x)
790 sinFloat (F# x) = F# (sinFloat# x)
791 cosFloat (F# x) = F# (cosFloat# x)
792 tanFloat (F# x) = F# (tanFloat# x)
793 asinFloat (F# x) = F# (asinFloat# x)
794 acosFloat (F# x) = F# (acosFloat# x)
795 atanFloat (F# x) = F# (atanFloat# x)
796 sinhFloat (F# x) = F# (sinhFloat# x)
797 coshFloat (F# x) = F# (coshFloat# x)
798 tanhFloat (F# x) = F# (tanhFloat# x)
800 powerFloat :: Float -> Float -> Float
801 powerFloat (F# x) (F# y) = F# (powerFloat# x y)
803 -- definitions of the boxed PrimOps; these will be
804 -- used in the case of partial applications, etc.
806 plusDouble, minusDouble, timesDouble, divideDouble :: Double -> Double -> Double
807 plusDouble (D# x) (D# y) = D# (x +## y)
808 minusDouble (D# x) (D# y) = D# (x -## y)
809 timesDouble (D# x) (D# y) = D# (x *## y)
810 divideDouble (D# x) (D# y) = D# (x /## y)
812 negateDouble :: Double -> Double
813 negateDouble (D# x) = D# (negateDouble# x)
815 gtDouble, geDouble, eqDouble, neDouble, leDouble, ltDouble :: Double -> Double -> Bool
816 gtDouble (D# x) (D# y) = x >## y
817 geDouble (D# x) (D# y) = x >=## y
818 eqDouble (D# x) (D# y) = x ==## y
819 neDouble (D# x) (D# y) = x /=## y
820 ltDouble (D# x) (D# y) = x <## y
821 leDouble (D# x) (D# y) = x <=## y
823 double2Int :: Double -> Int
824 double2Int (D# x) = I# (double2Int# x)
826 int2Double :: Int -> Double
827 int2Double (I# x) = D# (int2Double# x)
829 double2Float :: Double -> Float
830 double2Float (D# x) = F# (double2Float# x)
832 float2Double :: Float -> Double
833 float2Double (F# x) = D# (float2Double# x)
835 expDouble, logDouble, sqrtDouble :: Double -> Double
836 sinDouble, cosDouble, tanDouble :: Double -> Double
837 asinDouble, acosDouble, atanDouble :: Double -> Double
838 sinhDouble, coshDouble, tanhDouble :: Double -> Double
839 expDouble (D# x) = D# (expDouble# x)
840 logDouble (D# x) = D# (logDouble# x)
841 sqrtDouble (D# x) = D# (sqrtDouble# x)
842 sinDouble (D# x) = D# (sinDouble# x)
843 cosDouble (D# x) = D# (cosDouble# x)
844 tanDouble (D# x) = D# (tanDouble# x)
845 asinDouble (D# x) = D# (asinDouble# x)
846 acosDouble (D# x) = D# (acosDouble# x)
847 atanDouble (D# x) = D# (atanDouble# x)
848 sinhDouble (D# x) = D# (sinhDouble# x)
849 coshDouble (D# x) = D# (coshDouble# x)
850 tanhDouble (D# x) = D# (tanhDouble# x)
852 powerDouble :: Double -> Double -> Double
853 powerDouble (D# x) (D# y) = D# (x **## y)
857 foreign import ccall unsafe "__encodeFloat"
858 encodeFloat# :: Int# -> ByteArray# -> Int -> Float
859 foreign import ccall unsafe "__int_encodeFloat"
860 int_encodeFloat# :: Int# -> Int -> Float
863 foreign import ccall unsafe "isFloatNaN" isFloatNaN :: Float -> Int
864 foreign import ccall unsafe "isFloatInfinite" isFloatInfinite :: Float -> Int
865 foreign import ccall unsafe "isFloatDenormalized" isFloatDenormalized :: Float -> Int
866 foreign import ccall unsafe "isFloatNegativeZero" isFloatNegativeZero :: Float -> Int
869 foreign import ccall unsafe "__encodeDouble"
870 encodeDouble# :: Int# -> ByteArray# -> Int -> Double
871 foreign import ccall unsafe "__int_encodeDouble"
872 int_encodeDouble# :: Int# -> Int -> Double
874 foreign import ccall unsafe "isDoubleNaN" isDoubleNaN :: Double -> Int
875 foreign import ccall unsafe "isDoubleInfinite" isDoubleInfinite :: Double -> Int
876 foreign import ccall unsafe "isDoubleDenormalized" isDoubleDenormalized :: Double -> Int
877 foreign import ccall unsafe "isDoubleNegativeZero" isDoubleNegativeZero :: Double -> Int
880 %*********************************************************
882 \subsection{Coercion rules}
884 %*********************************************************
888 "fromIntegral/Int->Float" fromIntegral = int2Float
889 "fromIntegral/Int->Double" fromIntegral = int2Double
890 "realToFrac/Float->Float" realToFrac = id :: Float -> Float
891 "realToFrac/Float->Double" realToFrac = float2Double
892 "realToFrac/Double->Float" realToFrac = double2Float
893 "realToFrac/Double->Double" realToFrac = id :: Double -> Double