Brought to you by EarthWeb
ITKnowledge Logo Login Graphic IBM DB2 Universal Database.
IBM DB2 Universal Database.
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Finite precision

It’s important to understand that not all floating-point numbers can be exactly represented in a finite number of bits. For example, whereas one half is exactly 0.1 (binary) or 0.5 (decimal), one third in binary is 0.0101010101 . . . where the pattern repeats indefinitely. One third also repeats in decimal notation where it’s 0.33333333 . . . . Whether or not a number repeats or terminates depends on the base of the number system. One fifth is exactly 0.2 in decimal, but is 0.0011001100110011 . . . in binary. Some numbers, most famously [Pi], neither terminate nor repeat. Because computer arithmetic must truncate these infinite mantissas to just 24 bits, computer arithmetic on floats is often imprecise. The best Java can do with a number like [Pi] is approximate with an accuracy of 24 bits.

Doubles

If a float is not precise enough or large enough, you can use a double instead. A double has eight bytes, of which 1 bit is used for the sign, 11 bits for the exponent, and 53 bits for the mantissa. If you’re sharp, you’ll notice that this adds up to 65 bits. Don’t forget that the first bit of the mantissa is always 1, so you don’t need to store that bit. The exponent is biased by subtracting 1023.

Special values

Java’s floating-point numbers aren’t limited to the rational numbers you learned in high school. There are several special numbers that, while not true numbers in the traditional sense of the word, are produced by some calculations. If the non-biased exponent is 255, then the number takes on one of several special meanings.

Inf

Java has two special floating-point values to represent positive and negative infinity. There’s no literal for these infinities, but the public final static float values java.lang.Float.POSITIVE_INFINITY and java.lang.Float. NEGATIVE_INFINITY allow you to use them in source code.

More commonly you’ll bump across these values unexpectedly when a calculation goes in a direction that you didn’t anticipate. Positive infinity is produced when a positive float or a double is divided by zero. Dividing a negative float or double by zero gives negative infinity. For example:

     double x = 1.0/0.0;

There’s little reason to deliberately create a float or double that’s infinite. However, it is a rather common thing to create one accidentally in more complicated programs where all possible divisors aren’t determined until runtime. The Inf value lets your programs continue without crashing or throwing an exception.

You can get the value Inf only in a floating-point calculation. If you try to divide an integer by integer zero, an ArithmeticException is thrown instead. For example:

     int i = 1/0;

In a comparison test with <, <=, >, or >=, -Inf is smaller than any other number and Inf is larger than any other number. Each is equal only to itself.

The bit patterns for positive infinity and negative infinity are formed by the appropriate sign bit (1 for negative, 0 for positive), an unbiased exponent of 255 (11111111), and a mantissa of zero. Thus, positive infinity is 01111111100000000000000000000000, or in hexadecimal, 7F800000. Negative infinity is 11111111100000000000000000000000, or in hexadecimal, FF800000.

Double positive and negative infinity are formed in the same way. Choose the appropriate sign, fill the exponent with one bits, and set the mantissa to zero. Thus, positive double infinity is 7FF0000000000000 and negative double infinity is FFF0000000000000.

NaN

NaN is an acronym for “Not a Number.” A floating-point calculation returns NaN if it divides zero by zero. For example:

     double z = 0.0/0.0;

You can also get NaN values in certain other undefined arithmetic operations, such as taking the square root of a negative number or raising zero to the zeroth power.

There is no literal that lets you type NaN into Java source code, but you can get the same effect with the public, final, static float constant java.lang.Float.NaN.

More commonly, NaN will pop up unexpectedly. For example, the following code fragment divides 0.0 by 0.0 when x is equal to 5.0:

     double y = 10.0;
     for (double x = 0.0; x <= y; x+=1.0, y -= 1.0) {
       double z = x - 5.0;
       double result = (x - y)/z;
       System.out.println(x + " " + y + " " + z + " " + result);
     }

NaN is unordered, so the result will always be false if you compare it to other numbers with <, <=, >, >=, or ==. The only comparison that can return true is !=, which always returns true if one or both of the operands is NaN. In other words, NaN is never equal to any number (including itself), never greater than any number, and never less than any number.

Although division by zero does not crash your program like it does in some programming languages, the unexpected appearance of NaNs or Infs in program output generally indicates a bug that needs to be stomped. Real world quantities shouldn’t be infinite or “Not a Number.” If you see NaNs or Infs, it may be an indication that a small factor you left out of your analysis, friction for example, is becoming important in a special case because everything else is canceling out.

NaN is represented by any float or double bit pattern in which the exponent is all ones and the mantissa is non-zero. (If the mantissa is zero, then the number is either positive or negative infinity.) The sign bit is ignored because NaN is not signed. Thus, all floats from 7F800001 to 7FFFFFFF and from FF800001 to FFFFFFFF correspond to NaN. All doubles from 7FF0000000000001 to 7FFFFFFFFFFFFFFF and from FFF0000000000001 to FFFFFFFFFFFFFFFF also correspond to NaN.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.