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#
107 instance CCallable Float
108 instance CReturnable Float
110 instance CCallable Double
111 instance CReturnable Double
115 %*********************************************************
117 \subsection{Type @Float@}
119 %*********************************************************
122 instance Eq Float where
123 (F# x) == (F# y) = x `eqFloat#` y
125 instance Ord Float where
126 (F# x) `compare` (F# y) | x `ltFloat#` y = LT
127 | x `eqFloat#` y = EQ
130 (F# x) < (F# y) = x `ltFloat#` y
131 (F# x) <= (F# y) = x `leFloat#` y
132 (F# x) >= (F# y) = x `geFloat#` y
133 (F# x) > (F# y) = x `gtFloat#` y
135 instance Num Float where
136 (+) x y = plusFloat x y
137 (-) x y = minusFloat x y
138 negate x = negateFloat x
139 (*) x y = timesFloat x y
141 | otherwise = negateFloat x
142 signum x | x == 0.0 = 0
144 | otherwise = negate 1
146 {-# INLINE fromInteger #-}
147 fromInteger n = encodeFloat n 0
148 -- It's important that encodeFloat inlines here, and that
149 -- fromInteger in turn inlines,
150 -- so that if fromInteger is applied to an (S# i) the right thing happens
152 instance Real Float where
153 toRational x = (m%1)*(b%1)^^n
154 where (m,n) = decodeFloat x
157 instance Fractional Float where
158 (/) x y = divideFloat x y
159 fromRational x = fromRat x
162 {-# RULES "truncate/Float->Int" truncate = float2Int #-}
163 instance RealFrac Float where
165 {-# SPECIALIZE properFraction :: Float -> (Int, Float) #-}
166 {-# SPECIALIZE round :: Float -> Int #-}
168 {-# SPECIALIZE properFraction :: Float -> (Integer, Float) #-}
169 {-# SPECIALIZE round :: Float -> Integer #-}
171 -- ceiling, floor, and truncate are all small
172 {-# INLINE ceiling #-}
174 {-# INLINE truncate #-}
177 = case (decodeFloat x) of { (m,n) ->
178 let b = floatRadix x in
180 (fromInteger m * fromInteger b ^ n, 0.0)
182 case (quotRem m (b^(negate n))) of { (w,r) ->
183 (fromInteger w, encodeFloat r n)
187 truncate x = case properFraction x of
190 round x = case properFraction x of
192 m = if r < 0.0 then n - 1 else n + 1
193 half_down = abs r - 0.5
195 case (compare half_down 0.0) of
197 EQ -> if even n then n else m
200 ceiling x = case properFraction x of
201 (n,r) -> if r > 0.0 then n + 1 else n
203 floor x = case properFraction x of
204 (n,r) -> if r < 0.0 then n - 1 else n
206 instance Floating Float where
207 pi = 3.141592653589793238
220 (**) x y = powerFloat x y
221 logBase x y = log y / log x
223 asinh x = log (x + sqrt (1.0+x*x))
224 acosh x = log (x + (x+1.0) * sqrt ((x-1.0)/(x+1.0)))
225 atanh x = log ((x+1.0) / sqrt (1.0-x*x))
227 instance RealFloat Float where
228 floatRadix _ = FLT_RADIX -- from float.h
229 floatDigits _ = FLT_MANT_DIG -- ditto
230 floatRange _ = (FLT_MIN_EXP, FLT_MAX_EXP) -- ditto
233 = case decodeFloat# f# of
234 (# exp#, s#, d# #) -> (J# s# d#, I# exp#)
236 encodeFloat (S# i) j = int_encodeFloat# i j
237 encodeFloat (J# s# d#) e = encodeFloat# s# d# e
239 exponent x = case decodeFloat x of
240 (m,n) -> if m == 0 then 0 else n + floatDigits x
242 significand x = case decodeFloat x of
243 (m,_) -> encodeFloat m (negate (floatDigits x))
245 scaleFloat k x = case decodeFloat x of
246 (m,n) -> encodeFloat m (n+k)
247 isNaN x = 0 /= isFloatNaN x
248 isInfinite x = 0 /= isFloatInfinite x
249 isDenormalized x = 0 /= isFloatDenormalized x
250 isNegativeZero x = 0 /= isFloatNegativeZero x
253 instance Show Float where
254 showsPrec x = showSigned showFloat x
255 showList = showList__ (showsPrec 0)
258 %*********************************************************
260 \subsection{Type @Double@}
262 %*********************************************************
265 instance Eq Double where
266 (D# x) == (D# y) = x ==## y
268 instance Ord Double where
269 (D# x) `compare` (D# y) | x <## y = LT
273 (D# x) < (D# y) = x <## y
274 (D# x) <= (D# y) = x <=## y
275 (D# x) >= (D# y) = x >=## y
276 (D# x) > (D# y) = x >## y
278 instance Num Double where
279 (+) x y = plusDouble x y
280 (-) x y = minusDouble x y
281 negate x = negateDouble x
282 (*) x y = timesDouble x y
284 | otherwise = negateDouble x
285 signum x | x == 0.0 = 0
287 | otherwise = negate 1
289 {-# INLINE fromInteger #-}
290 -- See comments with Num Float
291 fromInteger (S# i#) = case (int2Double# i#) of { d# -> D# d# }
292 fromInteger (J# s# d#) = encodeDouble# s# d# 0
295 instance Real Double where
296 toRational x = (m%1)*(b%1)^^n
297 where (m,n) = decodeFloat x
300 instance Fractional Double where
301 (/) x y = divideDouble x y
302 fromRational x = fromRat x
305 instance Floating Double where
306 pi = 3.141592653589793238
309 sqrt x = sqrtDouble x
313 asin x = asinDouble x
314 acos x = acosDouble x
315 atan x = atanDouble x
316 sinh x = sinhDouble x
317 cosh x = coshDouble x
318 tanh x = tanhDouble x
319 (**) x y = powerDouble x y
320 logBase x y = log y / log x
322 asinh x = log (x + sqrt (1.0+x*x))
323 acosh x = log (x + (x+1.0) * sqrt ((x-1.0)/(x+1.0)))
324 atanh x = log ((x+1.0) / sqrt (1.0-x*x))
326 {-# RULES "truncate/Double->Int" truncate = double2Int #-}
327 instance RealFrac Double where
329 {-# SPECIALIZE properFraction :: Double -> (Int, Double) #-}
330 {-# SPECIALIZE round :: Double -> Int #-}
332 {-# SPECIALIZE properFraction :: Double -> (Integer, Double) #-}
333 {-# SPECIALIZE round :: Double -> Integer #-}
335 -- ceiling, floor, and truncate are all small
336 {-# INLINE ceiling #-}
338 {-# INLINE truncate #-}
341 = case (decodeFloat x) of { (m,n) ->
342 let b = floatRadix x in
344 (fromInteger m * fromInteger b ^ n, 0.0)
346 case (quotRem m (b^(negate n))) of { (w,r) ->
347 (fromInteger w, encodeFloat r n)
351 truncate x = case properFraction x of
354 round x = case properFraction x of
356 m = if r < 0.0 then n - 1 else n + 1
357 half_down = abs r - 0.5
359 case (compare half_down 0.0) of
361 EQ -> if even n then n else m
364 ceiling x = case properFraction x of
365 (n,r) -> if r > 0.0 then n + 1 else n
367 floor x = case properFraction x of
368 (n,r) -> if r < 0.0 then n - 1 else n
370 instance RealFloat Double where
371 floatRadix _ = FLT_RADIX -- from float.h
372 floatDigits _ = DBL_MANT_DIG -- ditto
373 floatRange _ = (DBL_MIN_EXP, DBL_MAX_EXP) -- ditto
376 = case decodeDouble# x# of
377 (# exp#, s#, d# #) -> (J# s# d#, I# exp#)
379 encodeFloat (S# i) j = int_encodeDouble# i j
380 encodeFloat (J# s# d#) e = encodeDouble# s# d# e
382 exponent x = case decodeFloat x of
383 (m,n) -> if m == 0 then 0 else n + floatDigits x
385 significand x = case decodeFloat x of
386 (m,_) -> encodeFloat m (negate (floatDigits x))
388 scaleFloat k x = case decodeFloat x of
389 (m,n) -> encodeFloat m (n+k)
391 isNaN x = 0 /= isDoubleNaN x
392 isInfinite x = 0 /= isDoubleInfinite x
393 isDenormalized x = 0 /= isDoubleDenormalized x
394 isNegativeZero x = 0 /= isDoubleNegativeZero x
397 instance Show Double where
398 showsPrec x = showSigned showFloat x
399 showList = showList__ (showsPrec 0)
402 %*********************************************************
404 \subsection{@Enum@ instances}
406 %*********************************************************
408 The @Enum@ instances for Floats and Doubles are slightly unusual.
409 The @toEnum@ function truncates numbers to Int. The definitions
410 of @enumFrom@ and @enumFromThen@ allow floats to be used in arithmetic
411 series: [0,0.1 .. 1.0]. However, roundoff errors make these somewhat
412 dubious. This example may have either 10 or 11 elements, depending on
413 how 0.1 is represented.
415 NOTE: The instances for Float and Double do not make use of the default
416 methods for @enumFromTo@ and @enumFromThenTo@, as these rely on there being
417 a `non-lossy' conversion to and from Ints. Instead we make use of the
418 1.2 default methods (back in the days when Enum had Ord as a superclass)
419 for these (@numericEnumFromTo@ and @numericEnumFromThenTo@ below.)
422 instance Enum Float where
426 fromEnum = fromInteger . truncate -- may overflow
427 enumFrom = numericEnumFrom
428 enumFromTo = numericEnumFromTo
429 enumFromThen = numericEnumFromThen
430 enumFromThenTo = numericEnumFromThenTo
432 instance Enum Double where
436 fromEnum = fromInteger . truncate -- may overflow
437 enumFrom = numericEnumFrom
438 enumFromTo = numericEnumFromTo
439 enumFromThen = numericEnumFromThen
440 enumFromThenTo = numericEnumFromThenTo
444 %*********************************************************
446 \subsection{Printing floating point}
448 %*********************************************************
452 showFloat :: (RealFloat a) => a -> ShowS
453 showFloat x = showString (formatRealFloat FFGeneric Nothing x)
455 -- These are the format types. This type is not exported.
457 data FFFormat = FFExponent | FFFixed | FFGeneric
459 formatRealFloat :: (RealFloat a) => FFFormat -> Maybe Int -> a -> String
460 formatRealFloat fmt decs x
462 | isInfinite x = if x < 0 then "-Infinity" else "Infinity"
463 | x < 0 || isNegativeZero x = '-':doFmt fmt (floatToDigits (toInteger base) (-x))
464 | otherwise = doFmt fmt (floatToDigits (toInteger base) x)
468 doFmt format (is, e) =
469 let ds = map intToDigit is in
472 doFmt (if e < 0 || e > 7 then FFExponent else FFFixed)
477 let show_e' = show (e-1) in
480 [d] -> d : ".0e" ++ show_e'
481 (d:ds') -> d : '.' : ds' ++ "e" ++ show_e'
483 let dec' = max dec 1 in
485 [0] -> '0' :'.' : take dec' (repeat '0') ++ "e0"
488 (ei,is') = roundTo base (dec'+1) is
489 (d:ds') = map intToDigit (if ei > 0 then init is' else is')
491 d:'.':ds' ++ 'e':show (e-1+ei)
494 mk0 ls = case ls of { "" -> "0" ; _ -> ls}
498 | e <= 0 -> "0." ++ replicate (-e) '0' ++ ds
501 f 0 s rs = mk0 (reverse s) ++ '.':mk0 rs
502 f n s "" = f (n-1) ('0':s) ""
503 f n s (r:rs) = f (n-1) (r:s) rs
507 let dec' = max dec 0 in
510 (ei,is') = roundTo base (dec' + e) is
511 (ls,rs) = splitAt (e+ei) (map intToDigit is')
513 mk0 ls ++ (if null rs then "" else '.':rs)
516 (ei,is') = roundTo base dec' (replicate (-e) 0 ++ is)
517 d:ds' = map intToDigit (if ei > 0 then is' else 0:is')
519 d : (if null ds' then "" else '.':ds')
522 roundTo :: Int -> Int -> [Int] -> (Int,[Int])
530 f n [] = (0, replicate n 0)
531 f 0 (x:_) = (if x >= b2 then 1 else 0, [])
533 | i' == base = (1,0:ds)
534 | otherwise = (0,i':ds)
539 -- Based on "Printing Floating-Point Numbers Quickly and Accurately"
540 -- by R.G. Burger and R.K. Dybvig in PLDI 96.
541 -- This version uses a much slower logarithm estimator. It should be improved.
543 -- floatToDigits takes a base and a non-negative RealFloat number,
544 -- and returns a list of digits and an exponent.
545 -- In particular, if x>=0, and
546 -- floatToDigits base x = ([d1,d2,...,dn], e)
549 -- (b) x = 0.d1d2...dn * (base**e)
550 -- (c) 0 <= di <= base-1
552 floatToDigits :: (RealFloat a) => Integer -> a -> ([Int], Int)
553 floatToDigits _ 0 = ([0], 0)
554 floatToDigits base x =
556 (f0, e0) = decodeFloat x
557 (minExp0, _) = floatRange x
560 minExp = minExp0 - p -- the real minimum exponent
561 -- Haskell requires that f be adjusted so denormalized numbers
562 -- will have an impossibly low exponent. Adjust for this.
564 let n = minExp - e0 in
565 if n > 0 then (f0 `div` (b^n), e0+n) else (f0, e0)
570 (f*be*b*2, 2*b, be*b, b)
574 if e > minExp && f == b^(p-1) then
575 (f*b*2, b^(-e+1)*2, b, 1)
577 (f*2, b^(-e)*2, 1, 1)
581 if b == 2 && base == 10 then
582 -- logBase 10 2 is slightly bigger than 3/10 so
583 -- the following will err on the low side. Ignoring
584 -- the fraction will make it err even more.
585 -- Haskell promises that p-1 <= logBase b f < p.
586 (p - 1 + e0) * 3 `div` 10
588 ceiling ((log (fromInteger (f+1)) +
589 fromInteger (int2Integer e) * log (fromInteger b)) /
590 log (fromInteger base))
591 --WAS: fromInt e * log (fromInteger b))
595 if r + mUp <= expt base n * s then n else fixup (n+1)
597 if expt base (-n) * (r + mUp) <= s then n else fixup (n+1)
601 gen ds rn sN mUpN mDnN =
603 (dn, rn') = (rn * base) `divMod` sN
607 case (rn' < mDnN', rn' + mUpN' > sN) of
608 (True, False) -> dn : ds
609 (False, True) -> dn+1 : ds
610 (True, True) -> if rn' * 2 < sN then dn : ds else dn+1 : ds
611 (False, False) -> gen (dn:ds) rn' sN mUpN' mDnN'
615 gen [] r (s * expt base k) mUp mDn
617 let bk = expt base (-k) in
618 gen [] (r * bk) s (mUp * bk) (mDn * bk)
620 (map fromIntegral (reverse rds), k)
625 %*********************************************************
627 \subsection{Converting from a Rational to a RealFloat
629 %*********************************************************
631 [In response to a request for documentation of how fromRational works,
632 Joe Fasel writes:] A quite reasonable request! This code was added to
633 the Prelude just before the 1.2 release, when Lennart, working with an
634 early version of hbi, noticed that (read . show) was not the identity
635 for floating-point numbers. (There was a one-bit error about half the
636 time.) The original version of the conversion function was in fact
637 simply a floating-point divide, as you suggest above. The new version
638 is, I grant you, somewhat denser.
640 Unfortunately, Joe's code doesn't work! Here's an example:
642 main = putStr (shows (1.82173691287639817263897126389712638972163e-300::Double) "\n")
647 1.8217369128763981e-300
652 fromRat :: (RealFloat a) => Rational -> a
656 -- If the exponent of the nearest floating-point number to x
657 -- is e, then the significand is the integer nearest xb^(-e),
658 -- where b is the floating-point radix. We start with a good
659 -- guess for e, and if it is correct, the exponent of the
660 -- floating-point number we construct will again be e. If
661 -- not, one more iteration is needed.
663 f e = if e' == e then y else f e'
664 where y = encodeFloat (round (x * (1 % b)^^e)) e
665 (_,e') = decodeFloat y
668 -- We obtain a trial exponent by doing a floating-point
669 -- division of x's numerator by its denominator. The
670 -- result of this division may not itself be the ultimate
671 -- result, because of an accumulation of three rounding
674 (s,e) = decodeFloat (fromInteger (numerator x) `asTypeOf` x'
675 / fromInteger (denominator x))
678 Now, here's Lennart's code (which works)
681 {-# SPECIALISE fromRat :: Rational -> Double,
682 Rational -> Float #-}
683 fromRat :: (RealFloat a) => Rational -> a
685 -- Deal with special cases first, delegating the real work to fromRat'
686 fromRat (n :% 0) | n > 0 = 1/0 -- +Infinity
687 | n == 0 = 0/0 -- NaN
688 | n < 0 = -1/0 -- -Infinity
690 fromRat (n :% d) | n > 0 = fromRat' (n :% d)
691 | n == 0 = encodeFloat 0 0 -- Zero
692 | n < 0 = - fromRat' ((-n) :% d)
694 -- Conversion process:
695 -- Scale the rational number by the RealFloat base until
696 -- it lies in the range of the mantissa (as used by decodeFloat/encodeFloat).
697 -- Then round the rational to an Integer and encode it with the exponent
698 -- that we got from the scaling.
699 -- To speed up the scaling process we compute the log2 of the number to get
700 -- a first guess of the exponent.
702 fromRat' :: (RealFloat a) => Rational -> a
703 -- Invariant: argument is strictly positive
705 where b = floatRadix r
707 (minExp0, _) = floatRange r
708 minExp = minExp0 - p -- the real minimum exponent
709 xMin = toRational (expt b (p-1))
710 xMax = toRational (expt b p)
711 p0 = (integerLogBase b (numerator x) - integerLogBase b (denominator x) - p) `max` minExp
712 f = if p0 < 0 then 1 % expt b (-p0) else expt b p0 % 1
713 (x', p') = scaleRat (toRational b) minExp xMin xMax p0 (x / f)
714 r = encodeFloat (round x') p'
716 -- Scale x until xMin <= x < xMax, or p (the exponent) <= minExp.
717 scaleRat :: Rational -> Int -> Rational -> Rational -> Int -> Rational -> (Rational, Int)
718 scaleRat b minExp xMin xMax p x
719 | p <= minExp = (x, p)
720 | x >= xMax = scaleRat b minExp xMin xMax (p+1) (x/b)
721 | x < xMin = scaleRat b minExp xMin xMax (p-1) (x*b)
724 -- Exponentiation with a cache for the most common numbers.
725 minExpt, maxExpt :: Int
729 expt :: Integer -> Int -> Integer
731 if base == 2 && n >= minExpt && n <= maxExpt then
736 expts :: Array Int Integer
737 expts = array (minExpt,maxExpt) [(n,2^n) | n <- [minExpt .. maxExpt]]
739 -- Compute the (floor of the) log of i in base b.
740 -- Simplest way would be just divide i by b until it's smaller then b, but that would
741 -- be very slow! We are just slightly more clever.
742 integerLogBase :: Integer -> Integer -> Int
745 | otherwise = doDiv (i `div` (b^l)) l
747 -- Try squaring the base first to cut down the number of divisions.
748 l = 2 * integerLogBase (b*b) i
750 doDiv :: Integer -> Int -> Int
753 | otherwise = doDiv (x `div` b) (y+1)
758 %*********************************************************
760 \subsection{Floating point numeric primops}
762 %*********************************************************
764 Definitions of the boxed PrimOps; these will be
765 used in the case of partial applications, etc.
768 plusFloat, minusFloat, timesFloat, divideFloat :: Float -> Float -> Float
769 plusFloat (F# x) (F# y) = F# (plusFloat# x y)
770 minusFloat (F# x) (F# y) = F# (minusFloat# x y)
771 timesFloat (F# x) (F# y) = F# (timesFloat# x y)
772 divideFloat (F# x) (F# y) = F# (divideFloat# x y)
774 negateFloat :: Float -> Float
775 negateFloat (F# x) = F# (negateFloat# x)
777 gtFloat, geFloat, eqFloat, neFloat, ltFloat, leFloat :: Float -> Float -> Bool
778 gtFloat (F# x) (F# y) = gtFloat# x y
779 geFloat (F# x) (F# y) = geFloat# x y
780 eqFloat (F# x) (F# y) = eqFloat# x y
781 neFloat (F# x) (F# y) = neFloat# x y
782 ltFloat (F# x) (F# y) = ltFloat# x y
783 leFloat (F# x) (F# y) = leFloat# x y
785 float2Int :: Float -> Int
786 float2Int (F# x) = I# (float2Int# x)
788 int2Float :: Int -> Float
789 int2Float (I# x) = F# (int2Float# x)
791 expFloat, logFloat, sqrtFloat :: Float -> Float
792 sinFloat, cosFloat, tanFloat :: Float -> Float
793 asinFloat, acosFloat, atanFloat :: Float -> Float
794 sinhFloat, coshFloat, tanhFloat :: Float -> Float
795 expFloat (F# x) = F# (expFloat# x)
796 logFloat (F# x) = F# (logFloat# x)
797 sqrtFloat (F# x) = F# (sqrtFloat# x)
798 sinFloat (F# x) = F# (sinFloat# x)
799 cosFloat (F# x) = F# (cosFloat# x)
800 tanFloat (F# x) = F# (tanFloat# x)
801 asinFloat (F# x) = F# (asinFloat# x)
802 acosFloat (F# x) = F# (acosFloat# x)
803 atanFloat (F# x) = F# (atanFloat# x)
804 sinhFloat (F# x) = F# (sinhFloat# x)
805 coshFloat (F# x) = F# (coshFloat# x)
806 tanhFloat (F# x) = F# (tanhFloat# x)
808 powerFloat :: Float -> Float -> Float
809 powerFloat (F# x) (F# y) = F# (powerFloat# x y)
811 -- definitions of the boxed PrimOps; these will be
812 -- used in the case of partial applications, etc.
814 plusDouble, minusDouble, timesDouble, divideDouble :: Double -> Double -> Double
815 plusDouble (D# x) (D# y) = D# (x +## y)
816 minusDouble (D# x) (D# y) = D# (x -## y)
817 timesDouble (D# x) (D# y) = D# (x *## y)
818 divideDouble (D# x) (D# y) = D# (x /## y)
820 negateDouble :: Double -> Double
821 negateDouble (D# x) = D# (negateDouble# x)
823 gtDouble, geDouble, eqDouble, neDouble, leDouble, ltDouble :: Double -> Double -> Bool
824 gtDouble (D# x) (D# y) = x >## y
825 geDouble (D# x) (D# y) = x >=## y
826 eqDouble (D# x) (D# y) = x ==## y
827 neDouble (D# x) (D# y) = x /=## y
828 ltDouble (D# x) (D# y) = x <## y
829 leDouble (D# x) (D# y) = x <=## y
831 double2Int :: Double -> Int
832 double2Int (D# x) = I# (double2Int# x)
834 int2Double :: Int -> Double
835 int2Double (I# x) = D# (int2Double# x)
837 double2Float :: Double -> Float
838 double2Float (D# x) = F# (double2Float# x)
840 float2Double :: Float -> Double
841 float2Double (F# x) = D# (float2Double# x)
843 expDouble, logDouble, sqrtDouble :: Double -> Double
844 sinDouble, cosDouble, tanDouble :: Double -> Double
845 asinDouble, acosDouble, atanDouble :: Double -> Double
846 sinhDouble, coshDouble, tanhDouble :: Double -> Double
847 expDouble (D# x) = D# (expDouble# x)
848 logDouble (D# x) = D# (logDouble# x)
849 sqrtDouble (D# x) = D# (sqrtDouble# x)
850 sinDouble (D# x) = D# (sinDouble# x)
851 cosDouble (D# x) = D# (cosDouble# x)
852 tanDouble (D# x) = D# (tanDouble# x)
853 asinDouble (D# x) = D# (asinDouble# x)
854 acosDouble (D# x) = D# (acosDouble# x)
855 atanDouble (D# x) = D# (atanDouble# x)
856 sinhDouble (D# x) = D# (sinhDouble# x)
857 coshDouble (D# x) = D# (coshDouble# x)
858 tanhDouble (D# x) = D# (tanhDouble# x)
860 powerDouble :: Double -> Double -> Double
861 powerDouble (D# x) (D# y) = D# (x **## y)
865 foreign import ccall unsafe "__encodeFloat"
866 encodeFloat# :: Int# -> ByteArray# -> Int -> Float
867 foreign import ccall unsafe "__int_encodeFloat"
868 int_encodeFloat# :: Int# -> Int -> Float
871 foreign import ccall unsafe "isFloatNaN" isFloatNaN :: Float -> Int
872 foreign import ccall unsafe "isFloatInfinite" isFloatInfinite :: Float -> Int
873 foreign import ccall unsafe "isFloatDenormalized" isFloatDenormalized :: Float -> Int
874 foreign import ccall unsafe "isFloatNegativeZero" isFloatNegativeZero :: Float -> Int
877 foreign import ccall unsafe "__encodeDouble"
878 encodeDouble# :: Int# -> ByteArray# -> Int -> Double
879 foreign import ccall unsafe "__int_encodeDouble"
880 int_encodeDouble# :: Int# -> Int -> Double
882 foreign import ccall unsafe "isDoubleNaN" isDoubleNaN :: Double -> Int
883 foreign import ccall unsafe "isDoubleInfinite" isDoubleInfinite :: Double -> Int
884 foreign import ccall unsafe "isDoubleDenormalized" isDoubleDenormalized :: Double -> Int
885 foreign import ccall unsafe "isDoubleNegativeZero" isDoubleNegativeZero :: Double -> Int
888 %*********************************************************
890 \subsection{Coercion rules}
892 %*********************************************************
896 "fromIntegral/Int->Float" fromIntegral = int2Float
897 "fromIntegral/Int->Double" fromIntegral = int2Double
898 "realToFrac/Float->Float" realToFrac = id :: Float -> Float
899 "realToFrac/Float->Double" realToFrac = float2Double
900 "realToFrac/Double->Float" realToFrac = double2Float
901 "realToFrac/Double->Double" realToFrac = id :: Double -> Double