New features in the Java Math class, part 2nd: floating point numbers

Source: Internet
Author: User
Tags abstract range ranges

The 5th edition of the Java™ language specification adds 10 new methods to Java.lang.Math and Java.lang.StrictMath, and Java 6 adds 10 more. The 1th part of this two-part series introduces a new and meaningful mathematical approach. It provides a function that mathematicians are familiar with in the age when computers are not present. In the 2nd part, I'm mainly concerned with functions that are designed to manipulate floating-point numbers rather than abstract real numbers.

As I mentioned in part 1th, the difference between a real number (such as e or 0.2) and its computer representation (such as Java double) is very important. The ideal number should be infinitely accurate, whereas the number of digits represented by Java is fixed (float is 32 bits, double is 64 bits). The maximum value of float is about3.4*10. This value is not enough to indicate something, such as the number of electrons in the universe.

The maximum of double is 1.8*10308, which can represent almost any physical quantity. However, when it comes to the calculation of abstract mathematical quantities, it may be beyond the range of these values. For example, light is 171! (171 * 170 * 169 * 168 * ... * 1) exceeds the double maximum value. Float can only represent 35! The number within. A very small number (a number close to 0) can also cause trouble, and it is very dangerous to compute very large numbers and very small numbers.

To address this issue, the floating-point math IEEE 754 standard (see resources) adds special value INF and NaN, which represent infinity (Infinity) and non-numeric (not a number) respectively. IEEE 754 also defines positive 0 and minus 0 (in general mathematics, 0 is not positive or negative, but in computer mathematics they can be positive or negative). These values create confusion over the traditional principles. For example, when NaN is used, excluded middle is not established. x = = Y or x!= y are all likely to be incorrect. Neither of the two formulas is true when x or Y is NaN.

In addition to the problem of number size, precision is a more practical problem. Take a look at this common loop, add 1.0 to 10 times and wait until the result is not 10, but 9.99999999999998:

for (double x = 0.0; x <= 10.0; x += 0.1) {
   System.err.println(x);
}

For simple applications, you usually have java.text.DecimalFormat to format the final output as the nearest integer to its value, which is OK. However, in scientific and engineering applications (you cannot determine whether the result of the calculation is an integer), you need to be extra careful. If you need to perform subtraction between exceptionally large numbers to get smaller numbers, you need to be extremely careful. It is also necessary to pay attention to the divisor of a particularly small number. These operations can turn small bugs into big errors and have a huge impact on real-world applications. A small rounding error caused by a finite precision floating-point number can severely distort mathematical precision calculations.

Binary representation of floating-point numbers and double-precision digits

The IEEE 754 floating-point number implemented by Java has 32 bits. The first digit is the sign bit, 0 indicates positive, and 1 is negative. The next 8 digits represent the index, whose value ranges from 125 to +127. The last 23 digits represent the mantissa (sometimes called a valid number), and its value ranges from 0 to 33,554,431. In combination, floating point numbers are expressed in this way: Sign * mantissa * 2exponent .

A keen reader may have noticed that there is something wrong with these numbers. First, the 8 bits representing the index should be from-128 to 127, just like the signed byte. But the deviation of these indices is 126, that is, using unsigned values (0 to 255) minus 126 to get the real exponent (now from-126 to 128). But 128 and-126 are special values. When the exponent is 1 digits (128), it indicates that the number is an INF,-inf, or NaN. To determine the specific situation, you must look at its mantissa. When the index is 0 digits (-126), it indicates that the number is not normal (detailed later), but the index is still-125.

The mantissa is generally a 23-bit unsigned integer-it's very simple. 23-bit can accommodate 0 to2-1, or 16,777,215. Wait a minute, did I just say the mantissa range is from 0 to 33,554,431? That is2-1. Where did the extra one come from?

Therefore, the index can be used to indicate what the 1th position is. If the index is 0 digits, the 1th digit is 0. Otherwise the 1th digit is 1. Because we usually know what the 1th bit is, there is no need to include it in the numbers. You get an extra bit of "free". Is it a little bizarre?

The 1th digit of the mantissa is 1 floating-point number is normal. That is, the value of the mantissa is usually between 1 and 2. The 1th digit of the mantissa is 0 floating-point number is not normal, although the index is usually 125, but it is usually able to represent smaller numbers.

The double-precision number is encoded in a similar way, but it uses the 52-bit mantissa and the 11-bit exponent to obtain higher precision. The deviation of the exponent of a double number is 1023.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.