[Reprinted] floating point number of Java

Source: Internet
Author: User

Although almost every processor andProgramming LanguageAll support floating point operations, but mostProgramStaff seldom pay attention to it. This is easy to understand-most of us seldom need non-integer types. In addition to scientific computing and occasional timing or benchmarking procedures, it is almost useless in other cases. Similarly, most developers can easily ignore arbitrary decimal numbers provided by Java. Math. bigdecimal-they are not used by most applications. However, in an integer-based program, it is sometimes unexpectedly necessary to represent non-integer data. For example, JDBC uses bigdecimal as the preferred interchange format for SQL decimal columns.

IEEE floating point
JAVA supports two basic floating point types: float and double, and the packaging classes Float and double corresponding to them. Both are based on the IEEE 754 standard, which defines binary standards for 32-bit floating point and 64-bit double-precision floating point binary decimals.

IEEE 754 uses a scientific notation to represent a floating point number with a base number of 2 decimal places. The IEEE floating point number uses a 1-digit symbol to represent the number, an 8-digit symbol to represent the index, and a 23-digit symbol to represent the ending number, that is, the fractional part. Indexes that are signed integers can be positive or negative. The decimal part is represented by a decimal number (base number 2), which means that the highest bit corresponds to the value? (2-1), the second digit corresponds? (2-2), and so on. For double-precision floating-point numbers, 11 bits are used to represent the index, and 52 bits are used to represent the ending number. The format of the IEEE floating point value is 1.
Figure 1. IEEE 754 floating point format

Because scientific notation can be used to represent a given number in multiple ways, it is necessary to normalize the floating point so that it can be expressed by a decimal point with a base number of 2 and a decimal point left of 1, adjust the index as needed to obtain the required number. Therefore, for example, the number 1.25 can be expressed as the ending number of 1.01, and the index is 0: (-1) 0*1.012*20

The number 10.0 can be expressed as the ending number of 1.01, and the index is 3: (-1) 0*1.012*23

Special Number

Except for the standard range of values allowed by encoding (for float, from 1.4e-45 to 3.4028235e + 38 ), there are also some special values that represent infinity, negative infinity,-0, and Nan (which represent "not a number. The existence of these values is to use a number in the floating point value set to represent the results in the case of an error condition (such as Arithmetic overflow, square root of a negative number, divided by 0.

These special numbers have some unusual characteristics. For example, 0 and-0 are different values, but they are considered equal when they are compared. Divide the number by a non-zero number and the result is 0. The special number Nan is unordered. When the =, <, and> operator is used to compare nan with other floating point values, the result is false. If F is Nan, false is returned even if (F = F. If you want to compare the floating point value with Nan, use the float. isnan () method. Table 1 shows the infinite and Nan attributes.
Table 1. Attributes of special floating point values

Expression Result
Math. SQRT (-1.0) Nan
0.0/0.0 Nan
1.0/0.0 Infinity
-1.0/0.0 Negative infinity
Nan + 1.0 Nan
Infinity + 1.0 Infinity
Infinity + infinity Infinity
Nan> 1.0 False
Nan = 1.0 False
Nan <1, 1.0 False
Nan = Nan False
0.0 =-0.01 True

The basic floating point type and the packaging floating point type have different comparison behaviors.

To make things worse, the rules used to compare Nan and-0 are different between the basic float type and the package class float. If the float value is equal to the two Nan values, false is returned. If the float. Equals () is used to compare the two Nan float objects, true is returned. This is because, if not, the Nan float object cannot be used as a key in hashmap. Similarly, although 0 and-0 are considered equal when they are expressed as floating point values, float is used. when compareto () is used to compare the 0 and-0 values of a float object, the-0 value is smaller than 0.

Risks in floating point

Due to the special behavior of infinity, Nan, and 0, when floating point numbers are applied, seemingly harmless conversion and optimization are actually incorrect. For example, although 0.0-F is obviously equal to-F, it is incorrect when F is 0. There are other similar gotcha, some of which are shown in table 2.
Table 2. Invalid floating point assumptions

This expression Not necessarily equal When
0.0-F -F F is 0
F <G ! (F> = g) F or G is Nan
F = f True F is Nan
F + G-G F G is infinite or Nan

Rounding Error

Floating point operations are rarely accurate. Although some numbers (such as 0.5) can be accurately expressed as binary (base number 2) decimal places (because 0.5 is equal to 2-1), some other numbers (such as 0.1) it cannot be accurately expressed. Therefore, floating-point operations may cause rounding errors, resulting in near-but not equal-possible results. For example, the following simple calculation will get 2.600000000000001 instead of 2.6:

 
Double S = 0; For (INT I = 0; I <26; I ++) S + = 0.1; system. Out. println (s );

Similarly, the result produced by multiplying. 1*26 is not equal to the result obtained by adding. 1 itself 26 times. When a floating point number is forcibly converted to an integer, the rounding error is even more serious, because the forced conversion to an integer type will discard the non-integer part, this type of problem exists even for calculations that "seem" to be expected to get an integer. For example, the following statements:

 
Double D = 29.0*0.01; system. Out. println (d); system. Out. println (INT) (D * 100 ));

The following output is displayed:

0.29

28 this may not be what you expected at first.

Floating Point comparison Guide

Due to the unusual comparative behaviors of Nan and the inevitable rounding error in almost all floating point calculations, it is troublesome to explain the comparison operators of floating point values.

It is best to avoid comparing floating point numbers. Of course, this is not always possible, but you should be aware that you must limit the floating point comparison. If you have to compare floating point numbers to see if they are equal, you should compare the absolute values of their differences with some pre-selected small positive numbers so that what you do is to test whether they are "close enough ". (If you do not know the basic computing scope, you can use the test "ABS (a/B-1) <Epsilon". This method is more accurate than simply comparing the two.). It is even dangerous to test whether a value is greater than zero or smaller than zero. "Think" will generate a calculation of a value slightly greater than zero. In fact, it may be slightly smaller than zero because of the accumulated rounding error..

The unordered nature of Nan makes it more prone to errors when comparing floating point numbers. When we compare floating point numbers, we will focus on the infinite and Nan problems,An empirical rule to avoid Gotcha is to explicitly test the value validity, rather than trying to exclude invalid values.. In listing 1, there are two possible setter implementations for the feature. This feature can only accept non-negative values. The first implementation will accept Nan, and the second will not. The second form is better, because it explicitly detects the range of values that you think are valid.

Listing 1. Better methods and solutions for non-negative floating point values

// Trying to test by exclusion -- this doesn't catch Nan or infinitypublic void setfoo (float Foo) {If (FOO <0) throw new illegalargumentexception (float. tostring (f); this. foo = Foo;} // testing by Division sion -- this does catch nanpublic void setfoo (float Foo) {If (FOO> = 0 & Foo <float. infinity) This. foo = Foo; elsethrow new illegalargumentexception (float. tostring (f ));}

Do not use floating point values to represent exact values

Some non-integer values (such as a few US dollars and a few US dollars) need to be accurate. Floating point numbers are not precise values, so using them causes rounding errors. Therefore, it is not a good idea to use floating point numbers to represent precise quantities like the amount of currency. Using Floating Point Numbers for dollar and cent calculations can have disastrous consequences. Floating Point Numbers are best used to represent values such as measured values, which are not very accurate from the very beginning.

Bigdecimal

Since JDK 1.3, Java developers have another numerical representation to represent a non-integer: bigdecimal. Bigdecimal is a standard class and does not require special support in the compiler. It can represent decimal places of any precision and calculate them. Internally, bigdecimal can be expressed by a value in any range of precision and a conversion factor. The conversion factor indicates the number of decimal places to move left to obtain the value in the expected range. Therefore, the number expressed in bigdecimal format is unscaledvalue * 10-scale.

The addition, subtraction, multiplication, and division methods provide arithmetic operations for bigdecimal values. Since bigdecimal objects are unchangeable, each of these methods generates a new bigdecimal object. Therefore, because of the overhead of object creation, bigdecimal is not suitable for a large number of mathematical calculations, but it is designed to accurately represent decimals. If you are looking for a value that accurately represents the amount of money, bigdecimal can be competent for this task.

All equals methods cannot be tested to be equal.

Similar to the floating point type, bigdecimal has some strange behaviors. Be careful when using the equals () method to check whether values are equal. In the equals () method, two bigdecimal values that indicate the same number but have different values (for example, 100.00 and 100.000) are not equal. However, the compareto () method considers these two numbers to be equal. Therefore, compareto () instead of equals () should be used to compare two bigdecimal values ().

In addition, in some cases, decimal operations with any precision still cannot represent precise results. For example, dividing 1 by 9 produces an infinite number of decimal places. 111111 .... For this reason, bigdecimal allows you to explicitly control rounding during Division operations. The movepointleft () method supports the exact division of 10 power.

Use bigdecimal as the Interchange Type

The SQL-92 includes the decimal data type, which is the exact numeric type used to represent the decimal point, which can perform basic arithmetic operations on the decimal point. Some SQL languages like to call this type numeric. Other SQL languages introduce the money data type. The money data type is defined as a decimal point with two decimal places.

If you want to store the number to the decimal field in the database or retrieve the value from the decimal field, how can you ensure accurate conversion of the number? You may not want to use the setfloat () and getfloat () methods provided by the JDBC preparedstatement and resultset classes, because the conversion between floating point numbers and decimals may lose accuracy. Instead, use the setbigdecimal () and getbigdecimal () Methods of preparedstatement and resultset.

There are several constructors available for bigdecimal. One constructor uses a double-precision floating point number as the input, the other uses an integer and a conversion factor as the input, and the other uses a decimal string as the input. Be careful to use the bigdecimal (double) constructor, because if you do not know about it, it will generate a rounding error during the calculation process. Use an integer or string-based constructor.

Construct the bigdecimal number

There are several constructors available for bigdecimal. One constructor uses a double-precision floating point number as the input, the other uses an integer and a conversion factor as the input, and the other uses a decimal string as the input. Be careful to use the bigdecimal (double) constructor, because if you do not know about it, it will generate a rounding error during the calculation process. Use an integer or string-based constructor.

If the bigdecimal (double) constructor is not appropriate, an exception in the JDBC driver may appear strange when it is passed to the JDBC setbigdecimal () method. For example, consider the following JDBCCodeIn this Code, you want to store the number 0.01 to the decimal field:

 
Preparedstatement PS = connection. preparestatement ("insert into Foo set name = ?, Value =? "); PS. setstring (1," Penny "); PS. setbigdecimal (2, new bigdecimal(0.01);;ps.exe cuteupdate ();

Some confusing exceptions will be thrown when executing this seemingly harmless code (depending on the specific JDBC driver), because the 0.01 double-precision approximation will lead to a large conversion value, this may confuse the JDBC driver or database. The JDBC driver produces exceptions, but it may not indicate where the code is actually wrong unless you are aware of the limitations of binary floating point numbers. Instead, use bigdecimal ("0.01") or bigdecimal (1, 2) to construct bigdecimal to avoid this problem, because both methods can accurately represent decimal places.

Conclusion

Using Floating Point Numbers and Decimals in Java programs is full of traps. Floating-point numbers and decimals are not exactly the same as integers. It cannot be assumed that floating-point Computation must produce integer or precise results, although they do "should. It is best to keep the floating point operation as a non-precise value for calculation, such as measurement. If you want to specify the number of points (for example, the number of US dollars and the number of US Dollars), use bigdecimal.

The original article is reproduced from: http://justjavac.iteye.com/blog/4103775.

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.