Precision floating-point type loss problem

 

Would like to write this question because there is a demand of days ago, you need to set up a database of a field to type double, and the value of this field will participate comparison, I think this down, in fact, the database or to try to avoid floating point type of data involved in the computation or comparison.

First we have to understand why floating point precision type of data will be lost, we must first understand why precision integer data is not lost. In the computer world, all numbers are converted into machine code 0 and 1 are combined in the bottom layer. The first thing is to learn how the university between decimal and binary conversion, such as 5 decimal to binary, follow these steps:

step1:5/2=2.....1

step2:2/2=1.....0

step3:1/2=0.....1

Bottom-binary 101 is 5, which is an integer, the remainder modulo 2 0 and 1 of only two cases, which means that any integer, we continue to die 2 will certainly get the last 1/2 = 0. .... 1, as long as an integer, it would surely like this. Therefore, the decimal integer into binary is unique.

Then decimal to binary decimal is how to convert it? Decimal decimal to binary conversion, mainly multiplied fractional part 2, after taking the integer part sequentially from left to right on the decimal point, until after the decimal point is 0. Then we consider the following cases to 0.1, for example:

step1: 0.1 * 2 = 0.2 ----- take 0

step2: 0.2 * 2 = 0.4 ----- take 0

step3: 0.4 * 2 = 0.8 ----- take 0

step4: 0.8 * 2 = 1.6 ----- take 1

step5: 0.6 * 2 = 1.2 ----- take 1

step6: 0.2 * 2 = 0.4 ----- 0 then we take the findings have returned to step2, and therefore would have been the cycle continues, the corresponding binary 0.1 also get 0.0 00,110,011 ......

We often say from two floating point types: single-precision and double-precision, single precision, i.e. float, on a 32-bit machine with four bytes of storage; and double double precision is 8 bytes of storage the (double from here can be found not only represent a wider range and more accurate). When faced with such an endless loop of decimal digits beyond the maximum number of bits float or double, then only the interception, it is precisely because of this, resulting in a loss of accuracy.

 

Finally, send us a tip (from Alibaba Java Development Manual): In the business scenario requires absolute precision under represented, such as the financial sector currency, whose value is recommended to use the smallest integer storage unit can be converted into the currency of the time display common units, such as the use of RMB sub storage, use dollar cents store. After the required accuracy of the decimal point represent the N-bit business scenarios, such as the storage requirements pi decimal numbers 1000, the same single-precision and double-precision floating-point save type is difficult to do, then the data array holding the recommended fractional part. When comparing floating, due to errors tend Unexpected results, it is prohibited to control some business processes two floating-point numbers are equal is determined. When you save a decimal in the database, it is recommended to use the decimal type, prohibit the use of float or double. Because of the loss of accuracy problems exist in both types of storage time.

 

Guess you like

Origin www.cnblogs.com/Edwardz/p/11286314.html