Type double
Overview
The values of type double are approximations
to the real numbers. Examples are
1.8 3.0 0.000005
We do not make heavy use of type double in
this course, and we present only a basic outline here. For more information
see section 1.1.2 (page 18) in Gries/Gries. Also, use Gries/Gries, section
6.4, pp. 221–224 and lesson pages 6–3 and 6–4 on the
Program live CD as references for type double.
Values of type double are approximations to
the real numbers because real numbers have an infinite number of decimal
places while each double number occupies a fixed number
of bits. There are an infinite number of real values and only a finite
number of double values. For example,
0.111111111111111111111111111111 in the DrJava interactions
pane evaluates to
0.1111111111111111
And, amazingly,
1.0/9.0 in the interactions pane evaluates to 0.1111111111111111 but
0.1/0.9 in the interactions pane evaluates to 0.11111111111111112 .
In mathematics, we use scientific notation, like 3.1*107,
which stands for 3.1 multiplied by 10,000,000. In Java, this number would
be written as 3.1E7 . In this notation, the number to the left of E is called
the mantissa and the number to the right is called the exponent.
The mantissa is truncated to about 16–17 decimal digits.
Obtain the maximum and minimum positive values using:
Double.MIN_VALUE: 4.9E-324
Double.MAX_VALUE: 1.7976931348623157E308
The following table gives the operations of type double .
They perform as expected, except that "round-off"
errors will occur because of the finite nature of double numbers.
Operation |
Name |
Example |
unary - |
negation |
- 3.8 |
unary + |
no-op |
+7.13E11 |
+ |
addition |
1.3E7 + 2.4E8 |
- |
subtraction |
3.14 - 2.73 |
* |
multiplication |
8.0 * 2.5 |
/ |
division |
1.0 / 9.0 |
% |
remainder |
5.1 % 2.0 |
If the operands of unary minus, - , + , * , / ,
% are of type int ,
the operation is an int operation. If at least one operand
is double , the operation is a double operation,
and, if necessary, the other operation is first converted to type double .
|