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 ArrBase ( Array, array, (!) )
+import STBase ( unsafePerformPrimIO )
import Ix ( Ix(..) )
import GHC
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
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
their greatest common divisor.
\begin{code}
-reduce _ 0 = error "{Ratio.%}: zero denominator"
+reduce x 0 = error "{Ratio.%}: zero denominator"
reduce x y = (x `quot` d) :% (y `quot` d)
where d = gcd x y
\end{code}
| 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