lecture 5 numbers and built in functions

Post on 12-May-2015

122 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1 of 45Module 5 : Numbers and Built‐in Functions

Introduction to       Computational Thinking

Module 5 :

Numbers and Built‐in Functions

Asst Prof Chi‐Wing FU, PhilipOffice: N4‐02c‐104

email: cwfu[at]ntu.edu.sg

2 of 45Module 5 : Numbers and Built‐in Functions

Topics• More on Numbers• Built-in functions• Comparing floating point numbers• Case Study: Finding Square Root• Self Study: Bitwise operators (optional)

3 of 45Module 5 : Numbers and Built‐in Functions

More on Numbers• Integers and Floating point numbers

• Their representations in computers•Data range•Optional: Bitwise operators on integers

4 of 45Module 5 : Numbers and Built‐in Functions

Memory: Bits and Bytes• 1 Byte = 8 bits (each bit either 0 or 1)• 1 Byte has 28 different variations, i.e., 256• How about 4 bytes?

0

0

0

0

0

0

0

0 0

0

00

1

11

1

1

1

1

1

1

1111000

1001

1002

Contents ofmemory

Memory

This is also covered in Introduction to Computing Systems

5 of 45Module 5 : Numbers and Built‐in Functions

Integers (others)• In most programming languages, an

integer takes up 4 bytes. The first bit for sign: +ve or -ve, i.e., 31 bits left

• Since there is a zero needed to be represented (as all zeros in the 32 bits), the available data range is [-231, +231 – 1 ]

6 of 45Module 5 : Numbers and Built‐in Functions

Integers (Python)• Integers in Python has unlimited precision• This is a useful feature in Python; otherwise,

we (programmers) have to handle them ourselves through programming effort

7 of 45Module 5 : Numbers and Built‐in Functions

Float• For real numbers with decimals• Limited precision because we use limited

number of bits to store the data in a special format (you will learn later in other course):•Max. representable finite float•Min. positive normalized float

Want to know more? http://docs.python.org/tutorial/floatingpoint.htmlhttp://en.wikipedia.org/wiki/Double_precision_floating-point_format

8 of 45Module 5 : Numbers and Built‐in Functions

Float• and it has limited precision…• Hence, computation results with floats

may not be exact

9 of 45Module 5 : Numbers and Built‐in Functions

Integer VS Float• When writing programs, variables that are

numeric can be integers or floats• So… how to choose?

Think about the context!!!

Need decimal values? Want precision?

10 of 45Module 5 : Numbers and Built‐in Functions

Integer VS Float• For the followings, which type is

more appropriate?•Age•Height•Volume of a container•Exchange rate•Voltage and current•Number of students in a class•Bank account balance

11 of 45Module 5 : Numbers and Built‐in Functions

Topics• More on Numbers• Built-in functions• Comparing floating point numbers• Case Study: Finding Square Root• Self Study: Bitwise operators (optional)

12 of 45Module 5 : Numbers and Built‐in Functions

What are Built-in functions• Built-in functions are subroutines, procedures,

or methods that are built into Python itself; each provides a specific functionality

• See the Python standard library: http://docs.python.org/library/functions.html

• In fact, you knew some of them already:• print (for displaying data)• input (for reading user input)• float, int, str (for converting data)• type (for checking data type) • exec (for executing a string in Python), …, etc.

13 of 45Module 5 : Numbers and Built‐in Functions

• abs(x) – Return absolute value of x

• min(x,y) and max(x,y) – Return minimum and maximum of x and y, respectively

They allow two or

more arguments

Let’s learn more: calling & arg(s)

Takes one argument

14 of 45Module 5 : Numbers and Built‐in Functions

More Built-in Functions• math.ceil(x) and math.floor(x) –

Return the ceiling and floor of x as “int”

Remember to import math

15 of 45Module 5 : Numbers and Built‐in Functions

More Built-in Functions• math.pow(x,y) – Return x raised to the power y• math.sqrt(x) – Return the square root of x• math.log(x) – Return natural logarithm of x• math.log10(x) – Return base-10 logarithm of x

Math functionsusually return“float”

16 of 45Module 5 : Numbers and Built‐in Functions

More Built-in Functions• math.sin(x), math.cos(x), math.tan(x)

– Return the sine, cosine, and tangent of x (radian)

• math.asin(x),math.acos(x),math.atan(x)– Return arc sin, cos, and tan of x in radian(Note: they all use radian!)

• math.degrees(x) and math.radians(x) – Return x in degrees and radians, respectively

17 of 45Module 5 : Numbers and Built‐in Functions

Help Function• help(“...”) – display help information on a

Python function or module

18 of 45Module 5 : Numbers and Built‐in Functions

Topics• More on Numbers• Built-in functions• Comparing floating point numbers• Case Study: Finding Square Root• Self Study: Bitwise operators (optional)

19 of 45Module 5 : Numbers and Built‐in Functions

No Equality!• If we want to know if two floating point numbers

are equal or not, can we use "==" ??• It is tricky!!!

They are not equal! Why?So… how to fix it?

20 of 45Module 5 : Numbers and Built‐in Functions

So… how to check?• First, we can define a relatively small floating

point value, usually called epsilon or tolerance• IDEA: if the absolute difference between the

two floating point numbers is smaller than epsilon, we say that their values are the same

Key idea!!!!

(computationally but not mathematically)

21 of 45Module 5 : Numbers and Built‐in Functions

Topics• More on Numbers• Built-in functions• Comparing floating point numbers• Case Study: Finding Square Root• Self Study: Bitwise operators (optional)

22 of 45Module 5 : Numbers and Built‐in Functions

Case Study• Here we want to implement an algorithm to

compute the square root of a value without using math.sqrt

You should implement, run, and try the case study yourself in the course

23 of 45Module 5 : Numbers and Built‐in Functions

Algorithm: compute square root• We can compute the square root of a value,

say x, iteratively using the following idea:Given x,

first compute an initial guess: guess = x/2then diff = guess* guess - xif abs(diff) < epsilon, we are doneif diff > 0, guess is too largeif diff < 0, guess is too smallupdate guess so that it gets closer, then

Repeat

24 of 45Module 5 : Numbers and Built‐in Functions

Algorithm: compute square root• But… how to update guess so that it gets

closer in every iteration…• Let’s check x/guess…

In case x=100 and guess=50,x/guess = 2

the targeting square root value should always lie between guess and x/guess!

• So… we may update guess as mean value of guess and x/guess and make it closer!

25 of 45Module 5 : Numbers and Built‐in Functions

Algorithm: compute square rootLet’s do this test…• Iteration #1

x=100 and guess=50x/guess=2 -> new guess = mean = 26

• Iteration #2x=100 and guess=26x/guess=3.846 -> new guess = mean = 14.923

• Iteration #3x=100 and guess=14.923x/guess=6.701 -> new guess = mean = 10.812…………

Really getting closer and closer!!!

26 of 45Module 5 : Numbers and Built‐in Functions

Implementation• Here is the Python implementation

We will learn “while” in module 6.2

27 of 45Module 5 : Numbers and Built‐in Functions

Implementation• Add variable "iter" to count number of iterations

Add iteration counter

28 of 45Module 5 : Numbers and Built‐in Functions

Verification… by math.sqrt• Lastly… we must verify our program!!!

Report the differenceThis is a verification

29 of 45Module 5 : Numbers and Built‐in Functions

Testing #1• We try with x = 100

Our result

Difference is very small

30 of 45Module 5 : Numbers and Built‐in Functions

Testing #2• We try with x = 100000

Our result

But…need moreiterations forlarger num.

Differencestill small

31 of 45Module 5 : Numbers and Built‐in Functions

Testing #3• Lastly… try with x = 2

Difference is still very small

Fewer iterationsthis time

Our result

32 of 45Module 5 : Numbers and Built‐in Functions

Topics• More on Numbers• Built-in functions• Comparing floating point numbers• Case Study: Finding Square Root• Self Study: Bitwise operators (optional)

33 of 45Module 5 : Numbers and Built‐in Functions

What are Bitwise operators• Recall that integers are stored in binary, e.g.,

34 of 45Module 5 : Numbers and Built‐in Functions

NOT~

SHIFT (right)>>

SHIFT (left)<<

Bitwise XOR^Bitwise OR|

Bitwise AND&meaningoperator

What are Bitwise operators• Python provides bitwise operators that process

input integers bit by bit correspondingly• Treat 1 as true and 0 as false

Input:15 -> 0000 111117 -> 0001 0001What is 15 | 17 ?

All are binaryoperations except ~,which is unary

35 of 45Module 5 : Numbers and Built‐in Functions

00 & 0

00 & 1

01 & 0

11 & 1

ResultAND

Truth tables of &, |, ^, ~

00 | 0

10 | 1

11 | 0

11 | 1

ResultOR

00 ^ 0

10 ^ 1

11 ^ 0

01 ^ 1

ResultXOR

• XOR is called exclusive or; it is true ONLY if one of the two bits is true (but not both)

• ~x is a special operation called 2’s complement, which is just -x-1, i.e., ~2 gives -3

36 of 45Module 5 : Numbers and Built‐in Functions

Examples: &, |, ^

&

|

^

37 of 45Module 5 : Numbers and Built‐in Functions

Examples: shift <<, >>

<<

>>

38 of 45Module 5 : Numbers and Built‐in Functions

3 << 1 gives 6SHIFT (left)<<

~3 gives -4NOT~

3 >> 1 gives 1SHIFT (right)>>

3 ^ 2 gives 1Bitwise XOR^

3 | 2 gives 3Bitwise OR|

3 & 2 gives 2Bitwise AND&examplesmeaningoperator

More examples

Try to work them out yourself

39 of 45Module 5 : Numbers and Built‐in Functions

Any Application?1. Simple Data Encryption and Decryption

using the xor operator2. Pseudo Random Number Generator

(PRNG)

40 of 45Module 5 : Numbers and Built‐in Functions

Application #1• xor can be used for doing simple encryption

and decryption• Given

D – our data (any integer, i.e., 32 bits)K – our key in encryption (which is an integer)

• We can perform encryption by:E = D xor K where E is the encrypted integer

• and decryption byE xor K gives D We can recover D from E using K

XOR has this interesting property Why? Study the truth table…

41 of 45Module 5 : Numbers and Built‐in Functions

Application #2• Pseudo Random Number Generator (PRNG):

•Seed – A number to initialize the generator•Random number sequence – Use seed as

input, we apply the generator to obtain the first random number; then use it as input to generate the next random number, and so on…See http://docs.python.org/library/random.html

• In designing a PRNG, bitwise operations are often used, see next slide for a simple example

Interesting article:http://spectrum.ieee.org/semiconductors/processors/behind-intels-new-randomnumber-generator

42 of 45Module 5 : Numbers and Built‐in Functions

Application #2 (cont.)• Example: Define a function “parityOf” to

compute the parity of an integer:A bit to add to an integer to make the number of 1s in its binary form to be even

Define a simple 12-bit PRNG

A sequence of random number(binary numbers) can begenerated starting from the seed

note: 0x indicateshexidecimal format

43 of 45Module 5 : Numbers and Built‐in Functions

Application #2 (cont.)• Note:

• You will learn how to use Python keyword “def” to define functions later in this course

• Using bitwise operators, we can efficiently compute the parity and develop pseudorandom number generators. There are many PRNGs in the world; some are more complicated, and they could have different quality…

• Since the numbers generated in the example code form a binary number sequence, we also call it a Pseudo Random Binary Sequence Generator (PRBSG)

• Related courses in the future:“Computer Organization and Architecture” and “Microprocessor-based System Design”

44 of 45Module 5 : Numbers and Built‐in Functions

Take Home Messages• Integers, Long, and Floating point numbers

– Representation and property: data range, minimum and maximum values, and precision

• Built-in functions are useful resources built into Python itself

• Comparing floating point numbers can be tricky (and error-prone)– Use absolute difference against a small epsilon to test

equality for floating point numbers

45 of 45Module 5 : Numbers and Built‐in Functions

Reading Assignment • Textbook

Chapter 1: Beginnings1.8 to 1.10

Note: Though some material (1.10) in textbook is not directly related to the lecture material, you can learn more from them.

top related