It's rather big!
\begin{code}
+{-# OPTIONS -fno-implicit-prelude -#include "cbits/floatExtreme.h" #-}
{-# OPTIONS -H20m #-}
+
#include "../includes/ieee-flpt.h"
+
\end{code}
\begin{code}
-{-# OPTIONS -fno-implicit-prelude #-}
-
module PrelNum where
-import {-# SOURCE #-} IOBase ( error )
-import PrelList
import PrelBase
import GHC
+import {-# SOURCE #-} IOBase ( error )
+import PrelList
+
+import ArrBase ( Array, array, (!) )
+import STBase ( unsafePerformPrimIO )
+import Ix ( Ix(..) )
+import Numeric
infixr 8 ^, ^^, **
-infixl 7 %, `quot`, `rem`, `div`, `mod`
+infixl 7 /, %, `quot`, `rem`, `div`, `mod`
\end{code}
showsPrec x = showSignedInteger x
showList = showList__ (showsPrec 0)
+instance Ix Integer where
+ range (m,n) = [m..n]
+ index b@(m,n) i
+ | inRange b i = fromInteger (i - m)
+ | otherwise = error "Integer.index: Index out of range."
+ inRange (m,n) i = m <= i && i <= n
+
integer_0, integer_1, integer_2, integer_m1 :: Integer
-integer_0 = 0; integer_1 = 1; integer_2 = 2; integer_m1 = -1
+integer_0 = int2Integer# 0#
+integer_1 = int2Integer# 1#
+integer_2 = int2Integer# 2#
+integer_m1 = int2Integer# (negateInt# 1#)
\end{code}
%*********************************************************
\begin{code}
+instance Eq Float where
+ (F# x) == (F# y) = x `eqFloat#` y
+
+instance Ord Float where
+ (F# x) `compare` (F# y) | x `ltFloat#` y = LT
+ | x `eqFloat#` y = EQ
+ | otherwise = GT
+
+ (F# x) < (F# y) = x `ltFloat#` y
+ (F# x) <= (F# y) = x `leFloat#` y
+ (F# x) >= (F# y) = x `geFloat#` y
+ (F# x) > (F# y) = x `gtFloat#` y
+
instance Num Float where
(+) x y = plusFloat x y
(-) x y = minusFloat x y
instance Fractional Float where
(/) x y = divideFloat x y
- fromRational x = fromRational__ x
+ fromRational x = fromRat x
recip x = 1.0 / x
instance Floating Float where
scaleFloat k x = case decodeFloat x of
(m,n) -> encodeFloat m (n+k)
+ isNaN x =
+ (0::Int) /= unsafePerformPrimIO (_ccall_ isFloatNaN x) {- a _pure_function! -}
+ isInfinite x =
+ (0::Int) /= unsafePerformPrimIO (_ccall_ isFloatInfinite x) {- ditto! -}
+ isDenormalized x =
+ (0::Int) /= unsafePerformPrimIO (_ccall_ isFloatDenormalized x) -- ..
+ isNegativeZero x =
+ (0::Int) /= unsafePerformPrimIO (_ccall_ isFloatNegativeZero x) -- ...
+ isIEEE x = True
instance Show Float where
showsPrec x = showSigned showFloat x
%*********************************************************
\begin{code}
+instance Eq Double where
+ (D# x) == (D# y) = x ==## y
+
+instance Ord Double where
+ (D# x) `compare` (D# y) | x <## y = LT
+ | x ==## y = EQ
+ | otherwise = GT
+
+ (D# x) < (D# y) = x <## y
+ (D# x) <= (D# y) = x <=## y
+ (D# x) >= (D# y) = x >=## y
+ (D# x) > (D# y) = x >## y
+
instance Num Double where
(+) x y = plusDouble x y
(-) x y = minusDouble x y
instance Fractional Double where
(/) x y = divideDouble x y
- fromRational x = fromRational__ x
+ fromRational x = fromRat x
recip x = 1.0 / x
instance Floating Double where
scaleFloat k x = case decodeFloat x of
(m,n) -> encodeFloat m (n+k)
+ isNaN x =
+ (0::Int) /= unsafePerformPrimIO (_ccall_ isDoubleNaN x) {- a _pure_function! -}
+ isInfinite x =
+ (0::Int) /= unsafePerformPrimIO (_ccall_ isDoubleInfinite x) {- ditto -}
+ isDenormalized x =
+ (0::Int) /= unsafePerformPrimIO (_ccall_ isDoubleDenormalized x) -- ..
+ isNegativeZero x =
+ (0::Int) /= unsafePerformPrimIO (_ccall_ isDoubleNegativeZero x) -- ...
+ isIEEE x = True
instance Show Double where
showsPrec x = showSigned showFloat x
%*********************************************************
\begin{code}
-data (Integral a) => Ratio a = a :% a deriving (Eq)
+data (Integral a) => Ratio a = !a :% !a deriving (Eq)
type Rational = Ratio Integer
\end{code}
numerator, denominator :: (Integral a) => Ratio a -> a
approxRational :: (RealFrac a) => a -> a -> Rational
+\end{code}
+
+\tr{reduce} is a subsidiary function used only in this module .
+It normalises a ratio by dividing both numerator and denominator by
+their greatest common divisor.
-reduce _ 0 = error "{Ratio.%}: zero denominator"
+\begin{code}
+reduce x 0 = error "{Ratio.%}: zero denominator"
reduce x y = (x `quot` d) :% (y `quot` d)
where d = gcd x y
+\end{code}
+\begin{code}
x % y = reduce (x * signum y) (abs y)
numerator (x:%y) = x
| x > 0 = simplest' n d n' d'
| y < 0 = - simplest' (-n') d' (-n) d
| otherwise = 0 :% 1
- where xr@(n:%d) = toRational x
- (n':%d') = toRational y
+ where xr = toRational x
+ n = numerator xr
+ d = denominator xr
+ nd' = toRational y
+ n' = numerator nd'
+ d' = denominator nd'
simplest' n d n' d' -- assumes 0 < n%d < n'%d'
| r == 0 = q :% 1
| otherwise = (q*n''+d'') :% n''
where (q,r) = quotRem n d
(q',r') = quotRem n' d'
- (n'':%d'') = simplest' d' r' d r
+ nd'' = simplest' d' r' d r
+ n'' = numerator nd''
+ d'' = denominator nd''
\end{code}
instance (Integral a) => Num (Ratio a) where
(x:%y) + (x':%y') = reduce (x*y' + x'*y) (y*y')
+ (x:%y) - (x':%y') = reduce (x*y' - x'*y) (y*y')
(x:%y) * (x':%y') = reduce (x * x') (y * y')
negate (x:%y) = (-x) :% y
abs (x:%y) = abs x :% y
(shows x . showString " % " . shows y)
\end{code}
-{-
-[In response to a request by simonpj, Joe Fasel writes:]
-
-A quite reasonable request! This code was added to the Prelude just
-before the 1.2 release, when Lennart, working with an early version
-of hbi, noticed that (read . show) was not the identity for
-floating-point numbers. (There was a one-bit error about half the time.)
-The original version of the conversion function was in fact simply
-a floating-point divide, as you suggest above. The new version is,
-I grant you, somewhat denser.
-
-How's this?
-
-Joe
--}
-
\begin{code}
-{-# GENERATE_SPECS fromRational__ a{Double#,Double} #-}
-fromRational__ :: (RealFloat a) => Rational -> a
-fromRational__ x = x'
- where x' = f e
-
--- If the exponent of the nearest floating-point number to x
--- is e, then the significand is the integer nearest xb^(-e),
--- where b is the floating-point radix. We start with a good
--- guess for e, and if it is correct, the exponent of the
--- floating-point number we construct will again be e. If
--- not, one more iteration is needed.
-
- f e = if e' == e then y else f e'
- where y = encodeFloat (round (x * (1 % b)^^e)) e
- (_,e') = decodeFloat y
- b = floatRadix x'
-
--- We obtain a trial exponent by doing a floating-point
--- division of x's numerator by its denominator. The
--- result of this division may not itself be the ultimate
--- result, because of an accumulation of three rounding
--- errors.
-
- (s,e) = decodeFloat (fromInteger (numerator x) `asTypeOf` x'
- / fromInteger (denominator x))
-\end{code}
-
-
-%*********************************************************
-%* *
-\subsection{Showing numbers}
-%* *
-%*********************************************************
-
-\begin{code}
-showInteger n r
- = case quotRem n 10 of { (n', d) ->
- case (chr (ord_0 + fromIntegral d)) of { C# c# -> -- stricter than necessary
- let
- r' = C# c# : r
- in
- if n' == 0 then r' else showInteger n' r'
- }}
-
-showSigned :: (Real a) => (a -> ShowS) -> Int -> a -> ShowS
-showSigned showPos p x = if x < 0 then showParen (p > 6)
- (showChar '-' . showPos (-x))
- else showPos x
-
showSignedInteger :: Int -> Integer -> ShowS
showSignedInteger p n r
= -- from HBC version; support code follows
jtos' (n `quot` 10) (chr (fromInteger (n `rem` 10 + ord_0)) : cs)
\end{code}
-The functions showFloat below uses rational arithmetic
-to insure correct conversion between the floating-point radix and
-decimal. It is often possible to use a higher-precision floating-
-point type to obtain the same results.
-
-\begin{code}
-{-# GENERATE_SPECS showFloat a{Double#,Double} #-}
-showFloat:: (RealFloat a) => a -> ShowS
-showFloat x =
- if x == 0 then showString ("0." ++ take (m-1) zeros)
- else if e >= m-1 || e < 0 then showSci else showFix
- where
- showFix = showString whole . showChar '.' . showString frac
- where (whole,frac) = splitAt (e+1) (show sig)
- showSci = showChar d . showChar '.' . showString frac
- . showChar 'e' . shows e
- where (d:frac) = show sig
- (m, sig, e) = if b == 10 then (w, s, n+w-1)
- else (m', sig', e' )
- m' = ceiling
- ((fromInt w * log (fromInteger b)) / log 10 :: Double)
- + 1
- (sig', e') = if sig1 >= 10^m' then (round (t/10), e1+1)
- else if sig1 < 10^(m'-1) then (round (t*10), e1-1)
- else (sig1, e1 )
- sig1 = round t
- t = s%1 * (b%1)^^n * 10^^(m'-e1-1)
- e1 = floor (logBase 10 x)
- (s, n) = decodeFloat x
- b = floatRadix x
- w = floatDigits x
-
-zeros = repeat '0'
-\end{code}
-
@showRational@ converts a Rational to a string that looks like a
floating point number, but without converting to any floating type
(because of the possible overflow).