P. 1
pure

pure

|Views: 19|Likes:
Published by Yezi Huang

More info:

Published by: Yezi Huang on Jan 27, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

11/13/2011

pdf

text

original

A positional number system [Knu73] is a notation for writing a number as a sequence of digits

b

0

:

:

:

b

m

;

1

. The digitb

0

is called the least significant digit and the digitb

m

;

1

is called the
most significant digit. Except when writing ordinary, decimal numbers, we will always write
sequences of digits from least significant to most significant.

Each digitb

i

has weightw

i

, so the value of the sequenceb

0

:

:

:

b

m

;

1

isP

m

;

1

i

=

0

b

i

w

i

. For any

given positional numbersystem, the sequence of weights is fixed, as is theset ofdigitsD

i

from

which eachb

i

is chosen. For unary numbers,w

i

=

1

andD

i

=

f

1g

for alli , and for binary

numbersw

i

=

2

i

andD

i

=

f

0

1g

. (By convention, we write all digits in typewriter font
except for ordinary, decimal digits.) A number is said to be written in baseB

ifw

i

=

B

i

and

D

i

=

f

0

:

:

:

B

;

1

g

. Usually, but not always, weights are increasing sequences of powers,

and the setD

i

is the same for every digit.

A number system is said to be redundant if there is more than one way to represent some
numbers. Forexample, we can obtain a redundantsystem of binarynumbers by takingw

i

=

2

i

andD

i

=

f

0

1

2g

. Then the decimal number 13 can be written 1011, or 1201, or 122. If
we allow trailing0s, then almostall positionalnumbersystems areredundant, sinceb

0

:

:

:

b

m

;

1

is always equivalent tob

0

:

:

:

b

m

;

1

0. Therefore, we disallow trailing0s.

Computer representations of positional number systems can be dense or sparse. A dense
representation is simply a list (or some other kind of sequence) of digits, including those digits
that happen to be 0. A sparse representation, on the other hand, includes only non-zero digits.
It must then include information on either the rank (i.e., the index) or the weight of each digit.
For example, Figure 6.1 shows two different representations of binary numbers in Standard
ML— one dense and one sparse — along with several representative functions on each.

6.1 Positional Number Systems

63

structure Dense =
struct

datatype Digit= Zeroj

One

type Nat = Digit list (

increasingorder of significance,no trailingZeros

)

fun inc [] = [One]

j

inc (Zero ::d

s ) = One ::d

s

j

inc (One ::d

s ) = Zero :: incd

s

(

carry

)

fun dec [One] = []

j

dec (One ::d

s ) = Zero ::d

s

j

dec (Zero ::d

s ) = One :: decd

s

(

borrow

)

fun add (d

s , []) =d

s

j

add ([],d

s ) =d

s

j

add (d

::d

s

1

, Zero ::d

s

2

) =d

:: add (d

s

1

,d

s

2

)

j

add (Zero ::d

s

1

,d

::d

s

2

) =d

:: add (d

s

1

,d

s

2

)

j

add (One ::d

s

1

, One ::d

s

2

) = Zero :: inc (add (d

s

1

,d

s

2

)) (

carry

)

end

structure SparseByWeight =
struct

type Nat = int list (

increasing list of weights, each a power of two )

(

add a new weight to a list, recurse if weight is already present

)

fun carry (w

, []) = [w

]

j

carry (w

,w

s asw

0

::r

e

s

t

) = ifw

<

w

0

thenw

::w

s else carry (2

w

,r

e

s

t

)

(

borrow from a digit of weightw

, recurse if weight is not present

)

fun borrow (w

,w

s asw

0

::r

e

s

t

) = ifw

=w

0

thenr

e

s

t

elsew

:: borrow (2

w

,w

s )

fun incw

s = carry (1,w

s )

fun decw

s = borrow (1,w

s )

fun add (w

s , []) =w

s

j

add ([],w

s ) =w

s

j

add (m

asw

1

::w

s

1

,n

asw

2

::w

s

2

) =

ifw

1

<

w

2

thenw

1

:: add (w

s

1

,n

)

else ifw

2

<

w

1

thenw

2

:: add (m

,w

s

2

)

else carry (2

w

1

, add (w

s

1

,w

s

2

))

end

Figure 6.1: Two implementations of binary numbers.

64

Numerical Representations

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->