0% found this document useful (0 votes)
42 views

Bigintb

This document outlines an assignment to implement arithmetic on arbitrarily large integers represented as lists of digits in a given base. Students are asked to define a type for such numerals along with functions for normalization, base conversion, and arithmetic operations like addition, subtraction, multiplication, division, and modulo. The functions are required to satisfy certain properties like producing results in normal form and using the maximum of the input bases.

Uploaded by

Prabhjot Sandhu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Bigintb

This document outlines an assignment to implement arithmetic on arbitrarily large integers represented as lists of digits in a given base. Students are asked to define a type for such numerals along with functions for normalization, base conversion, and arithmetic operations like addition, subtraction, multiplication, division, and modulo. The functions are required to satisfy certain properties like producing results in normal form and using the maximum of the input bases.

Uploaded by

Prabhjot Sandhu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CSL665: Introduction to (Logic and Functional) Programming I semester 2011-12 Standard ML Programming Assignment

The BigintB Numeral System

Introduction

Given a list of integers L = [al1 , . . . , a1 , a0 ] of length l > 0 we may interpret it as representing a polynomial p(x) = al1 xl1 + + a0 with integer cocients. We may also nd the value of the polynomial for particular (integer) values of the variable x. Further if x = B > 1 we may also think of the list L as representing a non-negative integer written in base B if all the elements of the list have values in the range 0..(B 1). The value of such a list is then dened as
l1

value(L, B) =
i=0

ai B i

(1)

Of course, for any value C > B, we could also interpret the list as a numeral in base C whose value is given by
l1

value(L, C) =
i=0

ai C i . Therefore in order to give these numerals a unique interpretation we need to specify

the base as well. But once we are given a base B > 1, the numeral would have a unique value and then it does not matter whether the individual elements ai in the list are positive, negative, zero or even have values greater than B or less than B. In all cases we assume the value given by equation (1). The pair (L, B) is called a numeral in base B. But for convenience it might be a good idea to have a standard format which ensures that the numeral conforms to the usual conventions adopted for representing values in the place-value system of representation1 . So what about the empty list? We simply adopt the convention that the empty list represents the number zero (regardless of the base) and is in fact, the standard representation of the number zero. This would allow us to go beyond the word-length of the machine and also the constraints imposed by particular bases and allow us to work across dierent bases with arbitrarily large-sized integers, in the negative domain too. It would then allow us to do computations over large numbers.

The Problem Specication


1. Dene a new type type bigintb = int list * int which is meant to represent the type of numbers written as lists of integers along with another integer (> 1) which is an indication of the base on which the value of the list is dened (as in equation (1)). ([],B) is a representation of the number zero in base B for any integer value of B > 1. 2. Dene the exception InvalidBase if the base is any integer less than 2. 3. Dene a function

there should be no leading zeroes, all the coecients should be non-positive for a negative number and all the coecients should be non-negative for a positive number etc.

1 e.g.

normalize: bigintb -> bigintb such that for any (L, B) : bigintb, with B > 1, normalize yields an element (N, B) such that value(L, B) = value(N, B) = v (say) (N, B) = ([ ], B) if v = 0 If N = [dn1 , . . . , d0 ] for some n > 0 then dn1 = 0 (i.e. no leading 0s in the representation) 0 dj < B for all 0 j < n whenever v > 0 and 0 dj > B for all 0 j < n whenever v < 0. A list N that satises the above constraints is said to be in normal form and is a standard representation of the number v in base B. 4. Dene a function convert: bigintb * int -> bigintb such that if convert((L, B), C) = (M, C) for any valid base C then (M, C) is the normal form in base C of the numeral (L, B). 5. Dene the functions (arithmetic operations) add, sub, mult, div, mod : bigintb bigintb > bigintb representing respectively the operations +, , , div and mod on integers with the added condition that for bop {add, sub, mult, div, mod}, if bop((L, B), (M, C)) = (N, D) then D = max(B, C), (N, D) is in normal form 6. Dene the boolean valued functions (arithmetic comparisons) eq, lt : bigintb bigintb > bool which check for value equality and less than respectively. Note: 1. You are not allowed to change any of the names given in font. You are not even allowed to change upper-case letters to lower-case letters or vice-versa. 2. You may dene any new auxiliary functions if you like besides those mentioned. 3. You need to think of the most ecient way of implementing the various functions given in the signature so that the function results satisfy their denitions and properties. 4. Since the evaluator is going to look at your source code before evaluating it, you must explain your algorithms in the form of ML comments, so that the evaluator can understand what you have implemented. 5. Follow standard indentation of code as shown in the examples in the class. 6. Do not add any more decorations or functions or user-interfaces in order to impress the evaluator of the program. Nobody is going to be impressed by it. 7. There is a serious penalty for copying. If it is felt that there is too much similarity in the code between any two persons, then both are going to be penalized equally. So please set permissions on your directories, so that others cannot copy your programs.

You might also like