Posts Tagged ‘big’

Programming Praxis – Big Numbers: Input And Output

June 14, 2011

In today’s Programming Praxis exercise, our task is to write functions to convert Big Numbers to and from strings. Let’s get started, shall we?

To convert from a string, we simply convert each digit to the correct value, multiplying them by the base as we go along.

```readBase :: (Num a, Enum a) => a -> String -> a
readBase b ('-':xs) = - readBase b xs
readBase b xs       = foldl (\a x -> b * a + val x) 0 xs where
val d = maybe (error "unrecognized digit") id . lookup d \$ zip
(['0'..'9'] ++ ['A'..'Z'] ++ ['a'..'z']) [0..]```

To convert to a string, we divide by the base until we reach zero. The remainders form the digits of the output.

```showBase :: Integral a => a -> a -> String
showBase b n = if n < 0 then '-' : showBase b (abs n) else
map (digit . snd) \$ m : reverse ms  where
((_:ms), (m:_)) = span ((> 0) . fst) \$ iterate (flip divMod b . fst) (n, 0)
digit d = maybe undefined id . lookup d . zip [0..] \$
['0'..'9'] ++ ['A'..'Z'] ++ ['a'..'z']```

While we’re at it let’s also make BigNum an instance of Read and Show, the typeclasses that normally handle conversion to and from strings.

```instance Read BigNum where
readsPrec _ = return . first (readBase 10) . split where
split ('-':xs) = first ('-':) \$ split xs
split xs       = span (`elem` ['0'..'9'] ++ ['A'..'Z'] ++ ['a'..'z']) xs

instance Show BigNum where
show = showBase 10```

Some tests to see if everything is working properly:

```main :: IO ()
main = do print \$ readBase 10 "1234"   == ( 1234 :: BigNum)
print \$ readBase 10 "-1234"  == (-1234 :: BigNum)
print \$ readBase  2 "101010" == (   42 :: BigNum)
print \$ read "-1234"         == (-1234 :: BigNum)
print \$ showBase 10 ( 1234 :: BigNum) ==   "1234"
print \$ showBase 10 (-1234 :: BigNum) ==  "-1234"
print \$ showBase  2 (   42 :: BigNum) == "101010"
print \$ show        (-1234 :: BigNum) ==  "-1234"```

Programming Praxis – Big Numbers: Division

June 7, 2011

In today’s Programming Praxis exercise, our goal is to add division to our Big Number library. Let’s get started, shall we?

Because I found two bugs in the existing code and to avoid spreading the code around too much, this week I’ll post the full code for the module again.

I’ve added some imports:

```import Control.Arrow
import Data.List
import Data.Ratio
import Test.QuickCheck
```

The data type, base and Ord instance are unchanged.

```data BigNum = B Int [Int] deriving (Eq, Show)

base :: Integer
base = 1000

instance Ord BigNum where
compare (B l1 ds1) (B l2 ds2) = case compare l1 l2 of
EQ -> maybe EQ id . find (/= EQ) . reverse \$ zipWith compare ds1 ds2
c  -> c
```

While testing division I discovered that the result of a subtraction operation wasn’t trimmed of leading zeroes. The new definition for + solves this.

```instance Num BigNum where
a@(B l1 ds1) + b@(B l2 ds2) = B (length t * signum l) (reverse t) where
B l _ = if abs b > abs a then b else a
(_,t) = span (== 0) . reverse . f 0 \$ (if abs b > abs a then flip else id)
(prep \$ if signum l1 == -signum l2 then (-) else (+)) ds1 ds2
prep op (x:xs) (y:ys) = op (toInteger x) (toInteger y) : prep op xs ys
prep _  xs     ys     = map toInteger \$ xs ++ ys
f r (x:xs) = let (d,m) = divMod (r + x) base in fromIntegral m : f d xs
f r []     = if r == 0 then [] else [fromIntegral r]
(B l1 ds1) * (B l2 ds2) = B (signum l1 * signum l2 * sl) sds where
B sl sds = sum \$ mult ds1 ds2
mult (x:xs) (y:ys) = fromIntegral (toInteger x * toInteger y) :
map shift (mult xs (y:ys)) ++
map shift (mult [x] ys)
mult _     _  = []
shift (B l ds) = B (l + 1) (0 : ds)
negate (B l ds) = B (-l) ds
abs (B l ds)    = B (abs l) ds
signum (B l _)  = fromIntegral \$ signum l
fromInteger n | n < 0     = negate \$ fromInteger (-n)
| otherwise = B (length ds) (map fromIntegral ds)
where ds = tail \$ f (n,0)
f (0,m) = [m]
f (d,m) = m : f (divMod d base)
```

These two instances aren’t strictly necessary, but making a data type an instance of Integral requires it instances Enum and Real as well. The definitions are trivial enough, so I added them.

```instance Enum BigNum where
fromEnum = fromIntegral . toInteger
toEnum   = fromInteger . fromIntegral

instance Real BigNum where
toRational = (% 1) . toInteger
```

The second bug I found was that converting a Big Number to an Integer didn’t keep the sign of the number. This is now fixed. Also, since we’re now instancing Integral I could give it the proper name.

```instance Integral BigNum where
toInteger (B l ds) = foldr (\x a -> fromIntegral (signum l * x) + base * a) 0 ds
```

For the actual division, I developed my own algorithm since I couldn’t find a good explanation of Knuth’s algorithm. It fairly simple, though probably a bit less efficient than Knuth’s version. Determine how often the denominator can go in the numerator based on the first digit group of the denominator (d1) and the first digit group of the numerator (n1). If n1 is less than d1, the second digit group of the numerator (n2) is used instead, and a value equal to how often d1 goes into the base is added. The resulting value is multiplied by the denominator, subtracted from the numerator and the algorithm is called recursively. Thanks to the Integral typeclass we get the other functions (div, mod, etc.) for free.

```    quotRem n@(B l1 ds1) d@(B l2 ds2)
| d == 0         = error "Division by zero"
| n < 0 || d < 0 = let (q',r') = quotRem (abs n) (abs d)
in (signum n * signum d * q', signum n * r')
| n < d          = (0, n)
| otherwise      = first (+ q) \$ quotRem (n - d*q) d where
(n1,n2,d1) = (last ds1, last \$ tail ds1, last ds2)
q = if n1 < d1 then shift (l1 - l2 - 1) \$ div n2 d1 +
fromIntegral (div base (fromIntegral d1))
else shift (l1 - l2) \$ div n1 d1
shift s i = B (s + 1) \$ (replicate s 0) ++ [i]
```

Since there are plenty of possible corner cases I automated the testing using QuickCheck to make sure there is no difference between Integer and BigNum division.

```divTest :: Integer -> Integer -> Property
divTest a b = b /= 0 ==> quotRem a b == (toInteger q, toInteger r)
where (q, r) = quotRem (fromInteger a :: BigNum) (fromInteger b)

main :: IO ()
main = do print \$ div 12345678 (3456 :: BigNum) == 3572
print \$ mod 12345678 (3456 :: BigNum) == 846
quickCheck divTest
```

Programming Praxis – Big Numbers: Addition, Subtraction, And Multiplication

May 31, 2011

In today’s Programming Praxis exercise, we’re going to add addition, subtraction and multiplication to our big number library. Let’s get started, shall we?

Addition and multiplication are both functions of the Num typeclass, so we add the two functions (the fact that a – b is equal to a + (-b) is already present in the Num typeclass, so since we already defined negate we don’t need to define the subtraction function) in our Num instance:

```instance Num BigNum where
```

For addition, we need to decide whether we’re adding or subtracting, which we do for each pair of digit groups. We could do this digit by digit, but I’m going to be lazy and do it per group. The conversion to and from Integers is not needed now, but will be required once the base is increased to prevent overflowing the Int type.

```    a@(B l1 ds1) + b@(B l2 ds2) = B (length ds * signum l) ds where
B l _ = if abs b > abs a then b else a
ds = f 0 \$ (if abs b > abs a then flip else id)
(prep \$ if signum l1 == -signum l2 then (-) else (+)) ds1 ds2
prep op (x:xs) (y:ys) = op (toInteger x) (toInteger y) : prep op xs ys
prep _  xs     ys     = map toInteger \$ xs ++ ys
f r (x:xs) = let (d,m) = divMod (r + x) base in fromIntegral m : f d xs
f r []     = if r == 0 then [] else [fromIntegral r]
```

For multiplication we use the grade school method of multiplying each digit group (again, instead of per-digit) and summing them up at the end.

```    (B l1 ds1) * (B l2 ds2) = B (signum l1 * signum l2 * sl) sds where
B sl sds = sum \$ mult ds1 ds2
mult (x:xs) (y:ys) = fromIntegral (toInteger x * toInteger y) :
map shift (mult xs (y:ys)) ++
map shift (mult [x] ys)
mult _     _  = []
shift (B l ds) = B (l + 1) (0 : ds)```

Some tests to see if everything is working properly:

```main :: IO ()
main = do print \$  12345678 +  987654321  == ( 999999999 :: BigNum)
print \$  12345678 -  987654321  == (-975308643 :: BigNum)
print \$ 987654321 -   12345678  == ( 975308643 :: BigNum)
print \$ -12345678 +  987654321  == ( 975308643 :: BigNum)
print \$ -12345678 -   87654321  == ( -99999999 :: BigNum)
print \$  12345678 *   87654321  == ( 1082152022374638 :: BigNum)
print \$  12345678 * (-87654321) == (-1082152022374638 :: BigNum)
print \$ -12345678 *   87654321  == (-1082152022374638 :: BigNum)
print \$ -12345678 * (-87654321) == ( 1082152022374638 :: BigNum)
```

Programming Praxis – Big Numbers: Getting Started

May 24, 2011

In today’s Programming Praxis exercise, our task is to implement the basics of a library for big numbers. Let’s get started, shall we?

A quick import:

```import Data.List
```

We could represent a big number as a plain list like the Scheme version does. Using a custom data structure, however, has the advantage of being able to make it an instance of the standard numeric classes, which means shorter function names and easier literals. We also store the number of digits separately because I find it a bit cleaner.

```data BigNum = B Int [Int] deriving (Eq, Show)
```

We’re using 1000 as our base for now.

```base :: Integer
base = 1000
```

The Num class gives us some of the required functions. Since addition and multiplication haven’t been implemented yet the compiler will throw some warnings.

```instance Num BigNum where
negate (B l ds) = B (-l) ds
abs (B l ds)    = B (abs l) ds
signum (B l _)  = fromIntegral \$ signum l
fromInteger n | n < 0     = negate \$ fromInteger (-n)
| otherwise = B (length ds) (map fromIntegral ds)
where ds = tail \$ f (n,0)
f (0,m) = [m]
f (d,m) = m : f (divMod d base)
```

I personally don’t see much use for three separate functions for the sign of a number, since you can either use signum or the appropriate comparsion, but we’ll stick to the assignment.

```positive, negative, zero :: (Num a, Ord a) => a -> Bool
positive = (> 0)
negative = (< 0)
zero     = (== 0)
```

If we make BigNum an instance of the Integral class we could use the default even and odd functions, but this requires implementing modulo arithmetic. We’ll use these for now until we have to do so in a following exercise.

```bigOdd, bigEven :: BigNum -> Bool
bigEven (B l ds) = l == 0 || even (head ds)
bigOdd           = not . bigEven
```

The Integral class also has a toInteger function, but for now we’ll use a separate function.

```fromBig :: BigNum -> Integer
fromBig (B _ ds) = foldr (\x a -> fromIntegral x + base * a) 0 ds
```

Here’s another advantage of using the standard type classes: we only need to implement the compare function to get all the others for free.

```instance Ord BigNum where
compare (B l1 ds1) (B l2 ds2) = case compare l1 l2 of
EQ -> maybe EQ id . find (/= EQ) . reverse \$ zipWith compare ds1 ds2
c  -> c
```

Time for a whole bunch of tests.

```main :: IO ()
main = do let a = 12345678
let b = -87654321
print \$ 0               == B 0 []
print \$ 1               == B 1 [1]
print \$ (-1)            == B (-1) [1]
print \$ a               == B 3 [678,345,12]
print \$ b               == B (-3) [321,654,87]
print \$ fromBig a       == 12345678
print \$ fromBig (abs b) == 87654321
print \$ abs a           == a
print \$ positive a
print \$ negative b
print \$ zero (0 :: BigNum)
print \$ bigEven a
print \$ bigOdd b
print \$ b /= a
print \$ b < a
```

Looks like everything is working properly.