Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *

Similar presentations


Presentation on theme: "Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *"— Presentation transcript:

1 Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb * b – 4 * a * c a + bca + b * c a + b (a + b) / (c + d) c + d 1 1 / (1 + x * x) 1 + x 2 ab – (b + c)a * b - (b + c)

2 Integer Division and Remainders / (Division)  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 % (Mod)  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 the 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 3.0  If a double value is assigned to an int variable, the fractional part is lost. m = 3.9 is the same as m 3  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.5m 10 x = 17 / 2x 8.0 x = 17 / 2.0x 8.5 m = 17 / 2.0m 8

5 Operator Precedence  All operators of the same priority are listed on the same line High priority: *, /, % Low priority: +, - Operations of the same priority are evaluated from left to right. 9 – 6 – 2 3 – 2 1 8 / 4 / 2 2 / 2 1 12.0 / 4.0 * 3.0 3.0 * 3.0 9.0 4 + 3 * 5 / 2 4 + 15 / 2 4 + 7 11

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

7 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? 90 +50 ----- 140 40 Underflow Example: Using only integers, what’s 1/3? 0


Download ppt "Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *"

Similar presentations


Ads by Google