fromInteger i = F# (floatFromInteger i)
instance Real Float where
- toRational x | isInfinite x = if x < 0 then -infinity else infinity
- | isNaN x = notANumber
- | isNegativeZero x = negativeZero
- | otherwise = (m%1)*(b%1)^^n
+ toRational x = (m%1)*(b%1)^^n
where (m,n) = decodeFloat x
b = floatRadix x
instance Real Double where
- toRational x | isInfinite x = if x < 0 then -infinity else infinity
- | isNaN x = notANumber
- | isNegativeZero x = negativeZero
- | otherwise = (m%1)*(b%1)^^n
+ toRational x = (m%1)*(b%1)^^n
where (m,n) = decodeFloat x
b = floatRadix x
fromRat (n :% d) | n > 0 = fromRat' (n :% d)
| n < 0 = - fromRat' ((-n) :% d)
- | d < 0 = 0/(-1) -- -0.0
| otherwise = encodeFloat 0 0 -- Zero
-- Conversion process:
ratioPrec = 7 -- Precedence of ':%' constructor
ratioPrec1 = ratioPrec + 1
-infinity, notANumber, negativeZero :: Rational
+infinity, notANumber :: Rational
infinity = 1 :% 0
notANumber = 0 :% 0
-negativeZero = 0 :% (-1)
-- Use :%, not % for Inf/NaN; the latter would
-- immediately lead to a runtime error, because it normalises.