Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arithmetic Operator Operation Example + addition x + y

Similar presentations


Presentation on theme: "Arithmetic Operator Operation Example + addition x + y"— Presentation transcript:

1 Arithmetic Operator Operation Example + addition x + y
- subtraction x - y * multiplication x * y / division x / y Mathematical Formula C Expressions b2 – 4ac b * b – 4 * a * c a + bc a + b * c a + b (a + b) / (c + d) c + d / (1 + x * x) 1 + x2 ab – (b + c) a * b - (b + c)

2 Integer Division and Remainders
When applied to two positive integers, the division operator computes the integral quotient. 7 / 2 is equal to 3 7.0 / 2.0 is equal to 3.5 % (Modulo) Must be applied to integers Returns the integer remainder of the result of dividing the first operand by the second. 7 % 2 is equal to 1

3 Data types of expressions
The data type of an expression depends on the data type of its operands. If both operands are of type integer, the result is integer. If both operands are of type double, the result is double. If one operand is integer and the other is double, the integer value is converted to double and the result of the operation is double. Placing a minus sign (unary - ) in front of an arithmetic expression does not change its type. - ( 3/2 ) is the value –1 - ( 3.0 / 2 ) is the value –1.5

4 Mixed-Type Assignment
int m; double x; If an int value is assigned to a double variable, the integer value is converted to double. x = 3; is the same as x If a double value is assigned to an int variable, the fractional part is lost. m = 3.9 is the same as m When an assignment statement is executed, the expression is evaluated and then the result is assigned to the variable on the left hand side of the = operator. m = 4 * 2.5 m x = 17 / 2 x x = 17 / 2.0 x m = 17 / 2.0 m

5 Operator Precedence Decreasing priority
All operators of the same priority are listed on the same line Highest priority: unary+, unary- High priority: *, /, % Low priority: , - Lowest priority: = Operations of the same priority are evaluated from left to right. 9 – 6 – – 8 / 4 / / 12.0 / 4.0 * * 4 + 3 * 5 / / Decreasing priority

6 Operator Precedence May use ( )’s to modify the order of evaluation.
12.0 / (4.0 * 3.0) / (4 + 3) * 5 / * 5 / / (4 + 3) * (5 / 2) 7 * (5 / 2) * 4 + 3 * ( 5 / 2) (3 * 2)

7 Compound operations a = a + b can be written as Increment / decrement
Similarly, for *, -, / Increment / decrement a = a + 1 can be written as a++ : means a is incremented after it is used in that expression ++a : means a is incremented before it is used in that expression E.g.: a = 4; y = a++ * 4; stores 16 in y and 5 in a But: a = 4; y = ++a * 4; stores 20 in y and 5 in a

8 Sources of Error in Real Arithmetic
Round-off error in simple addition and subtraction Loss of extra digits in multiplication and division Adding a large number to a small number Subtracting almost equal numbers Overflow and underflow Overflow Example: Using only numbers 0..99, what’s ? 90 +50 ----- Underflow Example: Using only integers, what’s 1/3?


Download ppt "Arithmetic Operator Operation Example + addition x + y"

Similar presentations


Ads by Google