PHP floating point precision loss solution

Source: Internet
Author: User
Floating point numbers are the ones with decimals, and they are not just numbers. when I use the payment interface, I have encountered the problem of floating point loss. let's take a look at the solution. let's take a look at the following code: $ f0.57; e... floating point numbers are the ones with decimals, and they are not just numbers. when I use the payment interface, I have encountered the problem of floating point loss. let's take a look at the solution.

Let's take a look at the following code:

$f = 0.57; echo intval($f * 100);  //56

The result may be a little unexpected. PHP follows IEEE 754 dual precision.

Floating point number, expressed in 64-bit dual precision, with one-bit sign bit (E), 11 exponent bit (Q), and 52-bit ending number (M) (64-bit in total ).

Symbol bit: the highest bit indicates positive and negative data, 0 indicates positive data, and 1 indicates negative data.

Exponent bit: the power of data at the bottom of 2, and the exponent is represented by an offset code.

Ending number: a valid number after the decimal point of the data.

Let's take a look at how decimal places are represented by binary: Take 2 as an integer and arrange them in order, multiply the decimal part by 2, then take the integer part, multiply the remaining decimal part by 2, and then take the integer part, the remaining decimal part is multiplied by 2, and the fractional part is obtained all the time. However, decimal places like 0.57 continue to be multiplied, and the decimal part cannot be zero. the decimal number of the valid bits is expressed in binary instead of infinite.

The binary representation of 0.57 is basically (52 bits): 0010001111010111000010100011110101110000101000111101

If there are only 52 digits, 0.57 = "0.56999999999999995

It is not difficult to see the unexpected results above. let's add some examples. There are many methods. here are two examples:

1. sprintf

Substr (sprintf ("%. 10f", ($ a/$ B), 0,-7 );

2. round (note that it will be rounded off)

Round ($ a/$ B, 3 );

I have always understood the precision as the number of decimal points reserved. is this true for floating point numbers in php? The answer is No.

A floating point is actually composed of an integer and a decimal part. the precision here is that the number of digits in the integer part plus the decimal part cannot exceed the maximum precision, the maximum precision value is truncated according to the rounding method. if the integer is 0, the number of digits is not counted. The value 0 at the end of the decimal part is not counted into the number of digits. In addition, for the same number, the precision is different, the display format may be different. the following uses examples to describe.

If the integer is 0:

$ Num = 0.12345600000000000; // The integer part is 0, the digits are 0, and the digits 0 at the end of the decimal part are not counted in the digits. Therefore, the total digits are 6 ini_set ("precision ", "12"); echo $ num; // 0.123456 // if the precision value is not exceeded, the displayed result is 0.123456 ini_set ("precision", "3"); echo $ num; // 0.123 // exceeds the precision value, retain 3 ini_set ("precision", "5"); echo $ num; // 0.12346 // exceeds the precision value, retain 5 bits

In this case, the precision value is equivalent to the number of digits after the decimal point.

If the integer part is greater than 0

$ Num = 12.12345600000000000; // The integer is 12 and the number of digits is 2. 0 at the end of the decimal part is not counted as the number of digits. the number of digits is 6, therefore, the total number of digits is 2 + 6 ini_set ("precision", "12"); echo $ num; // 12.123456 // not exceeding the precision value, the displayed result is 12.123456 ini_set ("precision", "3"); echo $ num; // 12.1 // beyond the precision value, the integer part of the number of digits is 2, therefore, only one decimal place ini_set ("precision", "5"); echo $ num; // 12.123 // exceeds the precision value. The number of digits in the integer part is 2, therefore, only three decimal places are reserved.

We can see that the number of digits retained after the decimal point is related to the number of digits whose precision is already an integer.

Case 2 where the integer part is greater than 0

$ Num = 12345678.12345600000000000; // The integer part is 12345678 and the number of digits is 8. the value 0 at the end of the decimal part is not counted as the number of digits. the number of digits is 6, therefore, the total number of digits is 8 + 6 ini_set ("precision", "12"); echo $ num; // 12345678.1235 // exceeds the precision value, the result is 12345678.1235 ini_set ("precision", "3"); echo $ num; // 1.23E + 7 // exceeds the precision value, and the number of digits in the integer part exceeds the precision, the fractional part is discarded, and only three ini_set ("precision", "5"); echo $ num; // 12346000 // exceeds the precision value, the number of digits in the integer part exceeds the precision. the fractional part is discarded, and the integer part is only five digits.

In the above example, we can see that the precision value is also related to the intercept of the integer part. Note that the methods shown in the last two examples are different. one is to use scientific notation, and the other is to use 0 complement, the experiment shows that when the number of digits in the integer part minus the precision value is greater than 4, scientific notation is used. otherwise, 0 is used to supplement the number. In other words, that is, after the number of digits in the integer part exceeds the precision value, after truncation, the number of digits supplemented by 0 will not exceed 4.

Floating point calculation

$ Num1 = 1331625729.687; $ num2 = 1331625730.934; ini_set ("precision", "8"); echo $ num1. ''; echo $ num2. ''; $ sub = $ num1-$ num2; echo $ sub. ''; // The output result is/* 1331625700 1331625700-1.247 */

The preceding example shows that the precision value only controls the display result, and the internal storage is still the original value. Therefore, the value of $ sub is 1331625729.687 minus 1331625730.934.

Related Article

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.