CS 101: Numerical Computing Abhiram Ranade. Representing Integers “int x;” : reserves one cell in memory for x. One cell: “One word” has 32 capacitors.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Computer Science 1620 Loops.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Overview Order of presentation different than published handouts Run a program on ccc Finish Arithmetic operations Data types integer char floating point.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
NUMBER REPRESENTATION CHAPTER 3 – part 3. ONE’S COMPLEMENT REPRESENTATION CHAPTER 3 – part 3.
Computer Science 111 Fundamentals of Programming I Number Systems.
Chapter 2 part #4 Operator
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Data Representation.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Representing numbers and Basic MATLAB 1. Representing numbers  numbers used by computers do not behave the same as numbers used in mathematics  e.g.,
CS 101: “If” and Recursion Abhiram Ranade. This week’ lab assignment: Write a program that takes as input an integer n and a sequence of numbers w1,w2,...,wn.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Chapter 3 Number Representation. Convert a number from decimal to binary notation and vice versa. Understand the different representations of an integer.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
CPS120: Introduction to Computer Science Operations Lecture 9.
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
C-1 9/30/99 CSE / ENGR 142 Programming I Arithmetic Expressions © 1999 UW CSE.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Introduction to Computer Programming
CSE 220 – C Programming Expressions.
Chapter 2: Introduction to C++
Data Types, Variables & Arithmetic
Chapter 2 Assignment and Interactive Input
Relational Operations
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Assignment and Arithmetic expressions
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Variables Kingdom of Saudi Arabia
Fundamentals of Programming I Number Systems
Chapter 7 Additional Control Structures
ECEG-3202 Computer Architecture and Organization
Number Representation
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Chapter 3 Operators and Expressions
Data Types and Expressions
Summary of what we learned yesterday
Chap 7. Advanced Control Statements in Java
Presentation transcript:

CS 101: Numerical Computing Abhiram Ranade

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?

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 = Min negative number: H H...H = is almost = 2 * 10 9 Actual representation is slightly different.

How to store larger numbers? Use Long integers: “ long x;” Reserves 2 words, with one sign bit. Max magnitude , about Use floating point representation.

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.

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.

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!

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!

Simple Numerical Program #include procedure turtleMain(){ float c; cout << “Centigrade temperature:” << endl; cin >> c; cout << “Fahrenheit:” << (c*9/5)+32; }

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.

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.

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..

Reassignment in Loops int i=1; repeat(5){ forward(i); right(90); i = i + 1; } What will be drawn?

Nested Squares How will you draw this pattern? Can you avoid writing 7 calls to procedure Polygon? Side length: 2, 4, 6,...

Homework: draw this Make pattern go around a polygon/circle Number of times to spiral: read from cin

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

Spiral using for int i; for(i=1; i < 6; i=i+1){ forward(i); right(90); }

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.

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”

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.

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?

Computing ln x Must use arithmetic operations. Estimate the area under f(x) = 1/x from 1 to x. Area approximated by small rectangles.

Riemann Integral 1 x

How many rectangles? More the merrier! Say 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)‏

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; }

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

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; }

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 x n given x and n. Write a program that computes n! given n.