R Computer Arithmetic
R Computer Arithmetic
for
⑤
Languages have
Types Integers
-
6 ((((1)
=
numbers
-write as seq. of
digits
Base B is
means
digits
30, B" 13 (Xn-n,...,x0]
-
is
ne . .
.,
a
seq
↑ -
Digits
written as
turn u-z..- to
Unsigned Interpretation:
Digits
of
(01172 (1172
=
(123) 10 1.102
=
2.10
+
+
3.100 123
=
(10272 1.23
=
0.22
+
1.21
+
1-20
+
41710
=
Lemma
...
then
(B)=()B. Bm (B)+
i
2=an -...Gobn-...bo
Proof
n - 1
Example:
()b 2iBl
=
i0
=
x -
1
(B)
B EBibl
=
i0 =
(p.B (kb +
(xii).Br (ii)
=
+
=(ii) t U
Lemma Injective by
pigeon hole principle
LetB >1 and so
190, ...,
B4-11 BY =
length u
of B
Proof
-
digits
Induction over u
n 1:=
Let k=90. . .
.,
B- 13
-
-- 1
I
(kYB
⑧
Then 4. B
24.B
= =
~the
lecture. Maximal is correct.
A has to be
such that r B"!!
.,
Bhtr_1].
number such that
B4 k q. r.
90, B-13 length
q
+
=
. . u
,
.
[ADB =(p)B
(k) B [qP7 =
r 30,...," 13
=
-
[P)B
=
&
Binary Numbers
B =
90,13
=
C1011 ↳2 G1)
=
no
Bits
⑮ 2.20 +
--1
:1 b
↳2:bur obizl
= -
... bo
Complement I A
most least
and or for
significant"
I I
a b adbalb a
-
b
bit
0 ⑧ O
00
0
8
O
⑧
1
1
I 1011G1) =
no
I 0111
(z)no
7
=(18710
1 1 ⑧
Arithmetic =
↓ Bitof
i-th a 168421
a b i-th Bitof b
/
+
citz: =
(ai&bi)((ai2(i)((biz(i)
Bitstigs
Lemma ath is sound. Add &1,1
--
*
-
↓
defined by
↓
Add 2, 1,
2
[.
(a b) (a) (b)
+
=
+
+ Co
4 ↑ --
Add &, 1in Add IN Add IN
bi ci
-Base Cast u 1:
=
Cit= (ai2bill(biD(i)
lanfe.)
bo 20 (a0)+/bo) co
+
C1 so
(so)z
a O
·
⑧ 0 0 O
O 1 1
- =
O
I I
:
8
I
2
O
=
O
!
⑧ 3 I
O
1
-
-
-
Induction Step:n-1-n
IH: (Cu-nSu-2 . . . So) =
Lan -2...ap) + 4bn -z ...bot
(a) (b) +
c
+
=(a+
cur) 2n-1
(uSn-1)
(Sn-2
=
=(anSu-n . . .
so) B
↑
carry out of most
then the
Resultmay be nt1 bits long; result cannot
62 acc. repr.
a tr b =
Su-n So
↳bits
. . .
=
Subtraction
of
Assume we have Base to digit sequences
Length
---
3 -2 3 -2
= + 1000 mod 103 =
1000
a b
=
mod m
=3 998 +
if a -
b k-m
=
=
Noon
1
=
mod 1000
complement b
of
Two's
(a b) (a)
-
=
(b)
+
22 -(b) 5)
=
1
+
=(a) -
(b) mod zY
Example
201032=407 10
24 -(107072 6
=
+ (970) =
207017
5 1
= +
Det a -
b: a =
5 1a
+ b
+
with co 1
=
M
subtr. On
Bitstigs
subtraction is sound
Lena
(a -
b) (a) 2"
=
+ -
(b)
Proof
-
(a b) (a 5 1)
-
=
+ +
5 1
=
-
b
=(a) (5) +
1
+
sigte
bit
=(a) +
((1 bi)zi)
=
i0 e
-
1
+
Er
(a)
(zi) -(Ebizi)+
+
e
subtraction (b7
accurate if [11132
carry
(cn) 1
=
=(a) -
(b) A (2:)
+
i8 =
142 4
=
+
8
+
15
=
ne
else not
=(a) (b) 2Y - +
2"
=
=(1000072
-
7
Signed interpretation
[.]:B" + 2 [bu-1bu-z...boy=-bu-2"-1
+
(bu -z...bo)
=(b) -
bu-n2"
Example n 3
=
7 2 3 -4 3 -
2 123
[.]
- -
(02) +
(140) =
↳700172 F(01 110) 1
+3
=
2
3 6 I
1
= = +
3 -
2
4
(om2) [04 30013 =
L003
-
4
+
+
I
3
7
iff
Lemma [a] [b] [anb] (n cur
= =
+
--
A
Add Z Add 2, 1, 2
Def
Prof (a) [b]
(a), (b) (an bin).2" []
+ =
- +
-n
soundh.
=(a b) +
-
4 ofAdd.
,
[a-nb] is accurate
=(Sn 1) 22
1
(Sn-2 ...so)
-
u
the
+ -
ouder same -
1
(an S--n)2
n -
condition +
=Cn?" +Sunth-1
=Sn-1
22
-
1
+ (Sn-2 ...
sob +
LanSura)=Aunburnt dur n
zYn-a- *n):
↓
-
2"sn-1-2se-1
Zan+Sn i
[s4
=
=(Suntn-an-burn)2"+ [atub]
-
=(cn -n
-
(u)2" (a+ub]
+
-
sound
least
sig. the
digits, result
W
of is accurate if
W
addu (a b)
+
cn 0
=
Carry bit
subu (a-b) G 1 =
Overflow bit
~[a b] +
Cu
-
Cu-1 0 =
"[a -b]
(001) 1
=
6 =
(110) sitch
20013: 14-2=[110] Sit