From 1197a106aa33ef1af6402dd4598af5ee4c11a6f4 Mon Sep 17 00:00:00 2001 From: Simon Marlow Date: Thu, 11 Feb 2010 10:19:55 +0000 Subject: [PATCH] Handle NaN, -Infinity and Infinity in the toRational for Float/Double (#3676) --- GHC/Float.lhs | 11 +++++++++-- GHC/Real.lhs | 3 ++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/GHC/Float.lhs b/GHC/Float.lhs index dbb556f..61d009f 100644 --- a/GHC/Float.lhs +++ b/GHC/Float.lhs @@ -171,7 +171,10 @@ instance Num Float where fromInteger i = F# (floatFromInteger i) instance Real Float where - toRational x = (m%1)*(b%1)^^n + toRational x | isInfinite x = if x < 0 then -infinity else infinity + | isNaN x = notANumber + | isNegativeZero x = negativeZero + | otherwise = (m%1)*(b%1)^^n where (m,n) = decodeFloat x b = floatRadix x @@ -303,7 +306,10 @@ instance Num Double where instance Real Double where - toRational x = (m%1)*(b%1)^^n + toRational x | isInfinite x = if x < 0 then -infinity else infinity + | isNaN x = notANumber + | isNegativeZero x = negativeZero + | otherwise = (m%1)*(b%1)^^n where (m,n) = decodeFloat x b = floatRadix x @@ -713,6 +719,7 @@ fromRat (n :% 0) | n > 0 = 1/0 -- +Infinity 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: diff --git a/GHC/Real.lhs b/GHC/Real.lhs index 971f276..71d35e6 100644 --- a/GHC/Real.lhs +++ b/GHC/Real.lhs @@ -54,9 +54,10 @@ ratioPrec, ratioPrec1 :: Int ratioPrec = 7 -- Precedence of ':%' constructor ratioPrec1 = ratioPrec + 1 -infinity, notANumber :: Rational +infinity, notANumber, negativeZero :: 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. -- 1.7.10.4