23
Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK mail: [email protected].

Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: [email protected]

Embed Size (px)

Citation preview

Page 1: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

Mathematics for Computing

Lecture 1:Numerical Representation

Dr Andrew Purkiss-TrewCancer Research UK

e-mail: [email protected]

Page 2: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

Course Information 2007-08

Numerical Representation

Logic

Algorithms and flow charts

Set theory

Relations

Functions

Page 3: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

Lecture / tutorial plans

Lecture every week in room SHB29(1 – 2½ hours)

Tutorial every other week in room SHB29 (1 – 1½ hours)

Page 4: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

Provisional TimetableDate Week Topic

01/10/07 1 Introduction and Numerical Representation

08/10/07 2 Logic 1 / Tutorial 1

15/10/07 3 Logic 2

22/10/07 4 Algorithms & Flowcharts / Tutorial 2

29/10/07 5 Set Theory 1

05/11/07 6 Set Theory 2 / Tutorial 3

12/11/07 7 Relations 1

19/11/07 8 Relations 2 / Tutorial 4

26/11/07 9 Functions 1

03/12/07 10 Functions 2 / Tutorial 5

10/12/07 11 Mock examination / hand out of coursework

Page 5: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

Versions of the number six Decimal: 6 Alphabetically: sixRoman: VI Tallying:

Versions of one and a quarter: Mixed number: 1¼, Improper fraction: 5/4, Decimal: 1.25

Representation of numbers

Page 6: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

Types of numbers

Natural numbers: 1, 2, 3, 4, …

Integers: …, -3, -2, -1, 0, 1, 2, 3, …

Rational numbers: m/n, where m and n are integers and n 0.Examples: ½, 5/3, ¼ = 0.25 1/3 = 0.3333…

Irrational numbers, examples: 2 1.414, 22/7, e 2.718.

Page 7: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

Decimal numbers (base 10)

String of digits

- symbol for negative numbers

Decimal point

A positional number system.Example: 3583.102 = 3 x 103 + 5 x 102 + 8 x 101 + 3 x 100 + 1 x 10-1 + 0 x 10-2 + 2 x 10-3

Page 8: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

Other number systems

Bases can be any natural number except 1.Example of time in seconds:3hrs 13mins 45secs = 3 x 602 + 13 x 601+ 45 x 600 seconds.

Binary (base 2), Octal (base 8) and hexadecimal (base 16)

1012 = 1 x 22 + 0 x 21 + 1 x 20 = 510

Page 9: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

The binary system

Base 2

Digits 0 and 1

Example:110110112

msd lsd

Page 10: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

Some binary numbers!!!

Binary Decimal Binary Decimal Binary Decimal

0 0 111 7 1110 14

1 1 1000 8 1111 15

10 2 1001 9 10000 16

11 3 1010 10 10001 17

100 4 1011 11 10010 18

101 5 1100 12 10011 19

110 6 1101 13 10100 20

Page 11: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

Octal

Base eight

Digits 0,1,2,3,4,5,6,7

Example: 1210 = 148 = 11002

100110111102 Binary

2 3 3 6 = 23368 Octal

Page 12: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

Hexadecimal

Base sixteen

Digits 0,1,2,3,4,5,6,7,8,9,A(10), B(11), C(12),D(13),E(14),F(15).

Example B316 = 17910 = 101100112

110101012 Binary

D 5 Hexadecimal

Page 13: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

Computer representation

Fixed length

Integers

Real

Sign

Page 14: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

Bits, bytes, words

Bit: a single binary digit

Byte: eight bits

Word: Depends!!!

Long Word: two words

Page 15: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

Integers

A two byte integer

16 bits

216 possibilities 65536

-32768 n 32767 or 0 n 65535

Page 16: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

Signed integers

First bit is sign bit. n 0, 0; n < 0, 1

For n 0, 15 bits are binary n

For n < 0, 15 bits are binary (n + 32768)

Example: -677210 (-0011010011101002)

10000000000000002

-0011010011101002

1100101100011002

Page 17: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

32bit word

Big and little ends

Big End

0 1 2 3

4 5 6 7

8 9 10 11

12 13 14 15

A N D Y

P

8bit byte32bit word

Little End

3 2 1 0

7 6 5 4

11 10 9 8

15 14 13 12

Y D N A

P

8bit byte

Page 18: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

Real numbers

‘Human’ form: 4563.2835

Exponential form: 0.4563 x 104

General form: m x be

Normalised binary exponential form: m x 2e

Another example: 6.02 x 1023

Page 19: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

Real numbers 2

For a 32 bit real number

Sign, 1 bit

Significand, 23 bits

Exponent, 8 bits

Exponent bias: 2n-1-1, where n is the number of bits

Page 20: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

Decimal to Binary conversion 1

n div 2 is the quotient.

n mod 2 is the remainder.

For example: 14 div 2 = 7, 14 mod 2 = 017 div 2 = 8, 17 mod 2 = 1

Page 21: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

Decimal to Binary conversion 2

1. Input n (natural no.)2. Repeat

2.1. Output n mod 2

2.2. n n div 2until n = 0

Example: 1110

Step n output1 11 -2.1 11 12.2 5 -2.1 5 12.2 2 -2.1 2 02.2 1 -2.1 1 12.2 0 -

Page 22: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk

Decimal to Binary conversion 3

1. Input n 2. Repeat

2.1. m 2n 2.2. Output m2.3. n frac(m)until n = 0

m is the integer part of m

frac(m) is the fraction part.

Example: 0.37510

Step m n output1 - 0.375 -2.1 0.75 0.375 -2.2 0.75 0.375 02.3 0.75 0.75 -2.1 1.5 0.75 -2.2 1.5 0.75 12.3 1.5 0.5 -2.1 1 0.5 -2.2 1 0.5 12.3 1 0 -

Page 23: Mathematics for Computing Lecture 1: Numerical Representation Dr Andrew Purkiss-Trew Cancer Research UK e-mail: a.purkiss@mail.cryst.bbk.ac.uk