6 , ForeignFunctionInterface
8 -- We believe we could deorphan this module, by moving lots of things
9 -- around, but we haven't got there yet:
10 {-# OPTIONS_GHC -fno-warn-orphans #-}
11 {-# OPTIONS_HADDOCK hide #-}
13 -----------------------------------------------------------------------------
16 -- Copyright : (c) The University of Glasgow 1994-2002
17 -- License : see libraries/base/LICENSE
19 -- Maintainer : cvs-ghc@haskell.org
20 -- Stability : internal
21 -- Portability : non-portable (GHC Extensions)
23 -- The types 'Float' and 'Double', and the classes 'Floating' and 'RealFloat'.
25 -----------------------------------------------------------------------------
27 #include "ieee-flpt.h"
30 module GHC.Float( module GHC.Float, Float(..), Double(..), Float#, Double# )
47 %*********************************************************
49 \subsection{Standard numeric classes}
51 %*********************************************************
54 -- | Trigonometric and hyperbolic functions and related functions.
56 -- Minimal complete definition:
57 -- 'pi', 'exp', 'log', 'sin', 'cos', 'sinh', 'cosh',
58 -- 'asin', 'acos', 'atan', 'asinh', 'acosh' and 'atanh'
59 class (Fractional a) => Floating a where
61 exp, log, sqrt :: a -> a
62 (**), logBase :: a -> a -> a
63 sin, cos, tan :: a -> a
64 asin, acos, atan :: a -> a
65 sinh, cosh, tanh :: a -> a
66 asinh, acosh, atanh :: a -> a
69 {-# INLINE logBase #-}
73 x ** y = exp (log x * y)
74 logBase x y = log y / log x
77 tanh x = sinh x / cosh x
79 -- | Efficient, machine-independent access to the components of a
80 -- floating-point number.
82 -- Minimal complete definition:
83 -- all except 'exponent', 'significand', 'scaleFloat' and 'atan2'
84 class (RealFrac a, Floating a) => RealFloat a where
85 -- | a constant function, returning the radix of the representation
87 floatRadix :: a -> Integer
88 -- | a constant function, returning the number of digits of
89 -- 'floatRadix' in the significand
90 floatDigits :: a -> Int
91 -- | a constant function, returning the lowest and highest values
92 -- the exponent may assume
93 floatRange :: a -> (Int,Int)
94 -- | The function 'decodeFloat' applied to a real floating-point
95 -- number returns the significand expressed as an 'Integer' and an
96 -- appropriately scaled exponent (an 'Int'). If @'decodeFloat' x@
97 -- yields @(m,n)@, then @x@ is equal in value to @m*b^^n@, where @b@
98 -- is the floating-point radix, and furthermore, either @m@ and @n@
99 -- are both zero or else @b^(d-1) <= m < b^d@, where @d@ is the value
100 -- of @'floatDigits' x@. In particular, @'decodeFloat' 0 = (0,0)@.
101 decodeFloat :: a -> (Integer,Int)
102 -- | 'encodeFloat' performs the inverse of 'decodeFloat'
103 encodeFloat :: Integer -> Int -> a
104 -- | the second component of 'decodeFloat'.
106 -- | the first component of 'decodeFloat', scaled to lie in the open
107 -- interval (@-1@,@1@)
108 significand :: a -> a
109 -- | multiplies a floating-point number by an integer power of the radix
110 scaleFloat :: Int -> a -> a
111 -- | 'True' if the argument is an IEEE \"not-a-number\" (NaN) value
113 -- | 'True' if the argument is an IEEE infinity or negative infinity
114 isInfinite :: a -> Bool
115 -- | 'True' if the argument is too small to be represented in
117 isDenormalized :: a -> Bool
118 -- | 'True' if the argument is an IEEE negative zero
119 isNegativeZero :: a -> Bool
120 -- | 'True' if the argument is an IEEE floating point number
122 -- | a version of arctangent taking two real floating-point arguments.
123 -- For real floating @x@ and @y@, @'atan2' y x@ computes the angle
124 -- (from the positive x-axis) of the vector from the origin to the
125 -- point @(x,y)@. @'atan2' y x@ returns a value in the range [@-pi@,
126 -- @pi@]. It follows the Common Lisp semantics for the origin when
127 -- signed zeroes are supported. @'atan2' y 1@, with @y@ in a type
128 -- that is 'RealFloat', should return the same value as @'atan' y@.
129 -- A default definition of 'atan2' is provided, but implementors
130 -- can provide a more accurate implementation.
134 exponent x = if m == 0 then 0 else n + floatDigits x
135 where (m,n) = decodeFloat x
137 significand x = encodeFloat m (negate (floatDigits x))
138 where (m,_) = decodeFloat x
140 scaleFloat k x = encodeFloat m (n + clamp b k)
141 where (m,n) = decodeFloat x
145 -- n+k may overflow, which would lead
146 -- to wrong results, hence we clamp the
147 -- scaling parameter.
148 -- If n + k would be larger than h,
149 -- n + clamp b k must be too, simliar
150 -- for smaller than l - d.
151 -- Add a little extra to keep clear
152 -- from the boundary cases.
156 | x == 0 && y > 0 = pi/2
157 | x < 0 && y > 0 = pi + atan (y/x)
158 |(x <= 0 && y < 0) ||
159 (x < 0 && isNegativeZero y) ||
160 (isNegativeZero x && isNegativeZero y)
162 | y == 0 && (x < 0 || isNegativeZero x)
163 = pi -- must be after the previous test on zero y
164 | x==0 && y==0 = y -- must be after the other double zero tests
165 | otherwise = x + y -- x or y is a NaN, return a NaN (via +)
169 %*********************************************************
171 \subsection{Type @Float@}
173 %*********************************************************
176 instance Num Float where
177 (+) x y = plusFloat x y
178 (-) x y = minusFloat x y
179 negate x = negateFloat x
180 (*) x y = timesFloat x y
182 | otherwise = negateFloat x
183 signum x | x == 0.0 = 0
185 | otherwise = negate 1
187 {-# INLINE fromInteger #-}
188 fromInteger i = F# (floatFromInteger i)
190 instance Real Float where
191 toRational x = (m%1)*(b%1)^^n
192 where (m,n) = decodeFloat x
195 instance Fractional Float where
196 (/) x y = divideFloat x y
197 fromRational x = fromRat x
200 {-# RULES "truncate/Float->Int" truncate = float2Int #-}
201 instance RealFrac Float where
203 {-# SPECIALIZE properFraction :: Float -> (Int, Float) #-}
204 {-# SPECIALIZE round :: Float -> Int #-}
206 {-# SPECIALIZE properFraction :: Float -> (Integer, Float) #-}
207 {-# SPECIALIZE round :: Float -> Integer #-}
209 -- ceiling, floor, and truncate are all small
210 {-# INLINE ceiling #-}
212 {-# INLINE truncate #-}
214 -- We assume that FLT_RADIX is 2 so that we can use more efficient code
216 #error FLT_RADIX must be 2
218 properFraction (F# x#)
219 = case decodeFloat_Int# x# of
225 then (fromIntegral m * (2 ^ n), 0.0)
226 else let i = if m >= 0 then m `shiftR` negate n
227 else negate (negate m `shiftR` negate n)
228 f = m - (i `shiftL` negate n)
229 in (fromIntegral i, encodeFloat (fromIntegral f) n)
231 truncate x = case properFraction x of
234 round x = case properFraction x of
236 m = if r < 0.0 then n - 1 else n + 1
237 half_down = abs r - 0.5
239 case (compare half_down 0.0) of
241 EQ -> if even n then n else m
244 ceiling x = case properFraction x of
245 (n,r) -> if r > 0.0 then n + 1 else n
247 floor x = case properFraction x of
248 (n,r) -> if r < 0.0 then n - 1 else n
250 instance Floating Float where
251 pi = 3.141592653589793238
264 (**) x y = powerFloat x y
265 logBase x y = log y / log x
267 asinh x = log (x + sqrt (1.0+x*x))
268 acosh x = log (x + (x+1.0) * sqrt ((x-1.0)/(x+1.0)))
269 atanh x = 0.5 * log ((1.0+x) / (1.0-x))
271 instance RealFloat Float where
272 floatRadix _ = FLT_RADIX -- from float.h
273 floatDigits _ = FLT_MANT_DIG -- ditto
274 floatRange _ = (FLT_MIN_EXP, FLT_MAX_EXP) -- ditto
276 decodeFloat (F# f#) = case decodeFloat_Int# f# of
277 (# i, e #) -> (smallInteger i, I# e)
279 encodeFloat i (I# e) = F# (encodeFloatInteger i e)
281 exponent x = case decodeFloat x of
282 (m,n) -> if m == 0 then 0 else n + floatDigits x
284 significand x = case decodeFloat x of
285 (m,_) -> encodeFloat m (negate (floatDigits x))
287 scaleFloat k x = case decodeFloat x of
288 (m,n) -> encodeFloat m (n + clamp bf k)
289 where bf = FLT_MAX_EXP - (FLT_MIN_EXP) + 4*FLT_MANT_DIG
291 isNaN x = 0 /= isFloatNaN x
292 isInfinite x = 0 /= isFloatInfinite x
293 isDenormalized x = 0 /= isFloatDenormalized x
294 isNegativeZero x = 0 /= isFloatNegativeZero x
297 instance Show Float where
298 showsPrec x = showSignedFloat showFloat x
299 showList = showList__ (showsPrec 0)
302 %*********************************************************
304 \subsection{Type @Double@}
306 %*********************************************************
309 instance Num Double where
310 (+) x y = plusDouble x y
311 (-) x y = minusDouble x y
312 negate x = negateDouble x
313 (*) x y = timesDouble x y
315 | otherwise = negateDouble x
316 signum x | x == 0.0 = 0
318 | otherwise = negate 1
320 {-# INLINE fromInteger #-}
321 fromInteger i = D# (doubleFromInteger i)
324 instance Real Double where
325 toRational x = (m%1)*(b%1)^^n
326 where (m,n) = decodeFloat x
329 instance Fractional Double where
330 (/) x y = divideDouble x y
331 fromRational x = fromRat x
334 instance Floating Double where
335 pi = 3.141592653589793238
338 sqrt x = sqrtDouble x
342 asin x = asinDouble x
343 acos x = acosDouble x
344 atan x = atanDouble x
345 sinh x = sinhDouble x
346 cosh x = coshDouble x
347 tanh x = tanhDouble x
348 (**) x y = powerDouble x y
349 logBase x y = log y / log x
351 asinh x = log (x + sqrt (1.0+x*x))
352 acosh x = log (x + (x+1.0) * sqrt ((x-1.0)/(x+1.0)))
353 atanh x = 0.5 * log ((1.0+x) / (1.0-x))
355 {-# RULES "truncate/Double->Int" truncate = double2Int #-}
356 instance RealFrac Double where
358 {-# SPECIALIZE properFraction :: Double -> (Int, Double) #-}
359 {-# SPECIALIZE round :: Double -> Int #-}
361 {-# SPECIALIZE properFraction :: Double -> (Integer, Double) #-}
362 {-# SPECIALIZE round :: Double -> Integer #-}
364 -- ceiling, floor, and truncate are all small
365 {-# INLINE ceiling #-}
367 {-# INLINE truncate #-}
370 = case (decodeFloat x) of { (m,n) ->
371 let b = floatRadix x in
373 (fromInteger m * fromInteger b ^ n, 0.0)
375 case (quotRem m (b^(negate n))) of { (w,r) ->
376 (fromInteger w, encodeFloat r n)
380 truncate x = case properFraction x of
383 round x = case properFraction x of
385 m = if r < 0.0 then n - 1 else n + 1
386 half_down = abs r - 0.5
388 case (compare half_down 0.0) of
390 EQ -> if even n then n else m
393 ceiling x = case properFraction x of
394 (n,r) -> if r > 0.0 then n + 1 else n
396 floor x = case properFraction x of
397 (n,r) -> if r < 0.0 then n - 1 else n
399 instance RealFloat Double where
400 floatRadix _ = FLT_RADIX -- from float.h
401 floatDigits _ = DBL_MANT_DIG -- ditto
402 floatRange _ = (DBL_MIN_EXP, DBL_MAX_EXP) -- ditto
405 = case decodeDoubleInteger x# of
406 (# i, j #) -> (i, I# j)
408 encodeFloat i (I# j) = D# (encodeDoubleInteger i j)
410 exponent x = case decodeFloat x of
411 (m,n) -> if m == 0 then 0 else n + floatDigits x
413 significand x = case decodeFloat x of
414 (m,_) -> encodeFloat m (negate (floatDigits x))
416 scaleFloat k x = case decodeFloat x of
417 (m,n) -> encodeFloat m (n + clamp bd k)
418 where bd = DBL_MAX_EXP - (DBL_MIN_EXP) + 4*DBL_MANT_DIG
420 isNaN x = 0 /= isDoubleNaN x
421 isInfinite x = 0 /= isDoubleInfinite x
422 isDenormalized x = 0 /= isDoubleDenormalized x
423 isNegativeZero x = 0 /= isDoubleNegativeZero x
426 instance Show Double where
427 showsPrec x = showSignedFloat showFloat x
428 showList = showList__ (showsPrec 0)
431 %*********************************************************
433 \subsection{@Enum@ instances}
435 %*********************************************************
437 The @Enum@ instances for Floats and Doubles are slightly unusual.
438 The @toEnum@ function truncates numbers to Int. The definitions
439 of @enumFrom@ and @enumFromThen@ allow floats to be used in arithmetic
440 series: [0,0.1 .. 1.0]. However, roundoff errors make these somewhat
441 dubious. This example may have either 10 or 11 elements, depending on
442 how 0.1 is represented.
444 NOTE: The instances for Float and Double do not make use of the default
445 methods for @enumFromTo@ and @enumFromThenTo@, as these rely on there being
446 a `non-lossy' conversion to and from Ints. Instead we make use of the
447 1.2 default methods (back in the days when Enum had Ord as a superclass)
448 for these (@numericEnumFromTo@ and @numericEnumFromThenTo@ below.)
451 instance Enum Float where
455 fromEnum = fromInteger . truncate -- may overflow
456 enumFrom = numericEnumFrom
457 enumFromTo = numericEnumFromTo
458 enumFromThen = numericEnumFromThen
459 enumFromThenTo = numericEnumFromThenTo
461 instance Enum Double where
465 fromEnum = fromInteger . truncate -- may overflow
466 enumFrom = numericEnumFrom
467 enumFromTo = numericEnumFromTo
468 enumFromThen = numericEnumFromThen
469 enumFromThenTo = numericEnumFromThenTo
473 %*********************************************************
475 \subsection{Printing floating point}
477 %*********************************************************
481 -- | Show a signed 'RealFloat' value to full precision
482 -- using standard decimal notation for arguments whose absolute value lies
483 -- between @0.1@ and @9,999,999@, and scientific notation otherwise.
484 showFloat :: (RealFloat a) => a -> ShowS
485 showFloat x = showString (formatRealFloat FFGeneric Nothing x)
487 -- These are the format types. This type is not exported.
489 data FFFormat = FFExponent | FFFixed | FFGeneric
491 formatRealFloat :: (RealFloat a) => FFFormat -> Maybe Int -> a -> String
492 formatRealFloat fmt decs x
494 | isInfinite x = if x < 0 then "-Infinity" else "Infinity"
495 | x < 0 || isNegativeZero x = '-':doFmt fmt (floatToDigits (toInteger base) (-x))
496 | otherwise = doFmt fmt (floatToDigits (toInteger base) x)
500 doFmt format (is, e) =
501 let ds = map intToDigit is in
504 doFmt (if e < 0 || e > 7 then FFExponent else FFFixed)
509 let show_e' = show (e-1) in
512 [d] -> d : ".0e" ++ show_e'
513 (d:ds') -> d : '.' : ds' ++ "e" ++ show_e'
514 [] -> error "formatRealFloat/doFmt/FFExponent: []"
516 let dec' = max dec 1 in
518 [0] -> '0' :'.' : take dec' (repeat '0') ++ "e0"
521 (ei,is') = roundTo base (dec'+1) is
522 (d:ds') = map intToDigit (if ei > 0 then init is' else is')
524 d:'.':ds' ++ 'e':show (e-1+ei)
527 mk0 ls = case ls of { "" -> "0" ; _ -> ls}
531 | e <= 0 -> "0." ++ replicate (-e) '0' ++ ds
534 f 0 s rs = mk0 (reverse s) ++ '.':mk0 rs
535 f n s "" = f (n-1) ('0':s) ""
536 f n s (r:rs) = f (n-1) (r:s) rs
540 let dec' = max dec 0 in
543 (ei,is') = roundTo base (dec' + e) is
544 (ls,rs) = splitAt (e+ei) (map intToDigit is')
546 mk0 ls ++ (if null rs then "" else '.':rs)
549 (ei,is') = roundTo base dec' (replicate (-e) 0 ++ is)
550 d:ds' = map intToDigit (if ei > 0 then is' else 0:is')
552 d : (if null ds' then "" else '.':ds')
555 roundTo :: Int -> Int -> [Int] -> (Int,[Int])
560 _ -> error "roundTo: bad Value"
564 f n [] = (0, replicate n 0)
565 f 0 (x:_) = (if x >= b2 then 1 else 0, [])
567 | i' == base = (1,0:ds)
568 | otherwise = (0,i':ds)
573 -- Based on "Printing Floating-Point Numbers Quickly and Accurately"
574 -- by R.G. Burger and R.K. Dybvig in PLDI 96.
575 -- This version uses a much slower logarithm estimator. It should be improved.
577 -- | 'floatToDigits' takes a base and a non-negative 'RealFloat' number,
578 -- and returns a list of digits and an exponent.
579 -- In particular, if @x>=0@, and
581 -- > floatToDigits base x = ([d1,d2,...,dn], e)
587 -- (2) @x = 0.d1d2...dn * (base**e)@
589 -- (3) @0 <= di <= base-1@
591 floatToDigits :: (RealFloat a) => Integer -> a -> ([Int], Int)
592 floatToDigits _ 0 = ([0], 0)
593 floatToDigits base x =
595 (f0, e0) = decodeFloat x
596 (minExp0, _) = floatRange x
599 minExp = minExp0 - p -- the real minimum exponent
600 -- Haskell requires that f be adjusted so denormalized numbers
601 -- will have an impossibly low exponent. Adjust for this.
603 let n = minExp - e0 in
604 if n > 0 then (f0 `quot` (expt b n), e0+n) else (f0, e0)
608 if f == expt b (p-1) then
609 (f*be*b*2, 2*b, be*b, be) -- according to Burger and Dybvig
613 if e > minExp && f == expt b (p-1) then
614 (f*b*2, expt b (-e+1)*2, b, 1)
616 (f*2, expt b (-e)*2, 1, 1)
622 if b == 2 && base == 10 then
623 -- logBase 10 2 is very slightly larger than 8651/28738
624 -- (about 5.3558e-10), so if log x >= 0, the approximation
625 -- k1 is too small, hence we add one and need one fixup step less.
626 -- If log x < 0, the approximation errs rather on the high side.
627 -- That is usually more than compensated for by ignoring the
628 -- fractional part of logBase 2 x, but when x is a power of 1/2
629 -- or slightly larger and the exponent is a multiple of the
630 -- denominator of the rational approximation to logBase 10 2,
631 -- k1 is larger than logBase 10 x. If k1 > 1 + logBase 10 x,
632 -- we get a leading zero-digit we don't want.
633 -- With the approximation 3/10, this happened for
634 -- 0.5^1030, 0.5^1040, ..., 0.5^1070 and values close above.
635 -- The approximation 8651/28738 guarantees k1 < 1 + logBase 10 x
636 -- for IEEE-ish floating point types with exponent fields
637 -- <= 17 bits and mantissae of several thousand bits, earlier
638 -- convergents to logBase 10 2 would fail for long double.
639 -- Using quot instead of div is a little faster and requires
640 -- fewer fixup steps for negative lx.
642 k1 = (lx * 8651) `quot` 28738
643 in if lx >= 0 then k1 + 1 else k1
645 -- f :: Integer, log :: Float -> Float,
646 -- ceiling :: Float -> Int
647 ceiling ((log (fromInteger (f+1) :: Float) +
648 fromIntegral e * log (fromInteger b)) /
649 log (fromInteger base))
650 --WAS: fromInt e * log (fromInteger b))
654 if r + mUp <= expt base n * s then n else fixup (n+1)
656 if expt base (-n) * (r + mUp) <= s then n else fixup (n+1)
660 gen ds rn sN mUpN mDnN =
662 (dn, rn') = (rn * base) `quotRem` sN
666 case (rn' < mDnN', rn' + mUpN' > sN) of
667 (True, False) -> dn : ds
668 (False, True) -> dn+1 : ds
669 (True, True) -> if rn' * 2 < sN then dn : ds else dn+1 : ds
670 (False, False) -> gen (dn:ds) rn' sN mUpN' mDnN'
674 gen [] r (s * expt base k) mUp mDn
676 let bk = expt base (-k) in
677 gen [] (r * bk) s (mUp * bk) (mDn * bk)
679 (map fromIntegral (reverse rds), k)
684 %*********************************************************
686 \subsection{Converting from a Rational to a RealFloat
688 %*********************************************************
690 [In response to a request for documentation of how fromRational works,
691 Joe Fasel writes:] A quite reasonable request! This code was added to
692 the Prelude just before the 1.2 release, when Lennart, working with an
693 early version of hbi, noticed that (read . show) was not the identity
694 for floating-point numbers. (There was a one-bit error about half the
695 time.) The original version of the conversion function was in fact
696 simply a floating-point divide, as you suggest above. The new version
697 is, I grant you, somewhat denser.
699 Unfortunately, Joe's code doesn't work! Here's an example:
701 main = putStr (shows (1.82173691287639817263897126389712638972163e-300::Double) "\n")
706 1.8217369128763981e-300
711 fromRat :: (RealFloat a) => Rational -> a
715 -- If the exponent of the nearest floating-point number to x
716 -- is e, then the significand is the integer nearest xb^(-e),
717 -- where b is the floating-point radix. We start with a good
718 -- guess for e, and if it is correct, the exponent of the
719 -- floating-point number we construct will again be e. If
720 -- not, one more iteration is needed.
722 f e = if e' == e then y else f e'
723 where y = encodeFloat (round (x * (1 % b)^^e)) e
724 (_,e') = decodeFloat y
727 -- We obtain a trial exponent by doing a floating-point
728 -- division of x's numerator by its denominator. The
729 -- result of this division may not itself be the ultimate
730 -- result, because of an accumulation of three rounding
733 (s,e) = decodeFloat (fromInteger (numerator x) `asTypeOf` x'
734 / fromInteger (denominator x))
737 Now, here's Lennart's code (which works)
740 -- | Converts a 'Rational' value into any type in class 'RealFloat'.
741 {-# SPECIALISE fromRat :: Rational -> Double,
742 Rational -> Float #-}
743 fromRat :: (RealFloat a) => Rational -> a
745 -- Deal with special cases first, delegating the real work to fromRat'
746 fromRat (n :% 0) | n > 0 = 1/0 -- +Infinity
747 | n < 0 = -1/0 -- -Infinity
748 | otherwise = 0/0 -- NaN
750 fromRat (n :% d) | n > 0 = fromRat' (n :% d)
751 | n < 0 = - fromRat' ((-n) :% d)
752 | otherwise = encodeFloat 0 0 -- Zero
754 -- Conversion process:
755 -- Scale the rational number by the RealFloat base until
756 -- it lies in the range of the mantissa (as used by decodeFloat/encodeFloat).
757 -- Then round the rational to an Integer and encode it with the exponent
758 -- that we got from the scaling.
759 -- To speed up the scaling process we compute the log2 of the number to get
760 -- a first guess of the exponent.
762 fromRat' :: (RealFloat a) => Rational -> a
763 -- Invariant: argument is strictly positive
765 where b = floatRadix r
767 (minExp0, _) = floatRange r
768 minExp = minExp0 - p -- the real minimum exponent
769 xMin = toRational (expt b (p-1))
770 xMax = toRational (expt b p)
771 p0 = (integerLogBase b (numerator x) - integerLogBase b (denominator x) - p) `max` minExp
772 f = if p0 < 0 then 1 % expt b (-p0) else expt b p0 % 1
773 (x', p') = scaleRat (toRational b) minExp xMin xMax p0 (x / f)
774 r = encodeFloat (round x') p'
776 -- Scale x until xMin <= x < xMax, or p (the exponent) <= minExp.
777 scaleRat :: Rational -> Int -> Rational -> Rational -> Int -> Rational -> (Rational, Int)
778 scaleRat b minExp xMin xMax p x
779 | p <= minExp = (x, p)
780 | x >= xMax = scaleRat b minExp xMin xMax (p+1) (x/b)
781 | x < xMin = scaleRat b minExp xMin xMax (p-1) (x*b)
784 -- Exponentiation with a cache for the most common numbers.
785 minExpt, maxExpt :: Int
789 expt :: Integer -> Int -> Integer
791 if base == 2 && n >= minExpt && n <= maxExpt then
794 if base == 10 && n <= maxExpt10 then
799 expts :: Array Int Integer
800 expts = array (minExpt,maxExpt) [(n,2^n) | n <- [minExpt .. maxExpt]]
805 expts10 :: Array Int Integer
806 expts10 = array (minExpt,maxExpt10) [(n,10^n) | n <- [minExpt .. maxExpt10]]
808 -- Compute the (floor of the) log of i in base b.
809 -- Simplest way would be just divide i by b until it's smaller then b, but that would
810 -- be very slow! We are just slightly more clever.
811 integerLogBase :: Integer -> Integer -> Int
814 | otherwise = doDiv (i `div` (b^l)) l
816 -- Try squaring the base first to cut down the number of divisions.
817 l = 2 * integerLogBase (b*b) i
819 doDiv :: Integer -> Int -> Int
822 | otherwise = doDiv (x `div` b) (y+1)
827 %*********************************************************
829 \subsection{Floating point numeric primops}
831 %*********************************************************
833 Definitions of the boxed PrimOps; these will be
834 used in the case of partial applications, etc.
837 plusFloat, minusFloat, timesFloat, divideFloat :: Float -> Float -> Float
838 plusFloat (F# x) (F# y) = F# (plusFloat# x y)
839 minusFloat (F# x) (F# y) = F# (minusFloat# x y)
840 timesFloat (F# x) (F# y) = F# (timesFloat# x y)
841 divideFloat (F# x) (F# y) = F# (divideFloat# x y)
843 negateFloat :: Float -> Float
844 negateFloat (F# x) = F# (negateFloat# x)
846 gtFloat, geFloat, eqFloat, neFloat, ltFloat, leFloat :: Float -> Float -> Bool
847 gtFloat (F# x) (F# y) = gtFloat# x y
848 geFloat (F# x) (F# y) = geFloat# x y
849 eqFloat (F# x) (F# y) = eqFloat# x y
850 neFloat (F# x) (F# y) = neFloat# x y
851 ltFloat (F# x) (F# y) = ltFloat# x y
852 leFloat (F# x) (F# y) = leFloat# x y
854 float2Int :: Float -> Int
855 float2Int (F# x) = I# (float2Int# x)
857 int2Float :: Int -> Float
858 int2Float (I# x) = F# (int2Float# x)
860 expFloat, logFloat, sqrtFloat :: Float -> Float
861 sinFloat, cosFloat, tanFloat :: Float -> Float
862 asinFloat, acosFloat, atanFloat :: Float -> Float
863 sinhFloat, coshFloat, tanhFloat :: Float -> Float
864 expFloat (F# x) = F# (expFloat# x)
865 logFloat (F# x) = F# (logFloat# x)
866 sqrtFloat (F# x) = F# (sqrtFloat# x)
867 sinFloat (F# x) = F# (sinFloat# x)
868 cosFloat (F# x) = F# (cosFloat# x)
869 tanFloat (F# x) = F# (tanFloat# x)
870 asinFloat (F# x) = F# (asinFloat# x)
871 acosFloat (F# x) = F# (acosFloat# x)
872 atanFloat (F# x) = F# (atanFloat# x)
873 sinhFloat (F# x) = F# (sinhFloat# x)
874 coshFloat (F# x) = F# (coshFloat# x)
875 tanhFloat (F# x) = F# (tanhFloat# x)
877 powerFloat :: Float -> Float -> Float
878 powerFloat (F# x) (F# y) = F# (powerFloat# x y)
880 -- definitions of the boxed PrimOps; these will be
881 -- used in the case of partial applications, etc.
883 plusDouble, minusDouble, timesDouble, divideDouble :: Double -> Double -> Double
884 plusDouble (D# x) (D# y) = D# (x +## y)
885 minusDouble (D# x) (D# y) = D# (x -## y)
886 timesDouble (D# x) (D# y) = D# (x *## y)
887 divideDouble (D# x) (D# y) = D# (x /## y)
889 negateDouble :: Double -> Double
890 negateDouble (D# x) = D# (negateDouble# x)
892 gtDouble, geDouble, eqDouble, neDouble, leDouble, ltDouble :: Double -> Double -> Bool
893 gtDouble (D# x) (D# y) = x >## y
894 geDouble (D# x) (D# y) = x >=## y
895 eqDouble (D# x) (D# y) = x ==## y
896 neDouble (D# x) (D# y) = x /=## y
897 ltDouble (D# x) (D# y) = x <## y
898 leDouble (D# x) (D# y) = x <=## y
900 double2Int :: Double -> Int
901 double2Int (D# x) = I# (double2Int# x)
903 int2Double :: Int -> Double
904 int2Double (I# x) = D# (int2Double# x)
906 double2Float :: Double -> Float
907 double2Float (D# x) = F# (double2Float# x)
909 float2Double :: Float -> Double
910 float2Double (F# x) = D# (float2Double# x)
912 expDouble, logDouble, sqrtDouble :: Double -> Double
913 sinDouble, cosDouble, tanDouble :: Double -> Double
914 asinDouble, acosDouble, atanDouble :: Double -> Double
915 sinhDouble, coshDouble, tanhDouble :: Double -> Double
916 expDouble (D# x) = D# (expDouble# x)
917 logDouble (D# x) = D# (logDouble# x)
918 sqrtDouble (D# x) = D# (sqrtDouble# x)
919 sinDouble (D# x) = D# (sinDouble# x)
920 cosDouble (D# x) = D# (cosDouble# x)
921 tanDouble (D# x) = D# (tanDouble# x)
922 asinDouble (D# x) = D# (asinDouble# x)
923 acosDouble (D# x) = D# (acosDouble# x)
924 atanDouble (D# x) = D# (atanDouble# x)
925 sinhDouble (D# x) = D# (sinhDouble# x)
926 coshDouble (D# x) = D# (coshDouble# x)
927 tanhDouble (D# x) = D# (tanhDouble# x)
929 powerDouble :: Double -> Double -> Double
930 powerDouble (D# x) (D# y) = D# (x **## y)
934 foreign import ccall unsafe "isFloatNaN" isFloatNaN :: Float -> Int
935 foreign import ccall unsafe "isFloatInfinite" isFloatInfinite :: Float -> Int
936 foreign import ccall unsafe "isFloatDenormalized" isFloatDenormalized :: Float -> Int
937 foreign import ccall unsafe "isFloatNegativeZero" isFloatNegativeZero :: Float -> Int
940 foreign import ccall unsafe "isDoubleNaN" isDoubleNaN :: Double -> Int
941 foreign import ccall unsafe "isDoubleInfinite" isDoubleInfinite :: Double -> Int
942 foreign import ccall unsafe "isDoubleDenormalized" isDoubleDenormalized :: Double -> Int
943 foreign import ccall unsafe "isDoubleNegativeZero" isDoubleNegativeZero :: Double -> Int
946 %*********************************************************
948 \subsection{Coercion rules}
950 %*********************************************************
954 "fromIntegral/Int->Float" fromIntegral = int2Float
955 "fromIntegral/Int->Double" fromIntegral = int2Double
956 "realToFrac/Float->Float" realToFrac = id :: Float -> Float
957 "realToFrac/Float->Double" realToFrac = float2Double
958 "realToFrac/Double->Float" realToFrac = double2Float
959 "realToFrac/Double->Double" realToFrac = id :: Double -> Double
960 "realToFrac/Int->Double" realToFrac = int2Double -- See Note [realToFrac int-to-float]
961 "realToFrac/Int->Float" realToFrac = int2Float -- ..ditto
965 Note [realToFrac int-to-float]
966 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
967 Don found that the RULES for realToFrac/Int->Double and simliarly
968 Float made a huge difference to some stream-fusion programs. Here's
971 import Data.Array.Vector
976 let c = replicateU n (2::Double)
977 a = mapU realToFrac (enumFromToU 0 (n-1) ) :: UArr Double
978 print (sumU (zipWithU (*) c a))
980 Without the RULE we get this loop body:
982 case $wtoRational sc_sY4 of ww_aM7 { (# ww1_aM9, ww2_aMa #) ->
983 case $wfromRat ww1_aM9 ww2_aMa of tpl_X1P { D# ipv_sW3 ->
987 (+## sc2_sY6 (*## 2.0 ipv_sW3))
994 (+## sc2_sXV (*## 2.0 (int2Double# sc_sXT)))
996 The running time of the program goes from 120 seconds to 0.198 seconds
997 with the native backend, and 0.143 seconds with the C backend.
999 A few more details in Trac #2251, and the patch message
1000 "Add RULES for realToFrac from Int".
1002 %*********************************************************
1006 %*********************************************************
1009 showSignedFloat :: (RealFloat a)
1010 => (a -> ShowS) -- ^ a function that can show unsigned values
1011 -> Int -- ^ the precedence of the enclosing context
1012 -> a -- ^ the value to show
1014 showSignedFloat showPos p x
1015 | x < 0 || isNegativeZero x
1016 = showParen (p > 6) (showChar '-' . showPos (-x))
1017 | otherwise = showPos x
1020 We need to prevent over/underflow of the exponent in encodeFloat when
1021 called from scaleFloat, hence we clamp the scaling parameter.
1022 We must have a large enough range to cover the maximum difference of
1023 exponents returned by decodeFloat.
1025 clamp :: Int -> Int -> Int
1026 clamp bd k = max (-bd) (min bd k)