[commit: base] master: Use round-to-even when formatting floats (#5963) (86a8756)

Paolo Capriotti <[email protected]>
Newsgroups gmane.comp.lang.haskell.cvs.libraries
Message-ID <[email protected]>
Repository : ssh://darcs.haskell.org//srv/darcs/packages/base

On branch  : master

http://hackage.haskell.org/trac/ghc/changeset/86a8756db23b62346242916230736705c5053c55

>---------------------------------------------------------------

commit 86a8756db23b62346242916230736705c5053c55
Author: Paolo Capriotti <[email protected]>
Date:   Wed Jun 20 16:18:10 2012 +0100

    Use round-to-even when formatting floats (#5963)
    
    Patch by Lennart Augustsson.

>---------------------------------------------------------------

 GHC/Float.lhs |   13 +++++++------
 1 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/GHC/Float.lhs b/GHC/Float.lhs
index dfa2365..48e3d49 100644
--- a/GHC/Float.lhs
+++ b/GHC/Float.lhs
@@ -636,20 +636,21 @@ formatRealFloat fmt decs x
 
 roundTo :: Int -> Int -> [Int] -> (Int,[Int])
 roundTo base d is =
-  case f d is of
+  case f d True is of
     x@(0,_) -> x
     (1,xs)  -> (1, 1:xs)
     _       -> error "roundTo: bad Value"
  where
-  b2 = base `div` 2
+  b2 = base `quot` 2
 
-  f n []     = (0, replicate n 0)
-  f 0 (x:_)  = (if x >= b2 then 1 else 0, [])
-  f n (i:xs)
+  f n _ []     = (0, replicate n 0)
+  f 0 e (x:xs) | x == b2 && e && all (== 0) xs = (0, [])   -- Round to even when at exactly half the base
+               | otherwise = (if x >= b2 then 1 else 0, [])
+  f n _ (i:xs)
      | i' == base = (1,0:ds)
      | otherwise  = (0,i':ds)
       where
-       (c,ds) = f (n-1) xs
+       (c,ds) = f (n-1) (even i) xs
        i'     = c + i
 
 -- Based on "Printing Floating-Point Numbers Quickly and Accurately"
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.