Skip to content

Commit

Permalink
Update roundTo to correspond to base-4.6.0.0 changes (gh-105)
Browse files Browse the repository at this point in the history
  • Loading branch information
bos committed Dec 12, 2014
1 parent b1c6756 commit 5902ed0
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Data/Text/Internal/Builder/RealFloat/Functions.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{-# LANGUAGE CPP #-}

-- |
-- Module: Data.Text.Internal.Builder.RealFloat.Functions
-- Copyright: (c) The University of Glasgow 1994-2002
Expand All @@ -13,6 +15,30 @@ module Data.Text.Internal.Builder.RealFloat.Functions
) where

roundTo :: Int -> [Int] -> (Int,[Int])

#if MIN_VERSION_base(4,6,0)

roundTo d is =
case f d True is of
x@(0,_) -> x
(1,xs) -> (1, 1:xs)
_ -> error "roundTo: bad Value"
where
b2 = base `quot` 2

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) (even i) xs
i' = c + i
base = 10

#else

roundTo d is =
case f d is of
x@(0,_) -> x
Expand All @@ -27,3 +53,5 @@ roundTo d is =
where
(c,ds) = f (n-1) xs
i' = c + i

#endif

0 comments on commit 5902ed0

Please sign in to comment.