Java Reference
In-Depth Information
All real numbers that end with
f
or
F
are called
float
literals. A
float
literal can be expressed in the following
two formats:
•
Decimal number format
•
Examples of
float
literals in decimal number format are as follows:
Scientific notation
float f1 = 8F;
float f2 = 8.F;
float f3 = 8.0F;
float f4 = 3.51F;
float f5 = 0.0F;
float f6 = 16.78f;
Real number
3.25
is also written using exponential forms such as
32.5 x 10
-1
or
0.325 x 10
1
. In Java, such real
numbers can be represented as
float
literals using scientific notation. In scientific notation, the number
32.5 x 10
-1
is written as 32.5E-1. As a
float
literal, it can be written as
32.5E-1F
or
32.5E-1f
. All of the following
float
literals
denote the same real number
32.5
:
3.25F
32.5E-1F
0.325E+1F
0.325E1F
0.0325E2F
0.0325e2F
3.25E0F
The
float
data type defines two zeros:
+0.0F
(or
0.0F
) and
-0.0F
. However, for the comparison purposes, both
+0.0F
and
-0.0F
are considered equal.
The
float
data type defines two infinities: positive infinity and negative infinity. For example, the result of the
dividing
2.5F
by
0.0F
is a
float
positive infinity whereas the result of dividing
2.5F
by
-0.0F
is a
float
negative infinity.
Results of some of the operations on
float
are not defined. For example, dividing
0.0F
by
0.0F
is indeterminate.
Indeterminate results are represented by a special value of the
float
data type called
NaN
(Not-a-Number). Java has a
Float
class (Note the upper case
F
in
Float
), which defines three constants that represent positive infinity, negative
infinity, and
NaN
of the
float
data type. Table
3-3
lists these three
float
constants and their meanings. The table also
lists two constants, which represent the maximum and minimum (greater than zero)
float
values that can be stored
in a
float
variable.
Table 3-3.
float Constants Defined in the float Class
float Constants
Meaning
Float.POSITIVE_INFINITY
Positive infinity of type
float
Float.NEGATIVE_INFINITY
Negative infinity of type
float
Float.NaN
Not a Number of type
float
Float.MAX_VALUE
The largest positive value that can be represented in a
float
variable. This is equal to
3.4 x 10
38
(approx.).
Float.MIN_VALUE
The smallest positive value greater than zero that can be represented in a
float
variable. This is equal to 1.4 x 10
-45
.