Java Reference
In-Depth Information
operands, as in
x+i
:
If either operand is of type
double
, the other is converted to
double
.
Otherwise, if either operand is of type
float
, the other is converted to
float
.
Otherwise, if either operand is of type
long
, the other is converted to
long
.
Otherwise, both operands are converted to type
int
.
This last rule can lead to confusion if adding two byte values. For example, the
code
byte a = 3, b = 6, c;
c = a+b;
does not compile because the
a
and
b
operands are automatically promoted to
type
int
before the addition occurs, meaning that the result is also an
int
. The
code then attempts to place the resulting
int
back into a
byte
type, resulting in
a “possible loss of precision” compiler error. The solution is to make an explicit
cast to
byte
:
c
=
(byte)(a + b);
2.11 Floating-point
Floating-point representation is obviously an important aspect of numerical com-
putation and how Java handles it while maintaining platform portability should be
understood. We first look at floating-point in general and then in Java. Appendix
3gives further details about Java floating-point representation and operations.
2.11.1 Floating-point basics
A floating-point number is represented in binary as
-
b
0
.b
1
b
2
b
3
...b
n-1
*2
exponent
where
b
i
represents the
i
bit in the
n
bits of the significand (also called the
mantissa). There is also a bit to indicate the sign. A floating-point value is calcu-
lated as
(-1)
s
· (b
0
+b
1
·2
-
1
+b
2
·2
-
2
+b
3
·2
-
3
+...+b
n
-
1
· 2
-
(n
-
1)
)·2
exponent
where
s
is a bit for the sign. Floating-point numbers involve a number of com-
plications with which the processor designers must deal. These complications
include:
Approximations
- The limited number of places in the significand means that only a
finite number of fractional values can be represented exactly. Similarly, the finite width
of the exponents limits the upper and lower size of the numbers.
Search WWH ::
Custom Search