cs 101: numerical computing

29
CS 101: Numerical Computing Abhiram Ranade

Upload: caine

Post on 11-Jan-2016

36 views

Category:

Documents


0 download

DESCRIPTION

Abhiram Ranade. CS 101: Numerical Computing. Representing Integers. “int x;” : reserves one cell in memory for x. One cell: “One word” has 32 capacitors. Each capacitor can have High/Low charge. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 101: Numerical Computing

CS 101: Numerical Computing

Abhiram Ranade

Page 2: CS 101: Numerical Computing

Representing Integers

• “int x;” : reserves one cell in memory for x.

• One cell: “One word” has 32 capacitors. Each capacitor can have High/Low charge.

• Storing positive numbers only: Represent number to be stored in binary, and store High charge for 1, Low charge for 0.

• How to store negative numbers?

Page 3: CS 101: Numerical Computing

Sign magnitude representation

• Key idea: One of the capacitors determines sign. L = “+”, H= “-”

• “x = 5;” : store L LLLL...LHLH• “x = -5;” : store H LLLL...LHLH• Max positive number: L H...H = + 231-1• Min negative number: H H...H = - 231-1• 231 is almost = 2 * 109

• Actual representation is slightly different.

Page 4: CS 101: Numerical Computing

How to store larger numbers?

• Use Long integers: “ long x;” Reserves 2 words, with one sign bit. Max

magnitude 263-1, about 1019

• Use floating point representation.

Page 5: CS 101: Numerical Computing

Floating Point Representation

• “float y;” : reserve 1 word = 32 bits as before.• 24 bits : mantissa. 8 bits: exponent. Each

includes a sign bit.• Mantissa can only store 23 bit accuracy, about 7

decimal digits.• Exponent can be between -100 and +100 (about).• “double z;” : 53 bits of mantissa, 11 bits exponent.

Page 6: CS 101: Numerical Computing

Summary

• Cohoon gives exact details. Not important in this course. Various other formats, e.g. “unsigned int”, “short”.

• Circuits are more complex for floating representation, sign...

“God made natural numbers, the rest is the work of man.” -- Leopold Kronecker.

Page 7: CS 101: Numerical Computing

Floating Point Arithmetic

float w, y=1.5, avogadro=6.023 E 23 ; w = avogadro + y;• What is the value of w?• Exact value of w and avogadro will differ in

the 23rd digit.• “float” stores only 7 digits. 23rd digit

ignored. -- “roundoff error”. • w = avogadro!

Page 8: CS 101: Numerical Computing

Mixed Arithmetic

• C++ converts from float to int and vice versa as needed.

• “int x=10; float y=6.5;”

• “x=y;” : 6.5 is rounded down. x=6.

• “360/x” : integer result is calculated.

• “360.0/x” : float result is calculated.

• “360/y” : float result. Try out yourself!

Page 9: CS 101: Numerical Computing

Simple Numerical Program

#include <turtlesim.h>

procedure turtleMain(){

float c;

cout << “Centigrade temperature:” << endl;

cin >> c;

cout << “Fahrenheit:” << (c*9/5)+32;

}

Page 10: CS 101: Numerical Computing

Remarks on Arithmetic

• Multiplication: write explicitly using “*”.

• *, / have higher precedence than +, -

• *, / have equal precedence, evaluated in left to right order. Similarly +, -

• “x = m % n;” % : mod. m=10,n=4, x=2.

• Same precedence as *, /

• Use () to override default precedence.

Page 11: CS 101: Numerical Computing

Comments

• Programs are read by compilers, but also by other programmers.

• You should include extra description (“comment”) to help other programmers figure out the logic:

• Style 1: “// ......” to end of line

• Style 2: “/* .... */” Examples soon.

Page 12: CS 101: Numerical Computing

Important Idea: Reassignment

int m=5;

m = 3*m + 1; // Reassignment

• Mathematically absurd: “simplify” to

0 = 2*m+1 • C++ interpretation: Calculate the value of rhs,

then store it in lhs. Above: m = 16.• Important inside loops. Next..

Page 13: CS 101: Numerical Computing

Reassignment in Loops

int i=1;

repeat(5){

forward(i); right(90);

forward(i); right(90);

i = i + 1;

}

• What will be drawn?

Page 14: CS 101: Numerical Computing
Page 15: CS 101: Numerical Computing

Nested Squares

How will you draw this pattern? Can you avoid

writing 7 calls to procedure Polygon?

Side length: 2, 4, 6, ...

Page 16: CS 101: Numerical Computing

Homework: draw this

Make pattern go around a polygon/circle

Number of times to spiral: read from cin

Page 17: CS 101: Numerical Computing

Common Programming pattern

“Repeat n times with i taking different values”

• Cleanly specified using for statement:

for( xxx; yyy; zzz ) { www } // next..

• Useful in numerical computing also

Page 18: CS 101: Numerical Computing

Spiral using for

int i;

for(i=1; i < 6; i=i+1){

forward(i); right(90);

forward(i); right(90);

}

Page 19: CS 101: Numerical Computing

for(xxx; yyy; zzz) { www }0. Execute “xxx;”. Must be an assignment

statement. “loop initialization”

1. Evaluate condition yyy. “loop test”. If true,

continue with step 2; if false, for statement

execution ends.

2. Execute www. “loop body”

3. Execute zzz. “loop increment”

4. Continue from 1.

Page 20: CS 101: Numerical Computing

Condition• “ a op b” where op is <, >, <=, >=, ==, !=

• == : is equal to

• != : is not equal to

• Conditions can be combined: – (a > 0) && (a <= 9) : true if a is a positive digit.

• && : conjunction. “and”

• || : disjunction. “or”

Page 21: CS 101: Numerical Computing

More examples of for

• for(j=10; j>0 ; j=j-1){ cout << j*j << endl; }

What is printed?

• for(int i=1; i < 100; i=i*2){ cout << i << endl; }

What is printed?

i can be defined inside for – then i cannot be accessed

outside of loop body ( {cout... } )

{ ... } is the scope of i.

Page 22: CS 101: Numerical Computing

And one more..

int i=0,num;

for( cin >> num; num > 1; num = num/2){ }

cout << i << endl;

num=num/2 : integer part of the quotient.

What is printed?

Page 23: CS 101: Numerical Computing

Computing ln x

• Must use arithmetic operations.

• Estimate the area under f(x) = 1/x from 1 to x.

• Area approximated by small rectangles.

Page 24: CS 101: Numerical Computing

Riemann Integral

1 x

Page 25: CS 101: Numerical Computing

How many rectangles?

• More the merrier! Say 1000.

• Total width of rectangles = x - 1.

• Width w of each = (x - 1)/1000

• x coordinate of left side of ith rectangle

1 + (i-1)w.

• Height of ith rectangle = 1/(1+(i-1)w)

Page 26: CS 101: Numerical Computing

Program to compute ln

procedure turtleMain(){

float x, area=0, w;

cin >> x; w = (x-1)/1000.0;

for(int i=1 ; i <= 1000 ; i=i+1){

area = area + w*(1/(1+(i-1)*w);

}

cout << “ln(” << x << “)=” << area << endl;

}

Page 27: CS 101: Numerical Computing

Notes

• ith iteration adds area of ith rectangle to “area”.

• It is customary to count starting at 0, so there is a zeroth rectangle, first rectangle..

• height of (new) ith rectangle = 1+ iw

• i++ : short form for i=i+1

• area += q : short form for area = area + q

Page 28: CS 101: Numerical Computing

New program

procedure turtleMain(){

float x, area=0, w;

cin >> x; w = (x-1)/1000.0;

for(int i=0; i < 1000; i++)

area += w/(1+i*w);

cout << “ln(”<< x << “)=”<< area << endl;

}

Page 29: CS 101: Numerical Computing

Homework

• Write a program that prints a conversion table from centigrade to Fahrenheit; for i = 1 to 100 it should print equivalent value in Fahrenheit. Use above procedure.

• Write a program that integrates f(x)=x. Check how much error it makes by doing a direct calculation.

• Write a program that computes xn given x and n.• Write a program that computes n! given n.