Floating-Point Variables

   

Floating-Point Variables

Floating-point numbers are the last category of primitive types that need to be covered. Floating-point numbers are used to represent numbers that have a decimal point in them (such as 5.3 or 99.234). Whole numbers can also be represented, but as a floating point, the number 5 is actually 5.0.

In Java, floating-point numbers are represented by the types float and double. Both of these follow a standard floating-point specification: IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Std. 754-1985 (IEEE, New York). The fact that these data types follow this specification ”no matter what machine an application or applet is running on ”is one of the details that makes Java so portable. In other languages, floating-point operations are defined for the floating-point unit (FPU) of the particular machine the program is executing on. For example, this means that the representation of 5.0 on an IBM PC might not be the same as on a DEC VAX.

Table 3.5 summarizes the numeric ranges supported for each of the floating-point types. It also defines the default value assigned to a float or double attribute of a class if it isn't explicitly initialized as part of its declaration.

Table 3.5. Floating-Point Types and Their Supported Ranges
Floating Point Type Maximum Positive Value Default Value Minimum Value
float 1.40239846e “45f 3.40282347e+38f
double 4.94065645841246544e “324d 1.7976931348623157e+308d

In addition to representing normal floating-point numbers, there are five unique states that can be assigned to float and double variables:

  • Negative infinity

  • Positive infinity

  • Positive zero

  • Negative zero

  • Not-a-Number (NaN)

These states are required, by definition of the IEEE 754-1985 standard, in part to account for rollover. For instance, adding one to the maximum number that can be stored in a floating-point type results in a positive infinity result. When an operation results in a number too small to be represented, this is an underflow condition that is represented by a zero state. Unlike some integer operations, floating-point arithmetic never results in an exception. Dividing by zero produces a positive infinity or negative infinity result depending on the sign of the non-zero operand (NaN results if both operands are zero). Positive or negative infinity also results if a positive operand is divided by a positive or negative zero, respectively. The Float wrapper class introduced in Chapter 7, "Classes," provides several useful methods for working with these special states.

Most of the operations that can be applied to integers have an analogous operation for floating-point numbers. The exceptions are the bitwise operations. The operators that can be used in expressions of type float or double are given in Table 3.6.

Table 3.6. Operations on float and double Expressions
Operation Description
=, +=, “=, *=, /= Assignment operators
==, != Equality and inequality operators
<, <=, >, >= Relational operators
+, “ Unary sign operators
+, “, *, /, % Addition, subtraction, multiplication, division, and remainder operators
+=, “=, *=, /= Addition, subtraction, multiplication, division, and assign operators
++, “ “ Increment and decrement operators
   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net