{-# LANGUAGE CPP #-}
module Data.Text.Internal.Builder.RealFloat.Functions
(
roundTo
) where
roundTo :: Int -> [Int] -> (Int,[Int])
roundTo :: Int -> [Int] -> (Int, [Int])
roundTo Int
d [Int]
is =
case Int -> Bool -> [Int] -> (Int, [Int])
f Int
d Bool
True [Int]
is of
x :: (Int, [Int])
x@(Int
0,[Int]
_) -> (Int, [Int])
x
(Int
1,[Int]
xs) -> (Int
1, Int
1forall a. a -> [a] -> [a]
:[Int]
xs)
(Int, [Int])
_ -> forall a. HasCallStack => [Char] -> a
error [Char]
"roundTo: bad Value"
where
b2 :: Int
b2 = Int
base forall a. Integral a => a -> a -> a
`quot` Int
2
f :: Int -> Bool -> [Int] -> (Int, [Int])
f Int
n Bool
_ [] = (Int
0, forall a. Int -> a -> [a]
replicate Int
n Int
0)
f Int
0 Bool
e (Int
x:[Int]
xs) | Int
x forall a. Eq a => a -> a -> Bool
== Int
b2 Bool -> Bool -> Bool
&& Bool
e Bool -> Bool -> Bool
&& forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (forall a. Eq a => a -> a -> Bool
== Int
0) [Int]
xs = (Int
0, [])
| Bool
otherwise = (if Int
x forall a. Ord a => a -> a -> Bool
>= Int
b2 then Int
1 else Int
0, [])
f Int
n Bool
_ (Int
i:[Int]
xs)
| Int
i' forall a. Eq a => a -> a -> Bool
== Int
base = (Int
1,Int
0forall a. a -> [a] -> [a]
:[Int]
ds)
| Bool
otherwise = (Int
0,Int
i'forall a. a -> [a] -> [a]
:[Int]
ds)
where
(Int
c,[Int]
ds) = Int -> Bool -> [Int] -> (Int, [Int])
f (Int
nforall a. Num a => a -> a -> a
-Int
1) (forall a. Integral a => a -> Bool
even Int
i) [Int]
xs
i' :: Int
i' = Int
c forall a. Num a => a -> a -> a
+ Int
i
base :: Int
base = Int
10