Presentation is loading. Please wait.

Presentation is loading. Please wait.

Numerical Computation in C C Programming by Ali iskurt 1 Numerical Computation in C Computer Programmin in C Fall Semester 2012-2013 (Slides include materials.

Similar presentations


Presentation on theme: "Numerical Computation in C C Programming by Ali iskurt 1 Numerical Computation in C Computer Programmin in C Fall Semester 2012-2013 (Slides include materials."— Presentation transcript:

1 Numerical Computation in C C Programming by Ali iskurt 1 Numerical Computation in C Computer Programmin in C Fall Semester 2012-2013 (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

2 Numerical Computation in C C Programming by Ali iskurt 2 Reminder – Reading Assignment in Kernighan & Ritchie Chapter 1 — Introduction §1.1 – Getting Started §1.2 – Variables and Arithmetic Expressions Chapter 2 — Types, Operators, and Expressions

3 Numerical Computation in C C Programming by Ali iskurt 3 Reminder – Programming Assignment #1 Due Thursday, October 11, at 15:00 Problem:– Read in the x- and y-coordinates of the three corners of a triangle Compute and print out the circumference and area of that triangle Need to know:– Reading assignment printf() and scanf() –See pp. 154 & 158 and/or Appendix B1.2 and B1.3 sqrt() – square root function

4 Numerical Computation in C C Programming by Ali iskurt 4 Review:– C is a “typed” language I.e., every data item has a “type” associated with it E.g., integer, floating point, character, structure, array, … Several purposes So compiler knows how to handle it in programs So compiler knows how to convert from one type to another So you don’t have to keep track of this crucial detail …

5 Numerical Computation in C C Programming by Ali iskurt 5 Review:– Numerical Data Types int – a signed integer, usually 16 or 32 bits long – a signed integer, usually 32 or 64 bits short – a signed integer, usually 16 bits Sizes of int, long, and short are machine dependent float – a real number in exponent-mantissa form, usually 32 bits in length double – a real number in exponent-mantissa form, usually 64 bits in length float and double are almost always IEEE standard format

6 Numerical Computation in C C Programming by Ali iskurt 6 Review:– Integer Data Types Integer types may be unsigned or signed –E.g., int i;/* signed */ long j;/* signed */ unsigned short k; –Default is signed Value ranges –signed : –2 (n-1) … +2 (n-1) -1 –unsigned : 0 … +2 (n) -1

7 Numerical Computation in C C Programming by Ali iskurt 7 Floating Point Data Types A way of representing numbers with very large or small magnitudes in computers … with a high degree of precision Equivalent to scientific notation … but in binary Examples 3.14159265358979323846 —  2.99792458  10 8 m/s — c, the velocity of light 6.626 068 85  10 -27 erg sec — h, Planck’s constant

8 Numerical Computation in C C Programming by Ali iskurt 8 Digression:– an Engineering Mystery It is known from their own writings that the ancient Egyptians … … knew the value of  to be “about 3” … did not have the mathematical sophistication to compute it to any decimal places of precision So in building the Great Pyramid, how did they make the ratio of the lengths of its sides to its height be an integer multiple of  — accurate to one part in 1000?

9 Numerical Computation in C C Programming by Ali iskurt 9 Floating Point Representation S = sign bit 0 = positive, 1 = negative Exponent Binary power of 2 to which number is raised Mantissa Binary representation of fractional part (Usually) in range 1.0 ≤ m  2.0 mantissaexponentS

10 Numerical Computation in C C Programming by Ali iskurt 10 float f; 1 sign bit 8 exponent bits 23 mantissa bits double g; 1 sign bit 11 exponent bits 52 mantissa bits Value of floating point number 1.m 0 m 1 m 2 …m 22  2 (exp-127) // float 1.m 0 m 1 m 2 ……m 51  2 (exp-1023) // double Floating Point Representation (continued) It is unlikely that you will ever have to convert from binary to decimal or vice versa “by hand”— printf() and scanf() will do it for you. There is also a long double type – 128 bits total.

11 Numerical Computation in C C Programming by Ali iskurt 11 Simple Program Example Calculate the lengths of the sides of a regular polygon, given … –the number of sides, and –the radius of the circumscribing circle r l

12 Numerical Computation in C C Programming by Ali iskurt 12 Simple Program Example (continued) #include const double pi = 3.14159265358979323846; int main () { unsigned int n; double r, length; printf(″Enter # of ″ ″sides:- ″); scanf(″%u″, &n); printf(″Enter radius″ ″:- ″); scanf(″%lf″, &r); /* calculate length */ length = 2 * r * sin(pi/n); printf(″The length of ″ ″a side of an %u-″ ″sided polygon of ″ ″radius %f is %f.″ ″\n″, length); return 0; }// main Concatenation of string constants

13 Numerical Computation in C C Programming by Ali iskurt 13 Digression:– Coding Conventions Definition:– White Space –A blank, tab, new line, vertical tab, form feed, or comment White space is ignored –However, it separates adjacent identifiers, keywords, constants, operators, etc. E.g. long double, unsigned int Rarely used!

14 Numerical Computation in C C Programming by Ali iskurt 14 Digression:– Coding Conventions (cont.) String constants (i.e., in double quotes) may not span lines! –E.g., "We, the people of the United States, in order to form …" Adjacent string constants (in double quotes) are concatenated! –E.g., "We, the people of the United" " States, in order to form …" Not allowed! Okay!

15 Numerical Computation in C C Programming by Ali iskurt 15 Digression:– Coding Conventions (cont.) Comments 1.Any sequence of characters between "/*" and "*/" 2.Any sequence of characters between "//" and end of line – … but not in a string constant (i.e., between double quotes) Comments are equivalent to white space Comments are necessary to create readable code

16 Numerical Computation in C C Programming by Ali iskurt 16 Simple Program Example (continued) #include const double pi = 3.14159265358979323846; int main () { unsigned int n; double r, length; printf(″Enter # of ″ ″sides:- ″); scanf(″%u″, &n); printf(″Enter radius″ ″:- ″); scanf(″%lf″, &r); /* calculate length */ length = 2 * r * sin(pi/n); printf(″The length of ″ ″a side of an %u-″ ″sided polygon of ″ ″radius %f is %f.″ ″\n″, n, r, length); return 0; }// main

17 Numerical Computation in C C Programming by Ali iskurt 17 Summary – Simple Example Printing Simple (concatenated) strings Strings with embedded data Scanning Unsigned integers Doubles (i.e., 64-bit floating point numbers) Assignment Value to length Function call sin Constant definition pi

18 Numerical Computation in C C Programming by Ali iskurt 18 Declaration vs. Definition Definition:– Declare – Introduce an identifier and associate it with a type No storage is created Nothing is compiled; compiler merely records information in its symbol table Definition:– Define – Create or set aside the code or storage for the object named by the identifier Storage is created and/or code is compiled Body of function is “filled in”

19 Numerical Computation in C C Programming by Ali iskurt 19 Definitions of Numeric Data unsigned int i; int j, k; short m = 0; long n = m; … Allocate a memory location big enough to hold an unsigned integer. Name of that location is i

20 Numerical Computation in C C Programming by Ali iskurt 20 Definitions of Numeric Data unsigned int i; int j, k; short m = 0; long n = m; … Allocate two memory locations big enough to hold an integer. Names of those locations are j and k Cannot count on them being in contiguous locations

21 Numerical Computation in C C Programming by Ali iskurt 21 Definitions of Numeric Data unsigned int i; int j, k; short m = 0; long n = m; … Allocate a memory location big enough to hold a short integer. –i.e., short int m = 0 Name of that location is m Initialize the value of that location to 0

22 Numerical Computation in C C Programming by Ali iskurt 22 Definitions of Numeric Data unsigned int i; int j, k; short m = 0; long n = m; … Allocate a memory location big enough to hold a long integer. Name of that location is n Initialize the value of that location to the value stored in m

23 Numerical Computation in C C Programming by Ali iskurt 23 Definitions of Numeric Data unsigned int i; int j, k; short m = 0; long n = k; … What if we had tried to initialize it to k instead? What would value of n be?

24 Numerical Computation in C C Programming by Ali iskurt 24 Definitions of Numeric Data (continued) double a; float b = 3.5; float d = 9.3e6; double e = 2.1e-8; … Same as previous, but for floating point data

25 Numerical Computation in C C Programming by Ali iskurt 25 Definitions of Numeric Data (continued) double a; float b = 3.5; float d = 9.3e6; double e = 2.1e-8; … Same as previous, but for floating point data Note the decimalized notation for the initialization

26 Numerical Computation in C C Programming by Ali iskurt 26 Definitions of Numeric Data (continued) double a; float b = 3.5; float d = 9.3e6; double e = 2.1e-8; … Same as previous, but for floating point data Scientific notation for initializations – i.e., –9.3  10 6 –2.1  10 -8

27 Numerical Computation in C C Programming by Ali iskurt 27 Questions?

28 Numerical Computation in C C Programming by Ali iskurt 28 Assignment Operator location ‘ = ’ value –Assigns the value from the right side to the memory location defined by the left E.g., –i = 3 –j = i –f = sin(x) –g = expression Note: computer scientists often refer to the location as the l-value (i.e., left value; see p.197)

29 Numerical Computation in C C Programming by Ali iskurt 29 Assignment Operator location ‘ = ’ value –Assigns the value from the right side to the memory location defined by the left E.g., –i = 3 –j = i –f = sin(x) –g = expression Assign the value 3 to the location i

30 Numerical Computation in C C Programming by Ali iskurt 30 Assignment Operator location ‘ = ’ value –Assigns the value from the right side to the memory location defined by the left E.g., –i = 3 –j = i –f = sin(x) –g = expression Assign the value from location i to location j

31 Numerical Computation in C C Programming by Ali iskurt 31 Assignment Operator location ‘ = ’ value –Assigns the value from the right side to the memory location defined by the left E.g., –i = 3 –j = i –f = sin(x) –g = expression Apply the sin function to the value at location x and store the result in location f

32 Numerical Computation in C C Programming by Ali iskurt 32 Assignment Operator location ‘ = ’ value –Assigns the value from the right side to the memory location defined by the left E.g., –i = 3 –j = i –f = sin(x) –g = expression Evaluate the expression (see below) and store the result in location g

33 Numerical Computation in C C Programming by Ali iskurt 33 Note A declaration of a variable with an initial value is equivalent to an assignment to that variable. E.g., float b = 3.5; is the same as float b; b = 3.5;

34 Numerical Computation in C C Programming by Ali iskurt 34 Definition — Expression A sequence of operands and operators that, when evaluated, produce a result value Always scanned left-to-right by compiler –However, precedence of operators may define a different order of evaluation (see below)

35 Numerical Computation in C C Programming by Ali iskurt 35 Arithmetic Operators Unary – ‘ + ’ and ‘ – ’ Indicates sign of number Additive – ‘ + ’ and ‘ – ’ Adds or subtracts two numbers, returns sum or difference Multiplicative – ‘ * ’, ‘ / ’, and ‘ % ’ ‘ * ’ – multiplies to numbers together, returns product ‘ / ’ – divides first number by second, returns quotient ‘ % ’ – integer division, returns remainder …

36 Numerical Computation in C C Programming by Ali iskurt 36 Arithmetic Expressions a*x + b c*c + 2*c*d + d*d c*c*c + 3*c*c*d + 3*c*d*d + d*d*d 1/(1/v1 + 1/v2) (minutes1 + minutes2) % 60

37 Numerical Computation in C C Programming by Ali iskurt 37 Arithmetic Expressions (continued) Arithmetic expressions always return a value of the same type as their operands Type conversion rules apply if operands are of mixed types See §A.2, pp. 197-198 More later ‘ / ’, and ‘ % ’ are undefined if divisor is zero

38 Numerical Computation in C C Programming by Ali iskurt 38 Assignment Operator (again) ‘ = ’ — assigns value of the expression on the right to memory location defined by left y = a*x + b; i = j + 1; z = y = a*x + b; Note: assignment is just another operator in an expression Value of the assignment expression is the value assigned

39 Numerical Computation in C C Programming by Ali iskurt 39 Assignment Expression ‘ = ’ — assigns value of the expression on the right to memory location defined by left Returns the value assigned y = a*x + b; i = j + 1; z = y = a*x + b; Evaluate this expression first

40 Numerical Computation in C C Programming by Ali iskurt 40 Assignment Expression (continued) ‘ = ’ — assigns value of the expression on the right to memory location defined by left Returns the value assigned y = a*x + b; i = j + 1; z = y = a*x + b; 40 Assign the result here

41 Numerical Computation in C C Programming by Ali iskurt 41 Assignment Expression (continued) ‘ = ’ — assigns value of the expression on the right to memory location defined by left Returns the value assigned y = a*x + b; i = j + 1; z = y = a*x + b; 41 Assign that result here

42 Numerical Computation in C C Programming by Ali iskurt 42 Questions?

43 Numerical Computation in C C Programming by Ali iskurt 43 Specifying Symbolic Constants in C Two ways –Textual substitution –Declaration of const data object

44 Numerical Computation in C C Programming by Ali iskurt 44 Constant – Textual Substitution See page 14 & 89 in K&R #define NAME replacement-text E.g., #define PI 3.14159265358979323846 #define LOWER 0 #define UPPER 300 #define STEP 20 It is traditional in C for textual substitution names to be all UPPER CASE

45 Numerical Computation in C C Programming by Ali iskurt 45 Constant – Textual Substitution See page 14 & 89 in K&R #define NAME replacement-text E.g., #define PI 3.14159265358979323846 #define LOWER 0 #define UPPER 300 #define STEP 20 When a textual substitution constant is used in a program, the compiler simply substitutes the replacement text on the fly

46 Numerical Computation in C C Programming by Ali iskurt 46 Constant Declaration const double pi = 3.14159265358979323846; const double c = 2.99792458e+8; /* speed of light in meters/sec */ Defines a value of the declared type with the declared name I.e., creates storage to hold this value Must be initialized May never be left side of an assignment

47 Numerical Computation in C C Programming by Ali iskurt 47 Questions?

48 Numerical Computation in C C Programming by Ali iskurt 48 Introduction to Operator Precedence Suppose you encounter the following expressions in a math, physics, or engineering problem:– How do you represent them in C and what order should the operators be evaluated?

49 Numerical Computation in C C Programming by Ali iskurt 49 Arithmetic Expressions with Multiple Operators pow(x,3) + 3*pow(x,2)*y + 3*x*pow(y,2) + pow(y,3) 1 st 2 nd 3 rd 4 th 5 th 6 th 8 th 7 th 9 th 10 th 11 th Value of expression

50 Numerical Computation in C C Programming by Ali iskurt 50 Exercise – Do the same for this expression Representation as a C expression Order of operations

51 Numerical Computation in C C Programming by Ali iskurt 51 Definitions Operator Precedence –The relative order in which operators in an expression in C are executed Operator Associativity –When two operators are of same precedence, whether the left or right operator is applied first See Table 2-1, p. 53 A very important topic. Easy to get tripped up. Source of lots of errors!

52 Numerical Computation in C C Programming by Ali iskurt 52 Operator Precedence Table A subset of the table is:– ( )/* highest – function call and parenthesization */ + -/* unary, right to left*/ * / %/* two operands, left to right */ + -/* two operands, l-to-r*/ =/* assignment, right to left */,/* lowest – sequence of expressions */

53 Numerical Computation in C C Programming by Ali iskurt 53 Operator Precedence When scanning an expression … A pending operator is not applied until it is of higher precedence than the next operator or It has the same precedence and associativity is left-to-right

54 Numerical Computation in C C Programming by Ali iskurt 54 Arithmetic Expressions with Multiple Operators pow(x,3) + 3*pow(x,2)*y + 3*x*pow(y,2) + pow(y,3) Highest – evaluate within () before anything else Low – look ahead to see if next operator is higher Medium – look ahead to see if next operator is lower

55 Numerical Computation in C C Programming by Ali iskurt 55 Warning Operator precedence does not specify order of evaluation of functions E.g., –x = f(arg1) + g(arg2); –f and g may be evaluated in either order! –arg1 and arg2 may be evaluated in either order Exceptions:– –&&, ||, ?:, and ',' Important if f and g have side effects

56 Numerical Computation in C C Programming by Ali iskurt 56 How does the compiler implement precedence? Answer:– using a data structure called a stack Definition:– Stack A linear data structure comprising a sequence of items, such that the last item added is the first item removed

57 Numerical Computation in C C Programming by Ali iskurt 57 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Detect constant token representing value 1 Place value 1 on value-location stack 1 values&locationsoperators

58 Numerical Computation in C C Programming by Ali iskurt 58 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover ‘ / ’ token Place on operator stack 1 values&locations / operators

59 Numerical Computation in C C Programming by Ali iskurt 59 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover ‘ ( ’ token –Start of a “sub expression” –Highest precedence of all Place matching ‘ ) ’ on operator stack 1 values&locations / operators ()

60 Numerical Computation in C C Programming by Ali iskurt 60 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover constant token 1 (again) Place on top of value- location stack 1 values&locations / operators ()1

61 Numerical Computation in C C Programming by Ali iskurt 61 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover ‘ / ’ token –Higher precedence than ‘ ( ’ Place ‘ / ’ on operator stack 1 values&locations / operators ()1 /

62 Numerical Computation in C C Programming by Ali iskurt 62 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover identifier token v1 Place on value- location stack 1 values&locations / operators ()1 /v1

63 Numerical Computation in C C Programming by Ali iskurt 63 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover token ‘ + ’ –Note that ‘ + ’ has lower precedence than ‘ / ’ –Cannot place it above ‘ / ’ Emit code to calculate 1/v1 –Consumes ‘ / ’ operator and top two values from stacks Place result on value- location stack Place on operator stack 1 values&locations / operators ()1 /v1

64 Numerical Computation in C C Programming by Ali iskurt 64 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Stacks now look like this 1 values&locations / operators ()1/v1 +

65 Numerical Computation in C C Programming by Ali iskurt 65 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover yet another value 1 –Place on stack 1 values&locations / operators ()1/v1 +1

66 Numerical Computation in C C Programming by Ali iskurt 66 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover yet another ‘ / ’ operator –Higher precedence than top of operator stack Place on operator stack 1 values&locations / operators ()1/v1 +1 /

67 Numerical Computation in C C Programming by Ali iskurt 67 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover yet another identifier v2 Place on value- location stack 1 values&locations / operators ()1/v1 +1 /v2

68 Numerical Computation in C C Programming by Ali iskurt 68 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover ‘ ) ’ token –Finish all operations back to ‘ ()’ 1 values&locations / operators ()1/v1 +1 /v2

69 Numerical Computation in C C Programming by Ali iskurt 69 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Emit code to do ‘ / ’ operation –Consumes top two values on the stack Places result on value location stack 1 values&locations / operators ()1/v1 +1 /v2

70 Numerical Computation in C C Programming by Ali iskurt 70 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Emit code for ‘ + ’ operation –Places result (i.e., sum ) on value location stack 1 values&locations / operators ()1/v1 +1/v2

71 Numerical Computation in C C Programming by Ali iskurt 71 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Delete ‘ () ’ 1 values&locations / operators ()sum

72 Numerical Computation in C C Programming by Ali iskurt 72 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Discover end of expression Emit code for ‘ / ’ operation Place result on value location stack 1 values&locations / operators sum

73 Numerical Computation in C C Programming by Ali iskurt 73 Understanding Operator Precedence Example:– 1/(1/v1 + 1/v2) ^ Final result is value of the expression 1/sum values&locationsoperators

74 Numerical Computation in C C Programming by Ali iskurt 74 Questions?


Download ppt "Numerical Computation in C C Programming by Ali iskurt 1 Numerical Computation in C Computer Programmin in C Fall Semester 2012-2013 (Slides include materials."

Similar presentations


Ads by Google