X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=GHC%2FFloat.lhs;h=fc52dd96487f05fa519d9948bd5d97086658e2f9;hb=ba2fa1aad38f8dab23977beb21d0cad826e5b564;hp=c31be1b9d5de8b9713882dd4b4ec56e52424429d;hpb=1523de9c11a0663e538f00679f6a4a682295b2de;p=ghc-base.git diff --git a/GHC/Float.lhs b/GHC/Float.lhs index c31be1b..fc52dd9 100644 --- a/GHC/Float.lhs +++ b/GHC/Float.lhs @@ -131,9 +131,20 @@ class (RealFrac a, Floating a) => RealFloat a where significand x = encodeFloat m (negate (floatDigits x)) where (m,_) = decodeFloat x - scaleFloat k x = encodeFloat m (n+k) + scaleFloat k x = encodeFloat m (n + clamp b k) where (m,n) = decodeFloat x - + (l,h) = floatRange x + d = floatDigits x + b = h - l + 4*d + -- n+k may overflow, which would lead + -- to wrong results, hence we clamp the + -- scaling parameter. + -- If n + k would be larger than h, + -- n + clamp b k must be too, simliar + -- for smaller than l - d. + -- Add a little extra to keep clear + -- from the boundary cases. + atan2 y x | x > 0 = atan (y/x) | x == 0 && y > 0 = pi/2 @@ -268,7 +279,9 @@ instance RealFloat Float where (m,_) -> encodeFloat m (negate (floatDigits x)) scaleFloat k x = case decodeFloat x of - (m,n) -> encodeFloat m (n+k) + (m,n) -> encodeFloat m (n + clamp bf k) + where bf = FLT_MAX_EXP - (FLT_MIN_EXP) + 4*FLT_MANT_DIG + isNaN x = 0 /= isFloatNaN x isInfinite x = 0 /= isFloatInfinite x isDenormalized x = 0 /= isFloatDenormalized x @@ -395,7 +408,8 @@ instance RealFloat Double where (m,_) -> encodeFloat m (negate (floatDigits x)) scaleFloat k x = case decodeFloat x of - (m,n) -> encodeFloat m (n+k) + (m,n) -> encodeFloat m (n + clamp bd k) + where bd = DBL_MAX_EXP - (DBL_MIN_EXP) + 4*DBL_MANT_DIG isNaN x = 0 /= isDoubleNaN x isInfinite x = 0 /= isDoubleInfinite x @@ -971,3 +985,12 @@ showSignedFloat showPos p x = showParen (p > 6) (showChar '-' . showPos (-x)) | otherwise = showPos x \end{code} + +We need to prevent over/underflow of the exponent in encodeFloat when +called from scaleFloat, hence we clamp the scaling parameter. +We must have a large enough range to cover the maximum difference of +exponents returned by decodeFloat. +\begin{code} +clamp :: Int -> Int -> Int +clamp bd k = max (-bd) (min bd k) +\end{code}