Java Reference
In-Depth Information
123.45
0.0
. 01
Floating-point literals can also use exponential, or scientific, notation, in which a
number is followed by the letter e or E (for exponent) and another number. This
second number represents the power of 10 by which the first number is multiplied.
For example:
a x
1.2345E02 // 1.2345 * 10^2 or 123.45
1 e - 6 // 1 * 10^-6 or 0.000001
6.02e23 // Avogadro's Number: 6.02 * 10^23
Floating-point literals are double values by default. To include a float value literally
in a program, follow the number with f or F :
double d = 6.02E23 ;
float f = 6.02e23f ;
Floating-point literals cannot be expressed in hexadecimal, binary, or octal notation.
Floating-Point Representations
Most real numbers, by their very nature, cannot be represented exactly in any finite
number of bits. Thus, it is important to remember that float and double values are
only approximations of the numbers they are meant to represent. A float is a 32-bit
approximation, which results in at least six significant decimal digits, and a double
is a 64-bit approximation, which results in at least 15 significant digits. In Chapter 9 ,
we will cover floating-point representations in more detail.
In addition to representing ordinary numbers, the float and double types can also
represent four special values: positive and negative infinity, zero, and NaN. The
infinity values result when a floating-point computation produces a value that
overflows the representable range of a float or double . When a floating-point com‐
putation underflows the representable range of a float or a double , a zero value
results.
The Java floating-point types make a distinction between positive zero and negative
zero, depending on the direction from which the underflow occurred. In practice,
positive and negative zero behave pretty much the same. Finally, the last special
floating-point value is NaN, which stands for “Not-a-number.” The NaN value
results when an illegal floating-point operation, such as 0.0/0.0, is performed. Here
are examples of statements that result in these special values:
double inf = 1.0 / 0.0 ; // Infinity
double neginf = - 1.0 / 0.0 ; // Negative Infinity
double negzero = - 1.0 / inf ; // Negative zero
double NaN = 0.0 / 0.0 ; // Not-a-number

Search WWH ::

Custom Search