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 | isInfinite x = if x < 0 then -infinity else infinity
175 | isNaN x = notANumber
176 | isNegativeZero x = negativeZero
177 | otherwise = (m%1)*(b%1)^^n
178 where (m,n) = decodeFloat x
181 instance Fractional Float where
182 (/) x y = divideFloat x y
183 fromRational x = fromRat x
186 {-# RULES "truncate/Float->Int" truncate = float2Int #-}
187 instance RealFrac Float where
189 {-# SPECIALIZE properFraction :: Float -> (Int, Float) #-}
190 {-# SPECIALIZE round :: Float -> Int #-}
192 {-# SPECIALIZE properFraction :: Float -> (Integer, Float) #-}
193 {-# SPECIALIZE round :: Float -> Integer #-}
195 -- ceiling, floor, and truncate are all small
196 {-# INLINE ceiling #-}
198 {-# INLINE truncate #-}
200 -- We assume that FLT_RADIX is 2 so that we can use more efficient code
202 #error FLT_RADIX must be 2
204 properFraction (F# x#)
205 = case decodeFloat_Int# x# of
211 then (fromIntegral m * (2 ^ n), 0.0)
212 else let i = if m >= 0 then m `shiftR` negate n
213 else negate (negate m `shiftR` negate n)
214 f = m - (i `shiftL` negate n)
215 in (fromIntegral i, encodeFloat (fromIntegral f) n)
217 truncate x = case properFraction x of
220 round x = case properFraction x of
222 m = if r < 0.0 then n - 1 else n + 1
223 half_down = abs r - 0.5
225 case (compare half_down 0.0) of
227 EQ -> if even n then n else m
230 ceiling x = case properFraction x of
231 (n,r) -> if r > 0.0 then n + 1 else n
233 floor x = case properFraction x of
234 (n,r) -> if r < 0.0 then n - 1 else n
236 instance Floating Float where
237 pi = 3.141592653589793238
250 (**) x y = powerFloat x y
251 logBase x y = log y / log x
253 asinh x = log (x + sqrt (1.0+x*x))
254 acosh x = log (x + (x+1.0) * sqrt ((x-1.0)/(x+1.0)))
255 atanh x = log ((x+1.0) / sqrt (1.0-x*x))
257 instance RealFloat Float where
258 floatRadix _ = FLT_RADIX -- from float.h
259 floatDigits _ = FLT_MANT_DIG -- ditto
260 floatRange _ = (FLT_MIN_EXP, FLT_MAX_EXP) -- ditto
262 decodeFloat (F# f#) = case decodeFloat_Int# f# of
263 (# i, e #) -> (smallInteger i, I# e)
265 encodeFloat i (I# e) = F# (encodeFloatInteger i e)
267 exponent x = case decodeFloat x of
268 (m,n) -> if m == 0 then 0 else n + floatDigits x
270 significand x = case decodeFloat x of
271 (m,_) -> encodeFloat m (negate (floatDigits x))
273 scaleFloat k x = case decodeFloat x of
274 (m,n) -> encodeFloat m (n+k)
275 isNaN x = 0 /= isFloatNaN x
276 isInfinite x = 0 /= isFloatInfinite x
277 isDenormalized x = 0 /= isFloatDenormalized x
278 isNegativeZero x = 0 /= isFloatNegativeZero x
281 instance Show Float where
282 showsPrec x = showSignedFloat showFloat x
283 showList = showList__ (showsPrec 0)
286 %*********************************************************
288 \subsection{Type @Double@}
290 %*********************************************************
293 instance Num Double where
294 (+) x y = plusDouble x y
295 (-) x y = minusDouble x y
296 negate x = negateDouble x
297 (*) x y = timesDouble x y
299 | otherwise = negateDouble x
300 signum x | x == 0.0 = 0
302 | otherwise = negate 1
304 {-# INLINE fromInteger #-}
305 fromInteger i = D# (doubleFromInteger i)
308 instance Real Double where
309 toRational x | isInfinite x = if x < 0 then -infinity else infinity
310 | isNaN x = notANumber
311 | isNegativeZero x = negativeZero
312 | otherwise = (m%1)*(b%1)^^n
313 where (m,n) = decodeFloat x
316 instance Fractional Double where
317 (/) x y = divideDouble x y
318 fromRational x = fromRat x
321 instance Floating Double where
322 pi = 3.141592653589793238
325 sqrt x = sqrtDouble x
329 asin x = asinDouble x
330 acos x = acosDouble x
331 atan x = atanDouble x
332 sinh x = sinhDouble x
333 cosh x = coshDouble x
334 tanh x = tanhDouble x
335 (**) x y = powerDouble x y
336 logBase x y = log y / log x
338 asinh x = log (x + sqrt (1.0+x*x))
339 acosh x = log (x + (x+1.0) * sqrt ((x-1.0)/(x+1.0)))
340 atanh x = log ((x+1.0) / sqrt (1.0-x*x))
342 {-# RULES "truncate/Double->Int" truncate = double2Int #-}
343 instance RealFrac Double where
345 {-# SPECIALIZE properFraction :: Double -> (Int, Double) #-}
346 {-# SPECIALIZE round :: Double -> Int #-}
348 {-# SPECIALIZE properFraction :: Double -> (Integer, Double) #-}
349 {-# SPECIALIZE round :: Double -> Integer #-}
351 -- ceiling, floor, and truncate are all small
352 {-# INLINE ceiling #-}
354 {-# INLINE truncate #-}
357 = case (decodeFloat x) of { (m,n) ->
358 let b = floatRadix x in
360 (fromInteger m * fromInteger b ^ n, 0.0)
362 case (quotRem m (b^(negate n))) of { (w,r) ->
363 (fromInteger w, encodeFloat r n)
367 truncate x = case properFraction x of
370 round x = case properFraction x of
372 m = if r < 0.0 then n - 1 else n + 1
373 half_down = abs r - 0.5
375 case (compare half_down 0.0) of
377 EQ -> if even n then n else m
380 ceiling x = case properFraction x of
381 (n,r) -> if r > 0.0 then n + 1 else n
383 floor x = case properFraction x of
384 (n,r) -> if r < 0.0 then n - 1 else n
386 instance RealFloat Double where
387 floatRadix _ = FLT_RADIX -- from float.h
388 floatDigits _ = DBL_MANT_DIG -- ditto
389 floatRange _ = (DBL_MIN_EXP, DBL_MAX_EXP) -- ditto
392 = case decodeDoubleInteger x# of
393 (# i, j #) -> (i, I# j)
395 encodeFloat i (I# j) = D# (encodeDoubleInteger i j)
397 exponent x = case decodeFloat x of
398 (m,n) -> if m == 0 then 0 else n + floatDigits x
400 significand x = case decodeFloat x of
401 (m,_) -> encodeFloat m (negate (floatDigits x))
403 scaleFloat k x = case decodeFloat x of
404 (m,n) -> encodeFloat m (n+k)
406 isNaN x = 0 /= isDoubleNaN x
407 isInfinite x = 0 /= isDoubleInfinite x
408 isDenormalized x = 0 /= isDoubleDenormalized x
409 isNegativeZero x = 0 /= isDoubleNegativeZero x
412 instance Show Double where
413 showsPrec x = showSignedFloat showFloat x
414 showList = showList__ (showsPrec 0)
417 %*********************************************************
419 \subsection{@Enum@ instances}
421 %*********************************************************
423 The @Enum@ instances for Floats and Doubles are slightly unusual.
424 The @toEnum@ function truncates numbers to Int. The definitions
425 of @enumFrom@ and @enumFromThen@ allow floats to be used in arithmetic
426 series: [0,0.1 .. 1.0]. However, roundoff errors make these somewhat
427 dubious. This example may have either 10 or 11 elements, depending on
428 how 0.1 is represented.
430 NOTE: The instances for Float and Double do not make use of the default
431 methods for @enumFromTo@ and @enumFromThenTo@, as these rely on there being
432 a `non-lossy' conversion to and from Ints. Instead we make use of the
433 1.2 default methods (back in the days when Enum had Ord as a superclass)
434 for these (@numericEnumFromTo@ and @numericEnumFromThenTo@ below.)
437 instance Enum Float where
441 fromEnum = fromInteger . truncate -- may overflow
442 enumFrom = numericEnumFrom
443 enumFromTo = numericEnumFromTo
444 enumFromThen = numericEnumFromThen
445 enumFromThenTo = numericEnumFromThenTo
447 instance Enum Double where
451 fromEnum = fromInteger . truncate -- may overflow
452 enumFrom = numericEnumFrom
453 enumFromTo = numericEnumFromTo
454 enumFromThen = numericEnumFromThen
455 enumFromThenTo = numericEnumFromThenTo
459 %*********************************************************
461 \subsection{Printing floating point}
463 %*********************************************************
467 -- | Show a signed 'RealFloat' value to full precision
468 -- using standard decimal notation for arguments whose absolute value lies
469 -- between @0.1@ and @9,999,999@, and scientific notation otherwise.
470 showFloat :: (RealFloat a) => a -> ShowS
471 showFloat x = showString (formatRealFloat FFGeneric Nothing x)
473 -- These are the format types. This type is not exported.
475 data FFFormat = FFExponent | FFFixed | FFGeneric
477 formatRealFloat :: (RealFloat a) => FFFormat -> Maybe Int -> a -> String
478 formatRealFloat fmt decs x
480 | isInfinite x = if x < 0 then "-Infinity" else "Infinity"
481 | x < 0 || isNegativeZero x = '-':doFmt fmt (floatToDigits (toInteger base) (-x))
482 | otherwise = doFmt fmt (floatToDigits (toInteger base) x)
486 doFmt format (is, e) =
487 let ds = map intToDigit is in
490 doFmt (if e < 0 || e > 7 then FFExponent else FFFixed)
495 let show_e' = show (e-1) in
498 [d] -> d : ".0e" ++ show_e'
499 (d:ds') -> d : '.' : ds' ++ "e" ++ show_e'
500 [] -> error "formatRealFloat/doFmt/FFExponent: []"
502 let dec' = max dec 1 in
504 [0] -> '0' :'.' : take dec' (repeat '0') ++ "e0"
507 (ei,is') = roundTo base (dec'+1) is
508 (d:ds') = map intToDigit (if ei > 0 then init is' else is')
510 d:'.':ds' ++ 'e':show (e-1+ei)
513 mk0 ls = case ls of { "" -> "0" ; _ -> ls}
517 | e <= 0 -> "0." ++ replicate (-e) '0' ++ ds
520 f 0 s rs = mk0 (reverse s) ++ '.':mk0 rs
521 f n s "" = f (n-1) ('0':s) ""
522 f n s (r:rs) = f (n-1) (r:s) rs
526 let dec' = max dec 0 in
529 (ei,is') = roundTo base (dec' + e) is
530 (ls,rs) = splitAt (e+ei) (map intToDigit is')
532 mk0 ls ++ (if null rs then "" else '.':rs)
535 (ei,is') = roundTo base dec' (replicate (-e) 0 ++ is)
536 d:ds' = map intToDigit (if ei > 0 then is' else 0:is')
538 d : (if null ds' then "" else '.':ds')
541 roundTo :: Int -> Int -> [Int] -> (Int,[Int])
546 _ -> error "roundTo: bad Value"
550 f n [] = (0, replicate n 0)
551 f 0 (x:_) = (if x >= b2 then 1 else 0, [])
553 | i' == base = (1,0:ds)
554 | otherwise = (0,i':ds)
559 -- Based on "Printing Floating-Point Numbers Quickly and Accurately"
560 -- by R.G. Burger and R.K. Dybvig in PLDI 96.
561 -- This version uses a much slower logarithm estimator. It should be improved.
563 -- | 'floatToDigits' takes a base and a non-negative 'RealFloat' number,
564 -- and returns a list of digits and an exponent.
565 -- In particular, if @x>=0@, and
567 -- > floatToDigits base x = ([d1,d2,...,dn], e)
573 -- (2) @x = 0.d1d2...dn * (base**e)@
575 -- (3) @0 <= di <= base-1@
577 floatToDigits :: (RealFloat a) => Integer -> a -> ([Int], Int)
578 floatToDigits _ 0 = ([0], 0)
579 floatToDigits base x =
581 (f0, e0) = decodeFloat x
582 (minExp0, _) = floatRange x
585 minExp = minExp0 - p -- the real minimum exponent
586 -- Haskell requires that f be adjusted so denormalized numbers
587 -- will have an impossibly low exponent. Adjust for this.
589 let n = minExp - e0 in
590 if n > 0 then (f0 `div` (b^n), e0+n) else (f0, e0)
595 (f*be*b*2, 2*b, be*b, b)
599 if e > minExp && f == b^(p-1) then
600 (f*b*2, b^(-e+1)*2, b, 1)
602 (f*2, b^(-e)*2, 1, 1)
608 if b == 2 && base == 10 then
609 -- logBase 10 2 is slightly bigger than 3/10 so
610 -- the following will err on the low side. Ignoring
611 -- the fraction will make it err even more.
612 -- Haskell promises that p-1 <= logBase b f < p.
613 (p - 1 + e0) * 3 `div` 10
615 -- f :: Integer, log :: Float -> Float,
616 -- ceiling :: Float -> Int
617 ceiling ((log (fromInteger (f+1) :: Float) +
618 fromIntegral e * log (fromInteger b)) /
619 log (fromInteger base))
620 --WAS: fromInt e * log (fromInteger b))
624 if r + mUp <= expt base n * s then n else fixup (n+1)
626 if expt base (-n) * (r + mUp) <= s then n else fixup (n+1)
630 gen ds rn sN mUpN mDnN =
632 (dn, rn') = (rn * base) `divMod` sN
636 case (rn' < mDnN', rn' + mUpN' > sN) of
637 (True, False) -> dn : ds
638 (False, True) -> dn+1 : ds
639 (True, True) -> if rn' * 2 < sN then dn : ds else dn+1 : ds
640 (False, False) -> gen (dn:ds) rn' sN mUpN' mDnN'
644 gen [] r (s * expt base k) mUp mDn
646 let bk = expt base (-k) in
647 gen [] (r * bk) s (mUp * bk) (mDn * bk)
649 (map fromIntegral (reverse rds), k)
654 %*********************************************************
656 \subsection{Converting from a Rational to a RealFloat
658 %*********************************************************
660 [In response to a request for documentation of how fromRational works,
661 Joe Fasel writes:] A quite reasonable request! This code was added to
662 the Prelude just before the 1.2 release, when Lennart, working with an
663 early version of hbi, noticed that (read . show) was not the identity
664 for floating-point numbers. (There was a one-bit error about half the
665 time.) The original version of the conversion function was in fact
666 simply a floating-point divide, as you suggest above. The new version
667 is, I grant you, somewhat denser.
669 Unfortunately, Joe's code doesn't work! Here's an example:
671 main = putStr (shows (1.82173691287639817263897126389712638972163e-300::Double) "\n")
676 1.8217369128763981e-300
681 fromRat :: (RealFloat a) => Rational -> a
685 -- If the exponent of the nearest floating-point number to x
686 -- is e, then the significand is the integer nearest xb^(-e),
687 -- where b is the floating-point radix. We start with a good
688 -- guess for e, and if it is correct, the exponent of the
689 -- floating-point number we construct will again be e. If
690 -- not, one more iteration is needed.
692 f e = if e' == e then y else f e'
693 where y = encodeFloat (round (x * (1 % b)^^e)) e
694 (_,e') = decodeFloat y
697 -- We obtain a trial exponent by doing a floating-point
698 -- division of x's numerator by its denominator. The
699 -- result of this division may not itself be the ultimate
700 -- result, because of an accumulation of three rounding
703 (s,e) = decodeFloat (fromInteger (numerator x) `asTypeOf` x'
704 / fromInteger (denominator x))
707 Now, here's Lennart's code (which works)
710 -- | Converts a 'Rational' value into any type in class 'RealFloat'.
711 {-# SPECIALISE fromRat :: Rational -> Double,
712 Rational -> Float #-}
713 fromRat :: (RealFloat a) => Rational -> a
715 -- Deal with special cases first, delegating the real work to fromRat'
716 fromRat (n :% 0) | n > 0 = 1/0 -- +Infinity
717 | n < 0 = -1/0 -- -Infinity
718 | otherwise = 0/0 -- NaN
720 fromRat (n :% d) | n > 0 = fromRat' (n :% d)
721 | n < 0 = - fromRat' ((-n) :% d)
722 | d < 0 = 0/(-1) -- -0.0
723 | otherwise = encodeFloat 0 0 -- Zero
725 -- Conversion process:
726 -- Scale the rational number by the RealFloat base until
727 -- it lies in the range of the mantissa (as used by decodeFloat/encodeFloat).
728 -- Then round the rational to an Integer and encode it with the exponent
729 -- that we got from the scaling.
730 -- To speed up the scaling process we compute the log2 of the number to get
731 -- a first guess of the exponent.
733 fromRat' :: (RealFloat a) => Rational -> a
734 -- Invariant: argument is strictly positive
736 where b = floatRadix r
738 (minExp0, _) = floatRange r
739 minExp = minExp0 - p -- the real minimum exponent
740 xMin = toRational (expt b (p-1))
741 xMax = toRational (expt b p)
742 p0 = (integerLogBase b (numerator x) - integerLogBase b (denominator x) - p) `max` minExp
743 f = if p0 < 0 then 1 % expt b (-p0) else expt b p0 % 1
744 (x', p') = scaleRat (toRational b) minExp xMin xMax p0 (x / f)
745 r = encodeFloat (round x') p'
747 -- Scale x until xMin <= x < xMax, or p (the exponent) <= minExp.
748 scaleRat :: Rational -> Int -> Rational -> Rational -> Int -> Rational -> (Rational, Int)
749 scaleRat b minExp xMin xMax p x
750 | p <= minExp = (x, p)
751 | x >= xMax = scaleRat b minExp xMin xMax (p+1) (x/b)
752 | x < xMin = scaleRat b minExp xMin xMax (p-1) (x*b)
755 -- Exponentiation with a cache for the most common numbers.
756 minExpt, maxExpt :: Int
760 expt :: Integer -> Int -> Integer
762 if base == 2 && n >= minExpt && n <= maxExpt then
767 expts :: Array Int Integer
768 expts = array (minExpt,maxExpt) [(n,2^n) | n <- [minExpt .. maxExpt]]
770 -- Compute the (floor of the) log of i in base b.
771 -- Simplest way would be just divide i by b until it's smaller then b, but that would
772 -- be very slow! We are just slightly more clever.
773 integerLogBase :: Integer -> Integer -> Int
776 | otherwise = doDiv (i `div` (b^l)) l
778 -- Try squaring the base first to cut down the number of divisions.
779 l = 2 * integerLogBase (b*b) i
781 doDiv :: Integer -> Int -> Int
784 | otherwise = doDiv (x `div` b) (y+1)
789 %*********************************************************
791 \subsection{Floating point numeric primops}
793 %*********************************************************
795 Definitions of the boxed PrimOps; these will be
796 used in the case of partial applications, etc.
799 plusFloat, minusFloat, timesFloat, divideFloat :: Float -> Float -> Float
800 plusFloat (F# x) (F# y) = F# (plusFloat# x y)
801 minusFloat (F# x) (F# y) = F# (minusFloat# x y)
802 timesFloat (F# x) (F# y) = F# (timesFloat# x y)
803 divideFloat (F# x) (F# y) = F# (divideFloat# x y)
805 negateFloat :: Float -> Float
806 negateFloat (F# x) = F# (negateFloat# x)
808 gtFloat, geFloat, eqFloat, neFloat, ltFloat, leFloat :: Float -> Float -> Bool
809 gtFloat (F# x) (F# y) = gtFloat# x y
810 geFloat (F# x) (F# y) = geFloat# x y
811 eqFloat (F# x) (F# y) = eqFloat# x y
812 neFloat (F# x) (F# y) = neFloat# x y
813 ltFloat (F# x) (F# y) = ltFloat# x y
814 leFloat (F# x) (F# y) = leFloat# x y
816 float2Int :: Float -> Int
817 float2Int (F# x) = I# (float2Int# x)
819 int2Float :: Int -> Float
820 int2Float (I# x) = F# (int2Float# x)
822 expFloat, logFloat, sqrtFloat :: Float -> Float
823 sinFloat, cosFloat, tanFloat :: Float -> Float
824 asinFloat, acosFloat, atanFloat :: Float -> Float
825 sinhFloat, coshFloat, tanhFloat :: Float -> Float
826 expFloat (F# x) = F# (expFloat# x)
827 logFloat (F# x) = F# (logFloat# x)
828 sqrtFloat (F# x) = F# (sqrtFloat# x)
829 sinFloat (F# x) = F# (sinFloat# x)
830 cosFloat (F# x) = F# (cosFloat# x)
831 tanFloat (F# x) = F# (tanFloat# x)
832 asinFloat (F# x) = F# (asinFloat# x)
833 acosFloat (F# x) = F# (acosFloat# x)
834 atanFloat (F# x) = F# (atanFloat# x)
835 sinhFloat (F# x) = F# (sinhFloat# x)
836 coshFloat (F# x) = F# (coshFloat# x)
837 tanhFloat (F# x) = F# (tanhFloat# x)
839 powerFloat :: Float -> Float -> Float
840 powerFloat (F# x) (F# y) = F# (powerFloat# x y)
842 -- definitions of the boxed PrimOps; these will be
843 -- used in the case of partial applications, etc.
845 plusDouble, minusDouble, timesDouble, divideDouble :: Double -> Double -> Double
846 plusDouble (D# x) (D# y) = D# (x +## y)
847 minusDouble (D# x) (D# y) = D# (x -## y)
848 timesDouble (D# x) (D# y) = D# (x *## y)
849 divideDouble (D# x) (D# y) = D# (x /## y)
851 negateDouble :: Double -> Double
852 negateDouble (D# x) = D# (negateDouble# x)
854 gtDouble, geDouble, eqDouble, neDouble, leDouble, ltDouble :: Double -> Double -> Bool
855 gtDouble (D# x) (D# y) = x >## y
856 geDouble (D# x) (D# y) = x >=## y
857 eqDouble (D# x) (D# y) = x ==## y
858 neDouble (D# x) (D# y) = x /=## y
859 ltDouble (D# x) (D# y) = x <## y
860 leDouble (D# x) (D# y) = x <=## y
862 double2Int :: Double -> Int
863 double2Int (D# x) = I# (double2Int# x)
865 int2Double :: Int -> Double
866 int2Double (I# x) = D# (int2Double# x)
868 double2Float :: Double -> Float
869 double2Float (D# x) = F# (double2Float# x)
871 float2Double :: Float -> Double
872 float2Double (F# x) = D# (float2Double# x)
874 expDouble, logDouble, sqrtDouble :: Double -> Double
875 sinDouble, cosDouble, tanDouble :: Double -> Double
876 asinDouble, acosDouble, atanDouble :: Double -> Double
877 sinhDouble, coshDouble, tanhDouble :: Double -> Double
878 expDouble (D# x) = D# (expDouble# x)
879 logDouble (D# x) = D# (logDouble# x)
880 sqrtDouble (D# x) = D# (sqrtDouble# x)
881 sinDouble (D# x) = D# (sinDouble# x)
882 cosDouble (D# x) = D# (cosDouble# x)
883 tanDouble (D# x) = D# (tanDouble# x)
884 asinDouble (D# x) = D# (asinDouble# x)
885 acosDouble (D# x) = D# (acosDouble# x)
886 atanDouble (D# x) = D# (atanDouble# x)
887 sinhDouble (D# x) = D# (sinhDouble# x)
888 coshDouble (D# x) = D# (coshDouble# x)
889 tanhDouble (D# x) = D# (tanhDouble# x)
891 powerDouble :: Double -> Double -> Double
892 powerDouble (D# x) (D# y) = D# (x **## y)
896 foreign import ccall unsafe "isFloatNaN" isFloatNaN :: Float -> Int
897 foreign import ccall unsafe "isFloatInfinite" isFloatInfinite :: Float -> Int
898 foreign import ccall unsafe "isFloatDenormalized" isFloatDenormalized :: Float -> Int
899 foreign import ccall unsafe "isFloatNegativeZero" isFloatNegativeZero :: Float -> Int
902 foreign import ccall unsafe "isDoubleNaN" isDoubleNaN :: Double -> Int
903 foreign import ccall unsafe "isDoubleInfinite" isDoubleInfinite :: Double -> Int
904 foreign import ccall unsafe "isDoubleDenormalized" isDoubleDenormalized :: Double -> Int
905 foreign import ccall unsafe "isDoubleNegativeZero" isDoubleNegativeZero :: Double -> Int
908 %*********************************************************
910 \subsection{Coercion rules}
912 %*********************************************************
916 "fromIntegral/Int->Float" fromIntegral = int2Float
917 "fromIntegral/Int->Double" fromIntegral = int2Double
918 "realToFrac/Float->Float" realToFrac = id :: Float -> Float
919 "realToFrac/Float->Double" realToFrac = float2Double
920 "realToFrac/Double->Float" realToFrac = double2Float
921 "realToFrac/Double->Double" realToFrac = id :: Double -> Double
922 "realToFrac/Int->Double" realToFrac = int2Double -- See Note [realToFrac int-to-float]
923 "realToFrac/Int->Float" realToFrac = int2Float -- ..ditto
927 Note [realToFrac int-to-float]
928 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
929 Don found that the RULES for realToFrac/Int->Double and simliarly
930 Float made a huge difference to some stream-fusion programs. Here's
933 import Data.Array.Vector
938 let c = replicateU n (2::Double)
939 a = mapU realToFrac (enumFromToU 0 (n-1) ) :: UArr Double
940 print (sumU (zipWithU (*) c a))
942 Without the RULE we get this loop body:
944 case $wtoRational sc_sY4 of ww_aM7 { (# ww1_aM9, ww2_aMa #) ->
945 case $wfromRat ww1_aM9 ww2_aMa of tpl_X1P { D# ipv_sW3 ->
949 (+## sc2_sY6 (*## 2.0 ipv_sW3))
956 (+## sc2_sXV (*## 2.0 (int2Double# sc_sXT)))
958 The running time of the program goes from 120 seconds to 0.198 seconds
959 with the native backend, and 0.143 seconds with the C backend.
961 A few more details in Trac #2251, and the patch message
962 "Add RULES for realToFrac from Int".
964 %*********************************************************
968 %*********************************************************
971 showSignedFloat :: (RealFloat a)
972 => (a -> ShowS) -- ^ a function that can show unsigned values
973 -> Int -- ^ the precedence of the enclosing context
974 -> a -- ^ the value to show
976 showSignedFloat showPos p x
977 | x < 0 || isNegativeZero x
978 = showParen (p > 6) (showChar '-' . showPos (-x))
979 | otherwise = showPos x