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!)
14
Embed
Arithmetic Calculations n Basic arithmetic operators: + addition - subtraction * multiplication / division %remainder (or modulus). Same precedence and.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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!)
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)
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
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)
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*/
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!!
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
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 */
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
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
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)