Arithmetic Calculations Chapter 7 Arithmetic Calculations Basic arithmetic operators: + addition - subtraction * multiplication / division % remainder (or modulus). Same precedence and associativity as * and / Examples: 17 % 3 = 2 (operands must be integers) 100 / 6 = 16 Division of two integers! (The answer is not 16.666667 or 17!) The Computer Continuum
More examples with % and / Chapter 7 More examples with % and / Given num is an integer. Decide if it is even or odd? if (num % 2) printf(“Odd”); else printf(“Even”); Or, we could do the following: if ((num % 2) == 0) Given that a and b are integers. Let a=10, b=3. What is a / b? Answer: 3 (Result of integer division) What is a % b? Answer: 1 (Remainder of division) The Computer Continuum
Example 1 Given the following declarations and assignments: Chapter 7 Example 1 Given the following declarations and assignments: int a, b, c, d, e; Let a=10, b=20, c=15, d=8, and e=40 What is result of the following expression? (a + b / (c – 5)) / ((d + 7) / (e - 37) % 3) Answer: (a + b / 10) / ((d + 7) / (e - 37) % 3) (a + b / 10) / (15 / (e - 37) % 3) (a + b / 10) / (15 / 3 % 3) (a + 2) / (15 / 3 % 3) 12 / (15 / 3 % 3) 12 / (5 % 3) 12 / 2 = 6 The Computer Continuum
Example 2 Given the following declarations and assignments: Chapter 7 Example 2 Given the following declarations and assignments: double a, b, c, d, e; Let a=10.0, b=20.0, c=15.0, d=8.0, and e=40.0 What is result of the following expression? (a + b / (c – 5.0)) / ((d + 7.0) / (e – 37.0) / 3.0) Answer: (a + b / 10.0) / ((d + 7.0) / (e – 37.0) / 3.0) (a + b / 10.0) / (15.0 / (e – 37.0) / 3.0) (a + b / 10.0) / (15.0 / 3.0 / 3.0) (a + 2.0) / (15.0 / 3.0 / 3.0) 12.0 / (15.0 / 3.0 / 3.0) 12.0 / (5.0 / 3.0) 12.0 / 1.666667 = 7.2 The Computer Continuum
Example 3 Given the following algebraic expression: Chapter 7 Example 3 Given the following algebraic expression: Write the corresponding C expression: (a*a*a + b*b*b) / (c*c – d*d) The Computer Continuum
Increment and Decrement Operators Chapter 7 Increment and Decrement Operators Increment and decrement are unary operators In C, for integer variables, such as count, we can write: (Post-increment): count++ instead of count=count+1 (Pre-increment): ++count instead of count=count+1 (Post-decrement): count-- instead of count=count-1 (Pre-decrement): --count instead of count=count-1 Example: int count=3 printf(“%d”, count++); /* prints 3! */ printf(“%d”, count); /* prints 4 */ The Computer Continuum
Comparison of Prefix and Postfix Increments Chapter 7 Comparison of Prefix and Postfix Increments The Computer Continuum
Compound Assignment Operators Chapter 7 Compound Assignment Operators Compound assignment operators: += assignment sum -= assignment difference *= assignment product /= assignment division %= assignment remainder Examples: a += b; /* a = a + b; */ a *= b; /* a = a * b; */ Suppose a=10 and b=12. Then, a *= ( b %= 7); /* a = 50, b = 5*/ The Computer Continuum
Chapter 7 Some Rules Rules for assigning a type to arithmetic expressions that involve integers and doubles: If one or more operators in an arithmetic expression are of type double, the result of the expression is also of type double If all operands in an arithmetic expression are of type integer, the result of the expression is also of type integer The type of the entire statement and the type of the value stored in the variable to the left of the assignment operator are the same as the type of the variable on the left Examples: double first=4.7; int second=27; second = first + second; /* first + second is 31.7! */ However, second=31 since 31.7 is converted (and truncated) to integer!! The Computer Continuum
Examples of Mixed Expressions Chapter 7 Examples of Mixed Expressions double x; double b=12.5; int a=7; x = a / 3 + b; What is the value of x? Evaluate a / 3 = 7 / 3 = 2 Evaluate 2 + b = 14.5 So, x = 14.5 The Computer Continuum
Explicit Type Conversions Chapter 7 Explicit Type Conversions Explicit type conversions are done by casting: The form of a cast operation is (Type) Expression double first = 4.7; int second = 27; (int)(first + second) is 31 first = (int)first + second; /* first is 31.0 */ first = (int)first % second; /* first is 4.0 */ first = second % (int)second; /* first is 3.0 */ The Computer Continuum
Mathematical Library Functions Chapter 7 Mathematical Library Functions Declarations are found in <math.h> and <stdlib.h> ceil(x) ceil(4.2)=5, ceil(-5.7)=-5 floor(x) floor(4.2)=4, floor(-5.7)=-6 abs(x) abs(-12)=12, abs(-12.7)=12 fabs(x) fabs(-12)=12.0, fabs(-12.8)=12.8 sqrt(x) sqrt(2)=1.414214 pow(x, y) pow(2, 3)=8 cos(x) cos(60*3.141593/180)=0.5 sin(x) sin(30*3.141593/180)=0.5 tan(x) tan(45*3.141593/180)=1.0 exp(x) exp(2.1)=8.16617 log(x) log(2)=0.693147 log10(x) log10(1000)=3.0 The Computer Continuum
Example 3 (modified) Given the following algebraic expression: Chapter 7 Example 3 (modified) Given the following algebraic expression: Write the corresponding C expression using pow(). (pow(a,3)+pow(b,3)) / (pow(c,2)–pow(d,2)) The Computer Continuum
Arithmetic Errors and Inaccuracies Chapter 7 Arithmetic Errors and Inaccuracies Division by zero (generally results in a run-time error) Arithmetic overflow When two numeric values are added or multiplied, result of operation may be in excess of max value that can be represented by the number of bits allocated for the type of target variable Arithmetic underflow Arithmetic operation results in a value that is less than the smallest value that can be stored for that data type. The computer stores a value of zero instead. Representational inaccuracies Precision limitations of floating-point data types 4.0/3.0=1.33333 (round-off errors) The Computer Continuum