1 module PreludeCore ( Float(..) ) where
3 #include "../includes/ieee-flpt.h"
11 import Prel ( (^), (^^), otherwise )
12 import PS ( _PackedString, _unpackPS )
14 import TyComplex -- for pragmas only
16 -- definitions of the boxed PrimOps; these will be
17 -- used in the case of partial applications, etc.
19 plusFloat (F# x) (F# y) = F# (plusFloat# x y)
20 minusFloat (F# x) (F# y) = F# (minusFloat# x y)
21 timesFloat (F# x) (F# y) = F# (timesFloat# x y)
22 divideFloat (F# x) (F# y) = F# (divideFloat# x y)
23 negateFloat (F# x) = F# (negateFloat# x)
25 gtFloat (F# x) (F# y) = gtFloat# x y
26 geFloat (F# x) (F# y) = geFloat# x y
27 eqFloat (F# x) (F# y) = eqFloat# x y
28 neFloat (F# x) (F# y) = neFloat# x y
29 ltFloat (F# x) (F# y) = ltFloat# x y
30 leFloat (F# x) (F# y) = leFloat# x y
32 float2Int (F# x) = I# (float2Int# x)
33 int2Float (I# x) = F# (int2Float# x)
35 expFloat (F# x) = F# (expFloat# x)
36 logFloat (F# x) = F# (logFloat# x)
37 sqrtFloat (F# x) = F# (sqrtFloat# x)
38 sinFloat (F# x) = F# (sinFloat# x)
39 cosFloat (F# x) = F# (cosFloat# x)
40 tanFloat (F# x) = F# (tanFloat# x)
41 asinFloat (F# x) = F# (asinFloat# x)
42 acosFloat (F# x) = F# (acosFloat# x)
43 atanFloat (F# x) = F# (atanFloat# x)
44 sinhFloat (F# x) = F# (sinhFloat# x)
45 coshFloat (F# x) = F# (coshFloat# x)
46 tanhFloat (F# x) = F# (tanhFloat# x)
48 powerFloat (F# x) (F# y) = F# (powerFloat# x y)
50 ---------------------------------------------------------------
52 instance Eq Float where
53 (==) x y = eqFloat x y
54 (/=) x y = neFloat x y
56 instance Ord Float where
57 (<=) x y = leFloat x y
59 (>=) x y = geFloat x y
62 max a b = case _tagCmp a b of { _LT -> b; _EQ -> a; _GT -> a }
63 min a b = case _tagCmp a b of { _LT -> a; _EQ -> a; _GT -> b }
65 _tagCmp (F# a#) (F# b#)
66 = if (eqFloat# a# b#) then _EQ
67 else if (ltFloat# a# b#) then _LT else _GT
69 instance Num Float where
70 (+) x y = plusFloat x y
71 (-) x y = minusFloat x y
72 negate x = negateFloat x
73 (*) x y = timesFloat x y
75 | otherwise = negateFloat x
80 fromInteger n = encodeFloat n 0
81 fromInt i = int2Float i
83 instance Real Float where
84 toRational x = (m%1)*(b%1)^^n -- i.e., realFloatToRational x
85 where (m,n) = decodeFloat x
88 instance Fractional Float where
89 (/) x y = divideFloat x y
90 fromRational x = fromRationalX x -- ORIG: rationalToRealFloat x
92 instance Floating Float where
93 pi = 3.141592653589793238
106 (**) x y = powerFloat x y
108 {- WAS: but not all machines have these in their math library:
113 asinh x = log (x + sqrt (1+x*x))
114 acosh x = log (x + (x+1) * sqrt ((x-1)/(x+1)))
115 atanh x = log ((x+1) / sqrt (1 - x*x))
117 instance RealFrac Float where
118 properFraction x = _properFraction x
120 -- just call the versions in Core.hs
121 truncate x = _truncate x
123 ceiling x = _ceiling x
126 instance RealFloat Float where
127 floatRadix _ = FLT_RADIX -- from float.h
128 floatDigits _ = FLT_MANT_DIG -- ditto
129 floatRange _ = (FLT_MIN_EXP, FLT_MAX_EXP) -- ditto
132 = case decodeFloat# f# of
133 _ReturnIntAndGMP exp# a# s# d# ->
134 (J# a# s# d#, I# exp#)
136 encodeFloat (J# a# s# d#) (I# e#)
137 = case encodeFloat# a# s# d# e# of { flt# -> F# flt# }
139 instance Enum Float where
140 {- *** RAW PRELUDE ***
141 enumFrom = numericEnumFrom
142 enumFromThen = numericEnumFromThen
144 enumFrom x = x : enumFrom (x `plusFloat` 1.0)
145 enumFromThen m n = en' m (n `minusFloat` m)
146 where en' m n = m : en' (m `plusFloat` n) n
148 instance Text Float where
149 readsPrec p x = readSigned readFloat x
150 showsPrec x = showSigned showFloat x
152 ---------------------------------------------------------------
153 instance _CCallable Float
154 instance _CReturnable Float