25 import List ( reverse, dropWhile, take, drop, repeat, (++), head, tail )
26 import Prel ( (&&), (^^), (^), not, otherwise, asTypeOf, const, (.), atan2, maxInt )
27 import PS ( _PackedString, _unpackPS )
32 -----------------------------------------------------------------
33 -- some *** NON-STANDARD *** constants (to help compiling Cls.hs)
36 {-# GENERATE_SPECS __i0 a{Int#,Double#,Int,Integer,Double,Complex(Double#),Complex(Double),Rational} #-}
38 {-# GENERATE_SPECS __i1 a{Int#,Double#,Int,Integer,Double,Complex(Double#),Complex(Double),Rational} #-}
40 {-# GENERATE_SPECS __i2 a{Int#,Double#,Int,Integer,Double,Complex(Double#),Complex(Double),Rational} #-}
42 {-# GENERATE_SPECS __im1 a{Int#,Double#,Int,Integer,Double,Complex(Double#),Complex(Double),Rational} #-}
44 {-# GENERATE_SPECS __i8 a{Int#,Double#,Int,Integer,Double,Complex(Double#),Complex(Double),Rational} #-}
46 {-# GENERATE_SPECS __i10 a{Int#,Double#,Int,Integer,Double,Complex(Double#),Complex(Double),Rational} #-}
48 {-# GENERATE_SPECS __i16 a{Int#,Double#,Int,Integer,Double,Complex(Double#),Complex(Double),Rational} #-}
59 {-# GENERATE_SPECS __rhalf a{Double#,Double,Complex(Double#),Complex(Double),Rational} #-}
60 __rhalf :: Fractional a => a
61 __rhalf = fromRational (__i1:%__i2)
64 -- bits of PreludeCore that aren't classes, instances, etc.
67 [In response to a request by simonpj, Joe Fasel writes:]
69 A quite reasonable request! This code was added to the Prelude just
70 before the 1.2 release, when Lennart, working with an early version
71 of hbi, noticed that (read . show) was not the identity for
72 floating-point numbers. (There was a one-bit error about half the time.)
73 The original version of the conversion function was in fact simply
74 a floating-point divide, as you suggest above. The new version is,
75 I grant you, somewhat denser.
82 {-# GENERATE_SPECS _fromRational a{Double#,Double} #-}
83 _fromRational :: (RealFloat a) => Rational -> a
87 -- If the exponent of the nearest floating-point number to x
88 -- is e, then the significand is the integer nearest xb^(-e),
89 -- where b is the floating-point radix. We start with a good
90 -- guess for e, and if it is correct, the exponent of the
91 -- floating-point number we construct will again be e. If
92 -- not, one more iteration is needed.
94 f e = if e' == e then y else f e'
95 where y = encodeFloat (round (x * (__i1 % b)^^e)) e
96 (_,e') = decodeFloat y
99 -- We obtain a trial exponent by doing a floating-point
100 -- division of x's numerator by its denominator. The
101 -- result of this division may not itself be the ultimate
102 -- result, because of an accumulation of three rounding
105 (s,e) = decodeFloat (fromInteger (numerator x) `asTypeOf` x'
106 / fromInteger (denominator x))
111 -- Another version of _fromRational which is floating around ...
112 -- Any idea what is the true story ? (PS)
114 _fromRational :: (RealFloat a) => Rational -> a
117 h = ceiling (huge `asTypeOf` x)
118 b = toInteger (floatRadix x)
122 {--} _trace (shows e0 ('/' : shows r' ('/' : shows h "\n"))) (
123 let d = denominator r'
126 let e = integerLogBase b (d `div` h) + 1
127 in fromRat (e0-e) (n % (d `div` (b^e)))
128 else if abs n > h then
129 let e = integerLogBase b (abs n `div` h) + 1
130 in fromRat (e0+e) ((n `div` (b^e)) % d)
132 scaleFloat e0 (rationalToRealFloat r')
133 -- now that we know things are in-bounds,
134 -- we use the "old" Prelude code.
138 -- Compute the discrete log of i in base b.
139 -- Simplest way would be just divide i by b until it's smaller then b, but that would
140 -- be very slow! We are just slightly more clever.
141 integerLogBase :: Integer -> Integer -> Int
146 -- Try squaring the base first to cut down the number of divisions.
147 let l = 2 * integerLogBase (b*b) i
148 doDiv :: Integer -> Int -> Int
149 doDiv i l = if i < b then l else doDiv (i `div` b) (l+1)
150 in doDiv (i `div` (b^l)) l
155 -- Compute smallest and largest floating point values.
156 tiny :: (RealFloat a) => a
158 let (l, _) = floatRange x
159 x = encodeFloat 1 (l-1)
162 huge :: (RealFloat a) => a
164 let (_, u) = floatRange x
166 x = encodeFloat (floatRadix x ^ d - 1) (u - d)
171 -------------------------------------------------------------------------
172 -- from/by Lennart, 94/09/26
174 -- Convert a Rational to a string that looks like a floating point number,
175 -- but without converting to any floating type (because of the possible overflow).
176 _showRational :: Int -> Rational -> String
181 let (r', e) = normalize r
184 startExpExp = 4 :: Int
186 -- make sure 1 <= r < 10
187 normalize :: Rational -> (Rational, Int)
188 normalize r = if r < __i1 then
189 case norm startExpExp (__i1 / r) 0 of (r', e) -> (__i10 / r', -e-1)
192 where norm :: Int -> Rational -> Int -> (Rational, Int)
193 -- Invariant: r*10^e == original r
198 in if r >= tn then norm ee (r/tn) (e+n) else norm (ee-1) r e
201 drop0 (c:cs) = c : reverse (dropWhile (=='0') (reverse cs))
203 prR :: Int -> Rational -> Int -> String
204 prR n r e | r < __i1 = prR n (r*__i10) (e-1) -- final adjustment
205 prR n r e | r >= __i10 = prR n (r/__i10) (e+1)
207 let s = show ((round (r * __i10^n))::Integer)
209 in if e > 0 && e < 8 then
210 take e s ++ "." ++ drop0 (drop e s)
211 else if e <= 0 && e > -3 then
212 "0." ++ take (-e) (repeat '0') ++ drop0 s
214 head s : "."++ drop0 (tail s) ++ "e" ++ show e0
216 -----------------------------------------------------------------
218 {-# GENERATE_SPECS _readList a #-}
219 _readList :: ReadS a -> ReadS [a]
221 _readList readx = readParen False (\r -> [pr | ("[",s) <- lex r,
223 where readl s = [([],t) | ("]",t) <- lex s] ++
224 [(x:xs,u) | (x,t) <- readx s,
226 readl2 s = [([],t) | ("]",t) <- lex s] ++
227 [(x:xs,v) | (",",t) <- lex s,
231 {-# GENERATE_SPECS _showList a #-}
232 _showList :: (a -> ShowS) -> [a] -> ShowS
234 _showList showx [] = showString "[]"
235 _showList showx (x:xs)
236 = showChar '[' . showx x . showl xs
238 where showl [] = showChar ']'
239 showl (x:xs) = showString ", " . showx x . showl xs