Download presentation

Presentation is loading. Please wait.

Published byGabriella Newman Modified over 3 years ago

1
Arithmetic Calculations n Basic arithmetic operators: + addition - subtraction * multiplication / division %remainder (or modulus). Same precedence and associativity as * and / n Examples: 17 % 3 = 2 (operands must be integers) 100 / 6 = 16Division of two integers! (The answer is not or 17!)

2
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) printf(Even); else printf(Odd); 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)

3
Example 1 n Given the following declarations and assignments: int a, b, c, d, e; Let a=10, b=20, c=15, d=8, and e=40 n What is result of the following expression? (a + b / (c – 5)) / ((d + 7) / (e - 37) % 3) n 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

4
Example 2 n 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 n What is result of the following expression? (a + b / (c – 5.0)) / ((d + 7.0) / (e – 37.0) / 3.0) n 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 / = 7.2

5
Example 3 n Given the following algebraic expression: Write the corresponding C expression: (a*a*a + b*b*b) / (c*c – d*d)

6
Increment and Decrement Operators n 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 n Example: int count=3 printf(%d, count++); /* prints 3! */ printf(%d, count);/* prints 4 */

7
Comparison of Prefix and Postfix Increments

8
Compound Assignment Operators n 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*/

9
Some Rules n Rules for assigning a type to arithmetic expressions that involve integers and doubles: 1.If one or more operators in an arithmetic expression are of type double, the result of the expression is also of type double 2.If all operands in an arithmetic expression are of type integer, the result of the expression is also of type integer 3.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 n 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!!

10
Examples of Mixed Expressions double x; double b=12.5; int a=7; x = a / 3 + b; n What is the value of x? Evaluate a / 3 = 7 / 3 = 2 Evaluate 2 + b = 14.5 So, x = 14.5

11
Explicit Type Conversions n 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 */

12
Mathematical Library Functions n Declarations are found in and 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)= pow(x, y)pow(2, 3)=8 cos(x)cos(60* /180)=0.5 sin(x)sin(30* /180)=0.5 tan(x)tan(45* /180)=1.0 exp(x)exp(2.1)= log(x)log(2)= log10(x)log10(1000)=3.0

13
Example 3 (modified) n Given the following algebraic expression: Write the corresponding C expression using pow(). (pow(a,3)+pow(b,3)) / (pow(c,2)–pow(d,2))

14
Arithmetic Errors and Inaccuracies n Division by zero (generally results in a run-time error) n 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 n 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. n Representational inaccuracies Precision limitations of floating-point data types 4.0/3.0= (round-off errors)

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google