cs: numerical computing
Post on 03-Jun-2018
223 Views
Preview:
TRANSCRIPT
-
8/11/2019 CS: Numerical Computing
1/29
CS 101: Numerical Computing
Abhiram Ranade
-
8/11/2019 CS: Numerical Computing
2/29
-
8/11/2019 CS: Numerical Computing
3/29
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 231is almost = 2 * 109
Actual representation is slightly different.
-
8/11/2019 CS: Numerical Computing
4/29
-
8/11/2019 CS: Numerical Computing
5/29
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.
-
8/11/2019 CS: Numerical Computing
6/29
Summary
Cohoon gives exact details. Not importantin 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 thework of man. -- Leopold Kronecker.
-
8/11/2019 CS: Numerical Computing
7/29
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 inthe 23rd digit.
float stores only 7 digits. 23rd digitignored. -- roundoff error.
w = avogadro!
-
8/11/2019 CS: Numerical Computing
8/29
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!
-
8/11/2019 CS: Numerical Computing
9/29
Simple Numerical Program
#include
procedure turtleMain(){
float c;
cout c;cout
-
8/11/2019 CS: Numerical Computing
10/29
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.
-
8/11/2019 CS: Numerical Computing
11/29
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.
-
8/11/2019 CS: Numerical Computing
12/29
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..
-
8/11/2019 CS: Numerical Computing
13/29
Reassignment in Loops
int i=1;
repeat(5){
forward(i); right(90);
forward(i); right(90);
i = i + 1;}
What will be drawn?
-
8/11/2019 CS: Numerical Computing
14/29
-
8/11/2019 CS: Numerical Computing
15/29
Nested Squares
How will you draw this pattern? Can you avoid
writing 7 calls to procedure Polygon?
Side length: 2, 4, 6, ...
-
8/11/2019 CS: Numerical Computing
16/29
Homework: draw this
Make pattern go around a polygon/circle
Number of times to spiral: read from cin
-
8/11/2019 CS: Numerical Computing
17/29
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
-
8/11/2019 CS: Numerical Computing
18/29
Spiral using for
int i;
for(i=1; i < 6; i=i+1){
forward(i); right(90);
forward(i); right(90);
}
-
8/11/2019 CS: Numerical Computing
19/29
for(xxx; yyy; zzz) { www }
0. Execute xxx;. Must be an assignmentstatement. 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.
-
8/11/2019 CS: Numerical Computing
20/29
Condition
a op b where op is , =, ==, !=
== : is equal to
!= : is not equal to
Conditions can be combined:
(a > 0) && (a
-
8/11/2019 CS: Numerical Computing
21/29
More examples of for
for(j=10; j>0 ; j=j-1){ cout
-
8/11/2019 CS: Numerical Computing
22/29
And one more..
int i=0,num;
for( cin >> num; num > 1; num = num/2){ }
cout
-
8/11/2019 CS: Numerical Computing
23/29
Computing ln x
Must use arithmetic operations.
Estimate the area under f(x) = 1/x from 1 to
x.
Area approximated by small rectangles.
-
8/11/2019 CS: Numerical Computing
24/29
Riemann Integral
1 x
-
8/11/2019 CS: Numerical Computing
25/29
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)
-
8/11/2019 CS: Numerical Computing
26/29
Program to compute ln
procedure turtleMain(){
float x, area=0, w;
cin >> x; w = (x-1)/1000.0;
for(int i=1 ; i
-
8/11/2019 CS: Numerical Computing
27/29
Notes
ith iteration adds area of ith rectangle to
area.
It is customary to count starting at 0, sothere 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
-
8/11/2019 CS: Numerical Computing
28/29
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
-
8/11/2019 CS: Numerical Computing
29/29
Homework
Write a program that prints a conversion tablefrom centigrade to Fahrenheit; for i = 1 to 100 itshould print equivalent value in Fahrenheit. Use
above procedure.
Write a program that integrates f(x)=x. Checkhow much error it makes by doing a directcalculation.
Write a program that computes xngiven x and n.
Write a program that computes n! given n.
top related