2 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
3 -- We believe we could deorphan this module, by moving lots of things
4 -- around, but we haven't got there yet:
5 {-# OPTIONS_GHC -fno-warn-orphans #-}
6 {-# OPTIONS_HADDOCK hide #-}
7 -----------------------------------------------------------------------------
10 -- Copyright : (c) The University of Glasgow 1994-2002
11 -- License : see libraries/base/LICENSE
13 -- Maintainer : cvs-ghc@haskell.org
14 -- Stability : internal
15 -- Portability : non-portable (GHC Extensions)
17 -- The types 'Float' and 'Double', and the classes 'Floating' and 'RealFloat'.
19 -----------------------------------------------------------------------------
21 #include "ieee-flpt.h"
24 module GHC.Float( module GHC.Float, Float(..), Double(..), Float#, Double# )
41 %*********************************************************
43 \subsection{Standard numeric classes}
45 %*********************************************************
48 -- | Trigonometric and hyperbolic functions and related functions.
50 -- Minimal complete definition:
51 -- 'pi', 'exp', 'log', 'sin', 'cos', 'sinh', 'cosh',
52 -- 'asin', 'acos', 'atan', 'asinh', 'acosh' and 'atanh'
53 class (Fractional a) => Floating a where
55 exp, log, sqrt :: a -> a
56 (**), logBase :: a -> a -> a
57 sin, cos, tan :: a -> a
58 asin, acos, atan :: a -> a
59 sinh, cosh, tanh :: a -> a
60 asinh, acosh, atanh :: a -> a
63 {-# INLINE logBase #-}
67 x ** y = exp (log x * y)
68 logBase x y = log y / log x
71 tanh x = sinh x / cosh x
73 -- | Efficient, machine-independent access to the components of a
74 -- floating-point number.
76 -- Minimal complete definition:
77 -- all except 'exponent', 'significand', 'scaleFloat' and 'atan2'
78 class (RealFrac a, Floating a) => RealFloat a where
79 -- | a constant function, returning the radix of the representation
81 floatRadix :: a -> Integer
82 -- | a constant function, returning the number of digits of
83 -- 'floatRadix' in the significand
84 floatDigits :: a -> Int
85 -- | a constant function, returning the lowest and highest values
86 -- the exponent may assume
87 floatRange :: a -> (Int,Int)
88 -- | The function 'decodeFloat' applied to a real floating-point
89 -- number returns the significand expressed as an 'Integer' and an
90 -- appropriately scaled exponent (an 'Int'). If @'decodeFloat' x@
91 -- yields @(m,n)@, then @x@ is equal in value to @m*b^^n@, where @b@
92 -- is the floating-point radix, and furthermore, either @m@ and @n@
93 -- are both zero or else @b^(d-1) <= m < b^d@, where @d@ is the value
94 -- of @'floatDigits' x@. In particular, @'decodeFloat' 0 = (0,0)@.
95 decodeFloat :: a -> (Integer,Int)
96 -- | 'encodeFloat' performs the inverse of 'decodeFloat'
97 encodeFloat :: Integer -> Int -> a
98 -- | the second component of 'decodeFloat'.
100 -- | the first component of 'decodeFloat', scaled to lie in the open
101 -- interval (@-1@,@1@)
102 significand :: a -> a
103 -- | multiplies a floating-point number by an integer power of the radix
104 scaleFloat :: Int -> a -> a
105 -- | 'True' if the argument is an IEEE \"not-a-number\" (NaN) value
107 -- | 'True' if the argument is an IEEE infinity or negative infinity
108 isInfinite :: a -> Bool
109 -- | 'True' if the argument is too small to be represented in
111 isDenormalized :: a -> Bool
112 -- | 'True' if the argument is an IEEE negative zero
113 isNegativeZero :: a -> Bool
114 -- | 'True' if the argument is an IEEE floating point number
116 -- | a version of arctangent taking two real floating-point arguments.
117 -- For real floating @x@ and @y@, @'atan2' y x@ computes the angle
118 -- (from the positive x-axis) of the vector from the origin to the
119 -- point @(x,y)@. @'atan2' y x@ returns a value in the range [@-pi@,
120 -- @pi@]. It follows the Common Lisp semantics for the origin when
121 -- signed zeroes are supported. @'atan2' y 1@, with @y@ in a type
122 -- that is 'RealFloat', should return the same value as @'atan' y@.
123 -- A default definition of 'atan2' is provided, but implementors
124 -- can provide a more accurate implementation.
128 exponent x = if m == 0 then 0 else n + floatDigits x
129 where (m,n) = decodeFloat x
131 significand x = encodeFloat m (negate (floatDigits x))
132 where (m,_) = decodeFloat x
134 scaleFloat k x = encodeFloat m (n+k)
135 where (m,n) = decodeFloat x
139 | x == 0 && y > 0 = pi/2
140 | x < 0 && y > 0 = pi + atan (y/x)
141 |(x <= 0 && y < 0) ||
142 (x < 0 && isNegativeZero y) ||
143 (isNegativeZero x && isNegativeZero y)
145 | y == 0 && (x < 0 || isNegativeZero x)
146 = pi -- must be after the previous test on zero y
147 | x==0 && y==0 = y -- must be after the other double zero tests
148 | otherwise = x + y -- x or y is a NaN, return a NaN (via +)
152 %*********************************************************
154 \subsection{Type @Float@}
156 %*********************************************************
159 instance Num Float where
160 (+) x y = plusFloat x y
161 (-) x y = minusFloat x y
162 negate x = negateFloat x
163 (*) x y = timesFloat x y
165 | otherwise = negateFloat x
166 signum x | x == 0.0 = 0
168 | otherwise = negate 1
170 {-# INLINE fromInteger #-}
171 fromInteger i = F# (floatFromInteger i)
173 instance Real Float where
174 toRational x = (m%1)*(b%1)^^n
175 where (m,n) = decodeFloat x
178 instance Fractional Float where
179 (/) x y = divideFloat x y
180 fromRational x = fromRat x
183 {-# RULES "truncate/Float->Int" truncate = float2Int #-}
184 instance RealFrac Float where
186 {-# SPECIALIZE properFraction :: Float -> (Int, Float) #-}
187 {-# SPECIALIZE round :: Float -> Int #-}
189 {-# SPECIALIZE properFraction :: Float -> (Integer, Float) #-}
190 {-# SPECIALIZE round :: Float -> Integer #-}
192 -- ceiling, floor, and truncate are all small
193 {-# INLINE ceiling #-}
195 {-# INLINE truncate #-}
197 -- We assume that FLT_RADIX is 2 so that we can use more efficient code
199 #error FLT_RADIX must be 2
201 properFraction (F# x#)
202 = case decodeFloat_Int# x# of
208 then (fromIntegral m * (2 ^ n), 0.0)
209 else let i = if m >= 0 then m `shiftR` negate n
210 else negate (negate m `shiftR` negate n)
211 f = m - (i `shiftL` negate n)
212 in (fromIntegral i, encodeFloat (fromIntegral f) n)
214 truncate x = case properFraction x of
217 round x = case properFraction x of
219 m = if r < 0.0 then n - 1 else n + 1
220 half_down = abs r - 0.5
222 case (compare half_down 0.0) of
224 EQ -> if even n then n else m
227 ceiling x = case properFraction x of
228 (n,r) -> if r > 0.0 then n + 1 else n
230 floor x = case properFraction x of
231 (n,r) -> if r < 0.0 then n - 1 else n
233 instance Floating Float where
234 pi = 3.141592653589793238
247 (**) x y = powerFloat x y
248 logBase x y = log y / log x
250 asinh x = log (x + sqrt (1.0+x*x))
251 acosh x = log (x + (x+1.0) * sqrt ((x-1.0)/(x+1.0)))
252 atanh x = log ((x+1.0) / sqrt (1.0-x*x))
254 instance RealFloat Float where
255 floatRadix _ = FLT_RADIX -- from float.h
256 floatDigits _ = FLT_MANT_DIG -- ditto
257 floatRange _ = (FLT_MIN_EXP, FLT_MAX_EXP) -- ditto
259 decodeFloat (F# f#) = case decodeFloat_Int# f# of
260 (# i, e #) -> (smallInteger i, I# e)
262 encodeFloat i (I# e) = F# (encodeFloatInteger i e)
264 exponent x = case decodeFloat x of
265 (m,n) -> if m == 0 then 0 else n + floatDigits x
267 significand x = case decodeFloat x of
268 (m,_) -> encodeFloat m (negate (floatDigits x))
270 scaleFloat k x = case decodeFloat x of
271 (m,n) -> encodeFloat m (n+k)
272 isNaN x = 0 /= isFloatNaN x
273 isInfinite x = 0 /= isFloatInfinite x
274 isDenormalized x = 0 /= isFloatDenormalized x
275 isNegativeZero x = 0 /= isFloatNegativeZero x
278 instance Show Float where
279 showsPrec x = showSignedFloat showFloat x
280 showList = showList__ (showsPrec 0)
283 %*********************************************************
285 \subsection{Type @Double@}
287 %*********************************************************
290 instance Num Double where
291 (+) x y = plusDouble x y
292 (-) x y = minusDouble x y
293 negate x = negateDouble x
294 (*) x y = timesDouble x y
296 | otherwise = negateDouble x
297 signum x | x == 0.0 = 0
299 | otherwise = negate 1
301 {-# INLINE fromInteger #-}
302 fromInteger i = D# (doubleFromInteger i)
305 instance Real Double where
306 toRational x = (m%1)*(b%1)^^n
307 where (m,n) = decodeFloat x
310 instance Fractional Double where
311 (/) x y = divideDouble x y
312 fromRational x = fromRat x
315 instance Floating Double where
316 pi = 3.141592653589793238
319 sqrt x = sqrtDouble x
323 asin x = asinDouble x
324 acos x = acosDouble x
325 atan x = atanDouble x
326 sinh x = sinhDouble x
327 cosh x = coshDouble x
328 tanh x = tanhDouble x
329 (**) x y = powerDouble x y
330 logBase x y = log y / log x
332 asinh x = log (x + sqrt (1.0+x*x))
333 acosh x = log (x + (x+1.0) * sqrt ((x-1.0)/(x+1.0)))
334 atanh x = log ((x+1.0) / sqrt (1.0-x*x))
336 {-# RULES "truncate/Double->Int" truncate = double2Int #-}
337 instance RealFrac Double where
339 {-# SPECIALIZE properFraction :: Double -> (Int, Double) #-}
340 {-# SPECIALIZE round :: Double -> Int #-}
342 {-# SPECIALIZE properFraction :: Double -> (Integer, Double) #-}
343 {-# SPECIALIZE round :: Double -> Integer #-}
345 -- ceiling, floor, and truncate are all small
346 {-# INLINE ceiling #-}
348 {-# INLINE truncate #-}
351 = case (decodeFloat x) of { (m,n) ->
352 let b = floatRadix x in
354 (fromInteger m * fromInteger b ^ n, 0.0)
356 case (quotRem m (b^(negate n))) of { (w,r) ->
357 (fromInteger w, encodeFloat r n)
361 truncate x = case properFraction x of
364 round x = case properFraction x of
366 m = if r < 0.0 then n - 1 else n + 1
367 half_down = abs r - 0.5
369 case (compare half_down 0.0) of
371 EQ -> if even n then n else m
374 ceiling x = case properFraction x of
375 (n,r) -> if r > 0.0 then n + 1 else n
377 floor x = case properFraction x of
378 (n,r) -> if r < 0.0 then n - 1 else n
380 instance RealFloat Double where
381 floatRadix _ = FLT_RADIX -- from float.h
382 floatDigits _ = DBL_MANT_DIG -- ditto
383 floatRange _ = (DBL_MIN_EXP, DBL_MAX_EXP) -- ditto
386 = case decodeDoubleInteger x# of
387 (# i, j #) -> (i, I# j)
389 encodeFloat i (I# j) = D# (encodeDoubleInteger i j)
391 exponent x = case decodeFloat x of
392 (m,n) -> if m == 0 then 0 else n + floatDigits x
394 significand x = case decodeFloat x of
395 (m,_) -> encodeFloat m (negate (floatDigits x))
397 scaleFloat k x = case decodeFloat x of
398 (m,n) -> encodeFloat m (n+k)
400 isNaN x = 0 /= isDoubleNaN x
401 isInfinite x = 0 /= isDoubleInfinite x
402 isDenormalized x = 0 /= isDoubleDenormalized x
403 isNegativeZero x = 0 /= isDoubleNegativeZero x
406 instance Show Double where
407 showsPrec x = showSignedFloat showFloat x
408 showList = showList__ (showsPrec 0)
411 %*********************************************************
413 \subsection{@Enum@ instances}
415 %*********************************************************
417 The @Enum@ instances for Floats and Doubles are slightly unusual.
418 The @toEnum@ function truncates numbers to Int. The definitions
419 of @enumFrom@ and @enumFromThen@ allow floats to be used in arithmetic
420 series: [0,0.1 .. 1.0]. However, roundoff errors make these somewhat
421 dubious. This example may have either 10 or 11 elements, depending on
422 how 0.1 is represented.
424 NOTE: The instances for Float and Double do not make use of the default
425 methods for @enumFromTo@ and @enumFromThenTo@, as these rely on there being
426 a `non-lossy' conversion to and from Ints. Instead we make use of the
427 1.2 default methods (back in the days when Enum had Ord as a superclass)
428 for these (@numericEnumFromTo@ and @numericEnumFromThenTo@ below.)
431 instance Enum Float where
435 fromEnum = fromInteger . truncate -- may overflow
436 enumFrom = numericEnumFrom
437 enumFromTo = numericEnumFromTo
438 enumFromThen = numericEnumFromThen
439 enumFromThenTo = numericEnumFromThenTo
441 instance Enum Double where
445 fromEnum = fromInteger . truncate -- may overflow
446 enumFrom = numericEnumFrom
447 enumFromTo = numericEnumFromTo
448 enumFromThen = numericEnumFromThen
449 enumFromThenTo = numericEnumFromThenTo
453 %*********************************************************
455 \subsection{Printing floating point}
457 %*********************************************************
461 -- | Show a signed 'RealFloat' value to full precision
462 -- using standard decimal notation for arguments whose absolute value lies
463 -- between @0.1@ and @9,999,999@, and scientific notation otherwise.
464 showFloat :: (RealFloat a) => a -> ShowS
465 showFloat x = showString (formatRealFloat FFGeneric Nothing x)
467 -- These are the format types. This type is not exported.
469 data FFFormat = FFExponent | FFFixed | FFGeneric
471 formatRealFloat :: (RealFloat a) => FFFormat -> Maybe Int -> a -> String
472 formatRealFloat fmt decs x
474 | isInfinite x = if x < 0 then "-Infinity" else "Infinity"
475 | x < 0 || isNegativeZero x = '-':doFmt fmt (floatToDigits (toInteger base) (-x))
476 | otherwise = doFmt fmt (floatToDigits (toInteger base) x)
480 doFmt format (is, e) =
481 let ds = map intToDigit is in
484 doFmt (if e < 0 || e > 7 then FFExponent else FFFixed)
489 let show_e' = show (e-1) in
492 [d] -> d : ".0e" ++ show_e'
493 (d:ds') -> d : '.' : ds' ++ "e" ++ show_e'
494 [] -> error "formatRealFloat/doFmt/FFExponent: []"
496 let dec' = max dec 1 in
498 [0] -> '0' :'.' : take dec' (repeat '0') ++ "e0"
501 (ei,is') = roundTo base (dec'+1) is
502 (d:ds') = map intToDigit (if ei > 0 then init is' else is')
504 d:'.':ds' ++ 'e':show (e-1+ei)
507 mk0 ls = case ls of { "" -> "0" ; _ -> ls}
511 | e <= 0 -> "0." ++ replicate (-e) '0' ++ ds
514 f 0 s rs = mk0 (reverse s) ++ '.':mk0 rs
515 f n s "" = f (n-1) ('0':s) ""
516 f n s (r:rs) = f (n-1) (r:s) rs
520 let dec' = max dec 0 in
523 (ei,is') = roundTo base (dec' + e) is
524 (ls,rs) = splitAt (e+ei) (map intToDigit is')
526 mk0 ls ++ (if null rs then "" else '.':rs)
529 (ei,is') = roundTo base dec' (replicate (-e) 0 ++ is)
530 d:ds' = map intToDigit (if ei > 0 then is' else 0:is')
532 d : (if null ds' then "" else '.':ds')
535 roundTo :: Int -> Int -> [Int] -> (Int,[Int])
540 _ -> error "roundTo: bad Value"
544 f n [] = (0, replicate n 0)
545 f 0 (x:_) = (if x >= b2 then 1 else 0, [])
547 | i' == base = (1,0:ds)
548 | otherwise = (0,i':ds)
553 -- Based on "Printing Floating-Point Numbers Quickly and Accurately"
554 -- by R.G. Burger and R.K. Dybvig in PLDI 96.
555 -- This version uses a much slower logarithm estimator. It should be improved.
557 -- | 'floatToDigits' takes a base and a non-negative 'RealFloat' number,
558 -- and returns a list of digits and an exponent.
559 -- In particular, if @x>=0@, and
561 -- > floatToDigits base x = ([d1,d2,...,dn], e)
567 -- (2) @x = 0.d1d2...dn * (base**e)@
569 -- (3) @0 <= di <= base-1@
571 floatToDigits :: (RealFloat a) => Integer -> a -> ([Int], Int)
572 floatToDigits _ 0 = ([0], 0)
573 floatToDigits base x =
575 (f0, e0) = decodeFloat x
576 (minExp0, _) = floatRange x
579 minExp = minExp0 - p -- the real minimum exponent
580 -- Haskell requires that f be adjusted so denormalized numbers
581 -- will have an impossibly low exponent. Adjust for this.
583 let n = minExp - e0 in
584 if n > 0 then (f0 `div` (b^n), e0+n) else (f0, e0)
589 (f*be*b*2, 2*b, be*b, b)
593 if e > minExp && f == b^(p-1) then
594 (f*b*2, b^(-e+1)*2, b, 1)
596 (f*2, b^(-e)*2, 1, 1)
602 if b == 2 && base == 10 then
603 -- logBase 10 2 is slightly bigger than 3/10 so
604 -- the following will err on the low side. Ignoring
605 -- the fraction will make it err even more.
606 -- Haskell promises that p-1 <= logBase b f < p.
607 (p - 1 + e0) * 3 `div` 10
609 -- f :: Integer, log :: Float -> Float,
610 -- ceiling :: Float -> Int
611 ceiling ((log (fromInteger (f+1) :: Float) +
612 fromIntegral e * log (fromInteger b)) /
613 log (fromInteger base))
614 --WAS: fromInt e * log (fromInteger b))
618 if r + mUp <= expt base n * s then n else fixup (n+1)
620 if expt base (-n) * (r + mUp) <= s then n else fixup (n+1)
624 gen ds rn sN mUpN mDnN =
626 (dn, rn') = (rn * base) `divMod` sN
630 case (rn' < mDnN', rn' + mUpN' > sN) of
631 (True, False) -> dn : ds
632 (False, True) -> dn+1 : ds
633 (True, True) -> if rn' * 2 < sN then dn : ds else dn+1 : ds
634 (False, False) -> gen (dn:ds) rn' sN mUpN' mDnN'
638 gen [] r (s * expt base k) mUp mDn
640 let bk = expt base (-k) in
641 gen [] (r * bk) s (mUp * bk) (mDn * bk)
643 (map fromIntegral (reverse rds), k)
648 %*********************************************************
650 \subsection{Converting from a Rational to a RealFloat
652 %*********************************************************
654 [In response to a request for documentation of how fromRational works,
655 Joe Fasel writes:] A quite reasonable request! This code was added to
656 the Prelude just before the 1.2 release, when Lennart, working with an
657 early version of hbi, noticed that (read . show) was not the identity
658 for floating-point numbers. (There was a one-bit error about half the
659 time.) The original version of the conversion function was in fact
660 simply a floating-point divide, as you suggest above. The new version
661 is, I grant you, somewhat denser.
663 Unfortunately, Joe's code doesn't work! Here's an example:
665 main = putStr (shows (1.82173691287639817263897126389712638972163e-300::Double) "\n")
670 1.8217369128763981e-300
675 fromRat :: (RealFloat a) => Rational -> a
679 -- If the exponent of the nearest floating-point number to x
680 -- is e, then the significand is the integer nearest xb^(-e),
681 -- where b is the floating-point radix. We start with a good
682 -- guess for e, and if it is correct, the exponent of the
683 -- floating-point number we construct will again be e. If
684 -- not, one more iteration is needed.
686 f e = if e' == e then y else f e'
687 where y = encodeFloat (round (x * (1 % b)^^e)) e
688 (_,e') = decodeFloat y
691 -- We obtain a trial exponent by doing a floating-point
692 -- division of x's numerator by its denominator. The
693 -- result of this division may not itself be the ultimate
694 -- result, because of an accumulation of three rounding
697 (s,e) = decodeFloat (fromInteger (numerator x) `asTypeOf` x'
698 / fromInteger (denominator x))
701 Now, here's Lennart's code (which works)
704 -- | Converts a 'Rational' value into any type in class 'RealFloat'.
705 {-# SPECIALISE fromRat :: Rational -> Double,
706 Rational -> Float #-}
707 fromRat :: (RealFloat a) => Rational -> a
709 -- Deal with special cases first, delegating the real work to fromRat'
710 fromRat (n :% 0) | n > 0 = 1/0 -- +Infinity
711 | n < 0 = -1/0 -- -Infinity
712 | otherwise = 0/0 -- NaN
714 fromRat (n :% d) | n > 0 = fromRat' (n :% d)
715 | n < 0 = - fromRat' ((-n) :% d)
716 | otherwise = encodeFloat 0 0 -- Zero
718 -- Conversion process:
719 -- Scale the rational number by the RealFloat base until
720 -- it lies in the range of the mantissa (as used by decodeFloat/encodeFloat).
721 -- Then round the rational to an Integer and encode it with the exponent
722 -- that we got from the scaling.
723 -- To speed up the scaling process we compute the log2 of the number to get
724 -- a first guess of the exponent.
726 fromRat' :: (RealFloat a) => Rational -> a
727 -- Invariant: argument is strictly positive
729 where b = floatRadix r
731 (minExp0, _) = floatRange r
732 minExp = minExp0 - p -- the real minimum exponent
733 xMin = toRational (expt b (p-1))
734 xMax = toRational (expt b p)
735 p0 = (integerLogBase b (numerator x) - integerLogBase b (denominator x) - p) `max` minExp
736 f = if p0 < 0 then 1 % expt b (-p0) else expt b p0 % 1
737 (x', p') = scaleRat (toRational b) minExp xMin xMax p0 (x / f)
738 r = encodeFloat (round x') p'
740 -- Scale x until xMin <= x < xMax, or p (the exponent) <= minExp.
741 scaleRat :: Rational -> Int -> Rational -> Rational -> Int -> Rational -> (Rational, Int)
742 scaleRat b minExp xMin xMax p x
743 | p <= minExp = (x, p)
744 | x >= xMax = scaleRat b minExp xMin xMax (p+1) (x/b)
745 | x < xMin = scaleRat b minExp xMin xMax (p-1) (x*b)
748 -- Exponentiation with a cache for the most common numbers.
749 minExpt, maxExpt :: Int
753 expt :: Integer -> Int -> Integer
755 if base == 2 && n >= minExpt && n <= maxExpt then
760 expts :: Array Int Integer
761 expts = array (minExpt,maxExpt) [(n,2^n) | n <- [minExpt .. maxExpt]]
763 -- Compute the (floor of the) log of i in base b.
764 -- Simplest way would be just divide i by b until it's smaller then b, but that would
765 -- be very slow! We are just slightly more clever.
766 integerLogBase :: Integer -> Integer -> Int
769 | otherwise = doDiv (i `div` (b^l)) l
771 -- Try squaring the base first to cut down the number of divisions.
772 l = 2 * integerLogBase (b*b) i
774 doDiv :: Integer -> Int -> Int
777 | otherwise = doDiv (x `div` b) (y+1)
782 %*********************************************************
784 \subsection{Floating point numeric primops}
786 %*********************************************************
788 Definitions of the boxed PrimOps; these will be
789 used in the case of partial applications, etc.
792 plusFloat, minusFloat, timesFloat, divideFloat :: Float -> Float -> Float
793 plusFloat (F# x) (F# y) = F# (plusFloat# x y)
794 minusFloat (F# x) (F# y) = F# (minusFloat# x y)
795 timesFloat (F# x) (F# y) = F# (timesFloat# x y)
796 divideFloat (F# x) (F# y) = F# (divideFloat# x y)
798 negateFloat :: Float -> Float
799 negateFloat (F# x) = F# (negateFloat# x)
801 gtFloat, geFloat, eqFloat, neFloat, ltFloat, leFloat :: Float -> Float -> Bool
802 gtFloat (F# x) (F# y) = gtFloat# x y
803 geFloat (F# x) (F# y) = geFloat# x y
804 eqFloat (F# x) (F# y) = eqFloat# x y
805 neFloat (F# x) (F# y) = neFloat# x y
806 ltFloat (F# x) (F# y) = ltFloat# x y
807 leFloat (F# x) (F# y) = leFloat# x y
809 float2Int :: Float -> Int
810 float2Int (F# x) = I# (float2Int# x)
812 int2Float :: Int -> Float
813 int2Float (I# x) = F# (int2Float# x)
815 expFloat, logFloat, sqrtFloat :: Float -> Float
816 sinFloat, cosFloat, tanFloat :: Float -> Float
817 asinFloat, acosFloat, atanFloat :: Float -> Float
818 sinhFloat, coshFloat, tanhFloat :: Float -> Float
819 expFloat (F# x) = F# (expFloat# x)
820 logFloat (F# x) = F# (logFloat# x)
821 sqrtFloat (F# x) = F# (sqrtFloat# x)
822 sinFloat (F# x) = F# (sinFloat# x)
823 cosFloat (F# x) = F# (cosFloat# x)
824 tanFloat (F# x) = F# (tanFloat# x)
825 asinFloat (F# x) = F# (asinFloat# x)
826 acosFloat (F# x) = F# (acosFloat# x)
827 atanFloat (F# x) = F# (atanFloat# x)
828 sinhFloat (F# x) = F# (sinhFloat# x)
829 coshFloat (F# x) = F# (coshFloat# x)
830 tanhFloat (F# x) = F# (tanhFloat# x)
832 powerFloat :: Float -> Float -> Float
833 powerFloat (F# x) (F# y) = F# (powerFloat# x y)
835 -- definitions of the boxed PrimOps; these will be
836 -- used in the case of partial applications, etc.
838 plusDouble, minusDouble, timesDouble, divideDouble :: Double -> Double -> Double
839 plusDouble (D# x) (D# y) = D# (x +## y)
840 minusDouble (D# x) (D# y) = D# (x -## y)
841 timesDouble (D# x) (D# y) = D# (x *## y)
842 divideDouble (D# x) (D# y) = D# (x /## y)
844 negateDouble :: Double -> Double
845 negateDouble (D# x) = D# (negateDouble# x)
847 gtDouble, geDouble, eqDouble, neDouble, leDouble, ltDouble :: Double -> Double -> Bool
848 gtDouble (D# x) (D# y) = x >## y
849 geDouble (D# x) (D# y) = x >=## y
850 eqDouble (D# x) (D# y) = x ==## y
851 neDouble (D# x) (D# y) = x /=## y
852 ltDouble (D# x) (D# y) = x <## y
853 leDouble (D# x) (D# y) = x <=## y
855 double2Int :: Double -> Int
856 double2Int (D# x) = I# (double2Int# x)
858 int2Double :: Int -> Double
859 int2Double (I# x) = D# (int2Double# x)
861 double2Float :: Double -> Float
862 double2Float (D# x) = F# (double2Float# x)
864 float2Double :: Float -> Double
865 float2Double (F# x) = D# (float2Double# x)
867 expDouble, logDouble, sqrtDouble :: Double -> Double
868 sinDouble, cosDouble, tanDouble :: Double -> Double
869 asinDouble, acosDouble, atanDouble :: Double -> Double
870 sinhDouble, coshDouble, tanhDouble :: Double -> Double
871 expDouble (D# x) = D# (expDouble# x)
872 logDouble (D# x) = D# (logDouble# x)
873 sqrtDouble (D# x) = D# (sqrtDouble# x)
874 sinDouble (D# x) = D# (sinDouble# x)
875 cosDouble (D# x) = D# (cosDouble# x)
876 tanDouble (D# x) = D# (tanDouble# x)
877 asinDouble (D# x) = D# (asinDouble# x)
878 acosDouble (D# x) = D# (acosDouble# x)
879 atanDouble (D# x) = D# (atanDouble# x)
880 sinhDouble (D# x) = D# (sinhDouble# x)
881 coshDouble (D# x) = D# (coshDouble# x)
882 tanhDouble (D# x) = D# (tanhDouble# x)
884 powerDouble :: Double -> Double -> Double
885 powerDouble (D# x) (D# y) = D# (x **## y)
889 foreign import ccall unsafe "isFloatNaN" isFloatNaN :: Float -> Int
890 foreign import ccall unsafe "isFloatInfinite" isFloatInfinite :: Float -> Int
891 foreign import ccall unsafe "isFloatDenormalized" isFloatDenormalized :: Float -> Int
892 foreign import ccall unsafe "isFloatNegativeZero" isFloatNegativeZero :: Float -> Int
895 foreign import ccall unsafe "isDoubleNaN" isDoubleNaN :: Double -> Int
896 foreign import ccall unsafe "isDoubleInfinite" isDoubleInfinite :: Double -> Int
897 foreign import ccall unsafe "isDoubleDenormalized" isDoubleDenormalized :: Double -> Int
898 foreign import ccall unsafe "isDoubleNegativeZero" isDoubleNegativeZero :: Double -> Int
901 %*********************************************************
903 \subsection{Coercion rules}
905 %*********************************************************
909 "fromIntegral/Int->Float" fromIntegral = int2Float
910 "fromIntegral/Int->Double" fromIntegral = int2Double
911 "realToFrac/Float->Float" realToFrac = id :: Float -> Float
912 "realToFrac/Float->Double" realToFrac = float2Double
913 "realToFrac/Double->Float" realToFrac = double2Float
914 "realToFrac/Double->Double" realToFrac = id :: Double -> Double
915 "realToFrac/Int->Double" realToFrac = int2Double -- See Note [realToFrac int-to-float]
916 "realToFrac/Int->Float" realToFrac = int2Float -- ..ditto
920 Note [realToFrac int-to-float]
921 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
922 Don found that the RULES for realToFrac/Int->Double and simliarly
923 Float made a huge difference to some stream-fusion programs. Here's
926 import Data.Array.Vector
931 let c = replicateU n (2::Double)
932 a = mapU realToFrac (enumFromToU 0 (n-1) ) :: UArr Double
933 print (sumU (zipWithU (*) c a))
935 Without the RULE we get this loop body:
937 case $wtoRational sc_sY4 of ww_aM7 { (# ww1_aM9, ww2_aMa #) ->
938 case $wfromRat ww1_aM9 ww2_aMa of tpl_X1P { D# ipv_sW3 ->
942 (+## sc2_sY6 (*## 2.0 ipv_sW3))
949 (+## sc2_sXV (*## 2.0 (int2Double# sc_sXT)))
951 The running time of the program goes from 120 seconds to 0.198 seconds
952 with the native backend, and 0.143 seconds with the C backend.
954 A few more details in Trac #2251, and the patch message
955 "Add RULES for realToFrac from Int".
957 %*********************************************************
961 %*********************************************************
964 showSignedFloat :: (RealFloat a)
965 => (a -> ShowS) -- ^ a function that can show unsigned values
966 -> Int -- ^ the precedence of the enclosing context
967 -> a -- ^ the value to show
969 showSignedFloat showPos p x
970 | x < 0 || isNegativeZero x
971 = showParen (p > 6) (showChar '-' . showPos (-x))
972 | otherwise = showPos x