14
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 = 16Division of two integers! (The answer is not 16.666667 or 17!)

Arithmetic Calculations n Basic arithmetic operators: + addition - subtraction * multiplication / division %remainder (or modulus). Same precedence and

Embed Size (px)

Citation preview

Page 1: Arithmetic Calculations n Basic arithmetic operators: + addition - subtraction * multiplication / division %remainder (or modulus). Same precedence and

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!)

Page 2: Arithmetic Calculations n Basic arithmetic operators: + addition - subtraction * multiplication / division %remainder (or modulus). Same precedence and

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)

Page 3: Arithmetic Calculations n Basic arithmetic operators: + addition - subtraction * multiplication / division %remainder (or modulus). Same precedence and

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

Page 4: Arithmetic Calculations n Basic arithmetic operators: + addition - subtraction * multiplication / division %remainder (or modulus). Same precedence and

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

Page 5: Arithmetic Calculations n Basic arithmetic operators: + addition - subtraction * multiplication / division %remainder (or modulus). Same precedence and

Example 3

Given the following algebraic expression:

Write the corresponding C expression:

(a*a*a + b*b*b) / (c*c – d*d)

3 3

2 2

a b

c d

Page 6: Arithmetic Calculations n Basic arithmetic operators: + addition - subtraction * multiplication / division %remainder (or modulus). Same precedence and

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=3printf(“%d”, count++); /* prints 3! */printf(“%d”, count); /* prints 4 */

Page 7: Arithmetic Calculations n Basic arithmetic operators: + addition - subtraction * multiplication / division %remainder (or modulus). Same precedence and

Comparison of Prefix and Postfix Increments

Page 8: Arithmetic Calculations n Basic arithmetic operators: + addition - subtraction * multiplication / division %remainder (or modulus). Same precedence and

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*/

Page 9: Arithmetic Calculations n Basic arithmetic operators: + addition - subtraction * multiplication / division %remainder (or modulus). Same precedence and

Some Rules

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

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

Page 10: Arithmetic Calculations n Basic arithmetic operators: + addition - subtraction * multiplication / division %remainder (or modulus). Same precedence and

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

Page 11: Arithmetic Calculations n Basic arithmetic operators: + addition - subtraction * multiplication / division %remainder (or modulus). Same precedence and

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 31first = (int)first + second; /* first is 31.0 */first = (int)first % second; /* first is 4.0 */first = second % (int)second; /* first is 3.0 */

Page 12: Arithmetic Calculations n Basic arithmetic operators: + addition - subtraction * multiplication / division %remainder (or modulus). Same precedence and

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

Page 13: Arithmetic Calculations n Basic arithmetic operators: + addition - subtraction * multiplication / division %remainder (or modulus). Same precedence and

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

3 3

2 2

a b

c d

Page 14: Arithmetic Calculations n Basic arithmetic operators: + addition - subtraction * multiplication / division %remainder (or modulus). Same precedence and

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)