1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
5 -- Copyright : (c) The University of Glasgow 2002
6 -- License : BSD-style (see the file libraries/core/LICENSE)
8 -- Maintainer : libraries@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
12 -- $Id: Numeric.hs,v 1.5 2002/02/12 10:52:18 simonmar Exp $
14 -- Odds and ends, mostly functions for reading and showing
15 -- RealFloat-like kind of values.
17 -----------------------------------------------------------------------------
21 fromRat, -- :: (RealFloat a) => Rational -> a
22 showSigned, -- :: (Real a) => (a -> ShowS) -> Int -> a -> ShowS
23 readSigned, -- :: (Real a) => ReadS a -> ReadS a
25 readInt, -- :: (Integral a) => a -> (Char -> Bool)
26 -- -> (Char -> Int) -> ReadS a
27 readDec, -- :: (Integral a) => ReadS a
28 readOct, -- :: (Integral a) => ReadS a
29 readHex, -- :: (Integral a) => ReadS a
31 showInt, -- :: Integral a => a -> ShowS
32 showIntAtBase, -- :: Integral a => a -> (a -> Char) -> a -> ShowS
33 showHex, -- :: Integral a => a -> ShowS
34 showOct, -- :: Integral a => a -> ShowS
35 showBin, -- :: Integral a => a -> ShowS
37 showEFloat, -- :: (RealFloat a) => Maybe Int -> a -> ShowS
38 showFFloat, -- :: (RealFloat a) => Maybe Int -> a -> ShowS
39 showGFloat, -- :: (RealFloat a) => Maybe Int -> a -> ShowS
40 showFloat, -- :: (RealFloat a) => a -> ShowS
41 readFloat, -- :: (RealFloat a) => ReadS a
43 floatToDigits, -- :: (RealFloat a) => Integer -> a -> ([Int], Int)
44 lexDigits, -- :: ReadS String
50 #ifdef __GLASGOW_HASKELL__
64 #ifdef __GLASGOW_HASKELL__
65 showInt :: Integral a => a -> ShowS
67 | n < 0 = error "Numeric.showInt: can't show negative numbers"
71 | n < 10 = case unsafeChr (ord '0' + fromIntegral n) of
73 | otherwise = case unsafeChr (ord '0' + fromIntegral r) of
74 c@(C# _) -> go q (c:cs)
76 (q,r) = n `quotRem` 10
78 -- Controlling the format and precision of floats. The code that
79 -- implements the formatting itself is in @PrelNum@ to avoid
80 -- mutual module deps.
82 {-# SPECIALIZE showEFloat ::
83 Maybe Int -> Float -> ShowS,
84 Maybe Int -> Double -> ShowS #-}
85 {-# SPECIALIZE showFFloat ::
86 Maybe Int -> Float -> ShowS,
87 Maybe Int -> Double -> ShowS #-}
88 {-# SPECIALIZE showGFloat ::
89 Maybe Int -> Float -> ShowS,
90 Maybe Int -> Double -> ShowS #-}
92 showEFloat :: (RealFloat a) => Maybe Int -> a -> ShowS
93 showFFloat :: (RealFloat a) => Maybe Int -> a -> ShowS
94 showGFloat :: (RealFloat a) => Maybe Int -> a -> ShowS
96 showEFloat d x = showString (formatRealFloat FFExponent d x)
97 showFFloat d x = showString (formatRealFloat FFFixed d x)
98 showGFloat d x = showString (formatRealFloat FFGeneric d x)
102 -- This converts a rational to a floating. This should be used in the
103 -- Fractional instances of Float and Double.
105 fromRat :: (RealFloat a) => Rational -> a
107 if x == 0 then encodeFloat 0 0 -- Handle exceptional cases
108 else if x < 0 then - fromRat' (-x) -- first.
111 -- Conversion process:
112 -- Scale the rational number by the RealFloat base until
113 -- it lies in the range of the mantissa (as used by decodeFloat/encodeFloat).
114 -- Then round the rational to an Integer and encode it with the exponent
115 -- that we got from the scaling.
116 -- To speed up the scaling process we compute the log2 of the number to get
117 -- a first guess of the exponent.
118 fromRat' :: (RealFloat a) => Rational -> a
120 where b = floatRadix r
122 (minExp0, _) = floatRange r
123 minExp = minExp0 - p -- the real minimum exponent
124 xMin = toRational (expt b (p-1))
125 xMax = toRational (expt b p)
126 p0 = (integerLogBase b (numerator x) -
127 integerLogBase b (denominator x) - p) `max` minExp
128 f = if p0 < 0 then 1 % expt b (-p0) else expt b p0 % 1
129 (x', p') = scaleRat (toRational b) minExp xMin xMax p0 (x / f)
130 r = encodeFloat (round x') p'
132 -- Scale x until xMin <= x < xMax, or p (the exponent) <= minExp.
133 scaleRat :: Rational -> Int -> Rational -> Rational ->
134 Int -> Rational -> (Rational, Int)
135 scaleRat b minExp xMin xMax p x =
138 else if x >= xMax then
139 scaleRat b minExp xMin xMax (p+1) (x/b)
140 else if x < xMin then
141 scaleRat b minExp xMin xMax (p-1) (x*b)
145 -- Exponentiation with a cache for the most common numbers.
148 expt :: Integer -> Int -> Integer
150 if base == 2 && n >= minExpt && n <= maxExpt then
155 expts :: Array Int Integer
156 expts = array (minExpt,maxExpt) [(n,2^n) | n <- [minExpt .. maxExpt]]
158 -- Compute the (floor of the) log of i in base b.
159 -- Simplest way would be just divide i by b until it's smaller then b,
160 -- but that would be very slow! We are just slightly more clever.
161 integerLogBase :: Integer -> Integer -> Int
166 -- Try squaring the base first to cut down the number of divisions.
167 let l = 2 * integerLogBase (b*b) i
168 doDiv :: Integer -> Int -> Int
169 doDiv i l = if i < b then l else doDiv (i `div` b) (l+1)
170 in doDiv (i `div` (b^l)) l
173 -- Misc utilities to show integers and floats
175 showEFloat :: (RealFloat a) => Maybe Int -> a -> ShowS
176 showFFloat :: (RealFloat a) => Maybe Int -> a -> ShowS
177 showGFloat :: (RealFloat a) => Maybe Int -> a -> ShowS
178 showFloat :: (RealFloat a) => a -> ShowS
180 showEFloat d x = showString (formatRealFloat FFExponent d x)
181 showFFloat d x = showString (formatRealFloat FFFixed d x)
182 showGFloat d x = showString (formatRealFloat FFGeneric d x)
183 showFloat = showGFloat Nothing
185 -- These are the format types. This type is not exported.
187 data FFFormat = FFExponent | FFFixed | FFGeneric
189 formatRealFloat :: (RealFloat a) => FFFormat -> Maybe Int -> a -> String
190 formatRealFloat fmt decs x = s
194 else if isInfinite x then
195 if x < 0 then "-Infinity" else "Infinity"
196 else if x < 0 || isNegativeZero x then
197 '-' : doFmt fmt (floatToDigits (toInteger base) (-x))
199 doFmt fmt (floatToDigits (toInteger base) x)
201 let ds = map intToDigit is
204 doFmt (if e < 0 || e > 7 then FFExponent else FFFixed)
211 [d] -> d : ".0e" ++ show (e-1)
212 d:ds -> d : '.' : ds ++ 'e':show (e-1)
214 let dec' = max dec 1 in
216 [0] -> '0':'.':take dec' (repeat '0') ++ "e0"
218 let (ei, is') = roundTo base (dec'+1) is
219 d:ds = map intToDigit
220 (if ei > 0 then init is' else is')
221 in d:'.':ds ++ "e" ++ show (e-1+ei)
225 let f 0 s ds = mk0 s ++ "." ++ mk0 ds
226 f n s "" = f (n-1) (s++"0") ""
227 f n s (d:ds) = f (n-1) (s++[d]) ds
232 let dec' = max dec 0 in
234 let (ei, is') = roundTo base (dec' + e) is
235 (ls, rs) = splitAt (e+ei) (map intToDigit is')
236 in (if null ls then "0" else ls) ++
237 (if null rs then "" else '.' : rs)
239 let (ei, is') = roundTo base dec'
240 (replicate (-e) 0 ++ is)
241 d : ds = map intToDigit
242 (if ei > 0 then is' else 0:is')
245 roundTo :: Int -> Int -> [Int] -> (Int, [Int])
246 roundTo base d is = case f d is of
248 (1, is) -> (1, 1 : is)
249 where b2 = base `div` 2
250 f n [] = (0, replicate n 0)
251 f 0 (i:_) = (if i >= b2 then 1 else 0, [])
253 let (c, ds) = f (d-1) is
255 in if i' == base then (1, 0:ds) else (0, i':ds)
258 -- Based on "Printing Floating-Point Numbers Quickly and Accurately"
259 -- by R.G. Burger and R. K. Dybvig, in PLDI 96.
260 -- This version uses a much slower logarithm estimator. It should be improved.
262 -- This function returns a list of digits (Ints in [0..base-1]) and an
265 floatToDigits :: (RealFloat a) => Integer -> a -> ([Int], Int)
267 floatToDigits _ 0 = ([0], 0)
268 floatToDigits base x =
269 let (f0, e0) = decodeFloat x
270 (minExp0, _) = floatRange x
273 minExp = minExp0 - p -- the real minimum exponent
274 -- Haskell requires that f be adjusted so denormalized numbers
275 -- will have an impossibly low exponent. Adjust for this.
276 (f, e) = let n = minExp - e0
277 in if n > 0 then (f0 `div` (b^n), e0+n) else (f0, e0)
283 (f*be*b*2, 2*b, be*b, b)
287 if e > minExp && f == b^(p-1) then
288 (f*b*2, b^(-e+1)*2, b, 1)
290 (f*2, b^(-e)*2, 1, 1)
293 if b==2 && base==10 then
294 -- logBase 10 2 is slightly bigger than 3/10 so
295 -- the following will err on the low side. Ignoring
296 -- the fraction will make it err even more.
297 -- Haskell promises that p-1 <= logBase b f < p.
298 (p - 1 + e0) * 3 `div` 10
300 ceiling ((log (fromInteger (f+1)) +
301 fromIntegral e * log (fromInteger b)) /
302 log (fromInteger base))
305 if r + mUp <= expt base n * s then n else fixup (n+1)
307 if expt base (-n) * (r + mUp) <= s then n
311 gen ds rn sN mUpN mDnN =
312 let (dn, rn') = (rn * base) `divMod` sN
315 in case (rn' < mDnN', rn' + mUpN' > sN) of
316 (True, False) -> dn : ds
317 (False, True) -> dn+1 : ds
318 (True, True) -> if rn' * 2 < sN then dn : ds else dn+1 : ds
319 (False, False) -> gen (dn:ds) rn' sN mUpN' mDnN'
322 gen [] r (s * expt base k) mUp mDn
324 let bk = expt base (-k)
325 in gen [] (r * bk) s (mUp * bk) (mDn * bk)
326 in (map fromIntegral (reverse rds), k)
329 -- ---------------------------------------------------------------------------
330 -- Integer printing functions
332 showIntAtBase :: Integral a => a -> (a -> Char) -> a -> ShowS
333 showIntAtBase base toChr n r
334 | n < 0 = error ("Numeric.showIntAtBase: applied to negative number " ++ show n)
336 case quotRem n base of { (n', d) ->
338 seq c $ -- stricter than necessary
342 if n' == 0 then r' else showIntAtBase base toChr n' r'
345 showHex :: Integral a => a -> ShowS
348 showIntAtBase 16 (toChrHex) n r
351 | d < 10 = chr (ord '0' + fromIntegral d)
352 | otherwise = chr (ord 'a' + fromIntegral (d - 10))
354 showOct :: Integral a => a -> ShowS
357 showIntAtBase 8 (toChrOct) n r
358 where toChrOct d = chr (ord '0' + fromIntegral d)
360 showBin :: Integral a => a -> ShowS
363 showIntAtBase 2 (toChrOct) n r
364 where toChrOct d = chr (ord '0' + fromIntegral d)