1. Unary
arithmetic operator
Work on single operand
-ve and + ve example-
y=-x ; or y=+x;
2. Binary
arithmetic operator
Work on two operand
Operator
|
Description
|
Usage
|
Examples
|
*
|
Multiplication
|
expr1 * expr2
|
2 * 3 → 6
3.3 * 1.0 → 3.3 |
/
|
Division
|
expr1 / expr2
|
1 / 2 → 0
1.0 / 2.0 → 0.5 |
%
|
Remainder (Modulus)
|
expr1 % expr2
|
5 % 2 → 1
-5 % 2 → -1 5.5 % 2.2 → 1.1 |
+
|
Addition
(or unary positive) |
expr1 + expr2
+expr |
1 + 2 → 3
1.1 + 2.2 → 3.3 |
-
|
Subtraction
(or unary negate) |
expr1 - expr2
-expr |
1 - 2 → -1
1.1 - 2.2 → -1.1 |
Arithmetic Expressions
In programming, the following arithmetic expression:
must be written as (1+2*a)/3 + (4*(b+c)*(5-d-e))/f -
6*(7/g+h). You cannot omit the multiplication symbol (*), as in Mathematics.
Rules
on Precedence Like Mathematics:
·
The multiplication (*), division (/) and
remainder (%) take precedence over addition (+) and subtraction (-). For
example, 1+2*3-4/2 is interpreted as 1+(2*3)-(4/2).
·
Unary '+' (positive) and '-' (negate) have
higher precedence.
·
Parentheses () have the highest precedence and
can be used to change the order of evaluation.
·
Within the same precedence level (e.g., addition
and subtraction), the expression is evaluated from left to right (called
left-associative). For example, 1+2-3+4 is evaluated as ((1+2)-3)+4 and 1*2%3/4
is ((1*2)%3)/4.
Mixed-Type Operations
·
The arithmetic operators are only applicable to
primitive numeric types: byte, short, int, long, float, double, and char. These
operators do not apply to boolean.
·
If both operands are int, long, float or double,
the arithmetic operations are carried in that type, and evaluated to a value of
that type, i.e., int 5 + int 6 → int 11; double 2.1 + double 1.2 → double 3.3.
·
It is important to take note int division
produces an int, i.e., int/int → int, with the result truncated, e.g., 1/2 → 0,
instead of 0.5?!
·
If both operand are byte, short or char, the
operations are carried out in int, and evaluated to a value of int. A char is
treated as a 16-bit unsigned integer in the range of [0, 65535]. For example,
byte 127 + byte 1 → int 127 + int 1 → int 128.
·
If the two operands belong to different types,
the value of the smaller type is promoted automatically to the larger type
(known as implicit type-casting). The operation is then carried out in the
larger type, and evaluated to a value in the larger type.
·
byte, short or char is first promoted to int
before comparing with the type of the other operand.
·
The order of promotion is: int → long → float →
double.
For examples,
1.
int/double →
double/double → double. Hence, 1/2 → 0, 1.0/2.0 → 0.5, 1.0/2 → 0.5, 1/2.0 →
0.5.
2.
char + float → int +
float → float + float → float.
3.
9 / 5 * 20.1 → (9 /
5) * 20.1 → 1 * 20.1 → 1.0 * 20.1 → 20.1 (You probably don't expect this
answer!)
4.
byte 1 + byte 2 →
int 1 + int 2 → int 3 (The result is an int, NOT byte!)
5.
byte b1 = 1, b2 = 2;
6.
byte b3 = b1 +
b2; // Compilation Error: possible loss
of precision
//
b1+b2 returns an int, cannot be assigned to byte
The type-promotion rules for
binary operations can be summarized as follows:
1. If
one of the operand is double, the other operand is promoted to double;
2. Else
If one of the operand is float, the other operand is promoted to float;
3. Else
If one of the operand is long, the other operand is promoted to long;
4. Else
both operands are promoted to int.
The type-promotion rules for
unary operations (e.g., negate '-') can be summarized as follows:
1. If
the operand is double, float, long or int, there is no promotion.
2.
Else (the operand is byte, short, char), the
operand is promoted to
int
.
For example,
byte b1 = 1;
byte b2 = -b1; //
Compilation Error: possible loss of precision
// -b1
returns an int, cannot be assigned to byte
Remainder (Modulus) Operator
To evaluate the remainder (for negative and floating-point
operands), perform repeated subtraction until the absolute value of the
remainder is less than the absolute value of the second operand.
For example,
-5 % 2 ⇒ -3 % 2 ⇒ -1
5.5 % 2.2 ⇒ 3.3 % 2.2 ⇒ 1.1
Exponent?
Java does not have an exponent operator. (The '^' operator
is for exclusive-or, NOT exponent). You need to use method Math.exp(x, y) to evaluate x raises to power y.
Overflow/Underflow
Study the output of the following program:
/*
* Illustrate
"int" overflow
*/
public class OverflowTest {
public static void main(String[] args) {
// Range of int is [-2147483648,
2147483647]
int i1 = 2147483647; // maximum int
System.out.println(i1 + 1); // -2147483648 (overflow!)
System.out.println(i1 + 2); // -2147483647
System.out.println(i1 * i1); // 1
int i2 = -2147483648; // minimum int
System.out.println(i2 - 1); // 2147483647 (overflow!)
System.out.println(i2 - 2); // 2147483646
System.out.println(i2 * i2); // 0
}
}
·
In arithmetic operations, the resultant value
wraps around if it exceeds its range (i.e., overflow). Java runtime does NOT
issue an error/warning message but produces an incorrect result.
·
On the other hand, integer division produces an
truncated integer and results in so-called underflow. For example, 1/2 gives 0,
instead of 0.5. Again, Java runtime does NOT issue an error/warning message,
but produces an imprecise result.
·
It is important to take note that checking of
overflow/underflow is the programmer's responsibility. i.e., your job!!!
·
Why computer does not flag overflow/underflow as
an error? This is due to the legacy design when the processors were very slow.
Checking for overflow/underflow consumes computation power. Today, processors
are fast. It is better to ask the computer to check for overflow/underflow (if
you design a new language), because few humans expect such results.
·
To check for arithmetic overflow (known as secure
coding) is tedious. Google for "INT32-C. Ensure that operations on signed
integers do not result in overflow" @ www.securecoding.cert.org.
No comments:
Post a Comment
Please write your view and suggestion....