# Sorting out floating point computing problems in php

Source: Internet
Author: User

If you use php +-*/to calculate the floating point number, you may encounter some calculation result errors, such as echo intval (0.58*100); 57 is printed instead of 58, this is actually a bug that the underlying binary of the computer cannot accurately represent floating-point numbers. It is cross-language. I also encountered this problem using python. Therefore, most languages provide precision computing class libraries or function libraries. For example, php has a high-precision BC function library. The following section describes some frequently-used high-precision BC functions.

Example

 The code is as follows: Copy code

Why is output 57? PHP bug?

I believe many people have such questions, because there are many people who ask similar questions, not to mention bugs.php.net...

To understand this, first we need to know the floating point representation (IEEE 754 ):

Floating point number. Taking the 64-bit length (double precision) as an example, one-bit sign bit (E), 11 index bit (Q), and 52-bit ending number (M) are used) (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.

The key point here is that decimal places are represented in binary. You can refer to Baidu for how decimal places are represented in binary. I will not describe them here. We need to understand the key points, 0.58 for binary representation, it is an infinitely long value (the following number saves the implicit 1 )..

The binary representation of 0.58 is basically (52 bits): the binary representation of 00101000111101011100001010001111010111000010100011110.57 is basically (52 bits): 001000111101011100001010001111010111000010100011110 and the binary of the two, if only these 52 bits are used for calculation, www.111cn.net

0.58-> 0.579999999999999960.57-> 0.5699999999999999 as for the specific Floating-point multiplication of 0.58*100, we do not consider this Details. If you are interested, you can see the Floating point. We will look at it in a fuzzy way... 0.58*100 = 57.999999999

Then you intval It. Naturally, it's 57 ....

It can be seen that the key point of this problem is: "You seem to have a poor decimal number, but it is infinite in the binary representation of the computer"

So, no longer think this is a PHP bug. This is the case .....

The PHP float type is running +-* %/, which is inaccurate.

For example:

1. <? Php

\$ A = 0.1;
\$ B = 0.7;
Var_dump (\$ a + \$ B) = 0.8 );

The printed value is boolean false.
Why? The PHP Manual provides the following warning information for floating point numbers:
Warning
Floating point precision
Obviously, a simple decimal score is like 0.1 or 0.7. It cannot be converted to an internal binary format without losing a little precision. This results in confusion: for example, floor (0.1 + 0.7) * 10) usually returns 7 instead of the expected 8, because the internal representation of this result is similar to 7.9999999999 ....
This is related to the fact that it is impossible to accurately express certain decimal scores with limited digits. For example, decimal 1/3 is changed to 0.3333333 ....
Therefore, never believe that the result of a floating point number is accurate to the last digit, or compare whether the two floating points are equal. If higher precision is required, use any precision mathematical function or gmp function.

 The code is as follows: Copy code

Bccomp-compare two high-precision numbers and return-1, 0, 1
Bcdiv-division of two high-precision numbers
Bcmod-precise digital remainder
Bcmul-multiply two high-precision numbers
Bcpow-calculate a high-precision digital multiplier
Bcpowmod-modulus of high-precision multiplication of numbers, which is very common in number theory.
Bcscale-the default number of decimal places configured, which is equivalent to "scale =" in Linux bc"
Bcsqrt-calculate the square root of a precise number
Bcsub-subtract two high-precision numbers

Sorted out some instances

The php BC high-precision function library includes: addition, comparison, division, subtraction, remainder, multiplication, and npower. The default number of decimal places is configured and the square is obtained. These functions are useful when it comes to money computing, such as e-commerce price calculation.

 The code is as follows: Copy code /*** Comparison of two high-precision numbers  * * @ Access global* @ Param float \$ left* @ Param float \$ right* @ Param int \$ the number of decimal places accurate to scale  * * @ Return int \$ left =\$ right returns 0 | \$ left <\$ right Returns-1 | \$ left> \$ right returns 1*/Var_dump (bccomp (\$ left = 4.45, \$ right = 5.54, 2 ));//-1  /*** Adding two high-precision numbers  * * @ Access global* @ Param float \$ left* @ Param float \$ right* @ Param int \$ the number of decimal places accurate to scale  * * @ Return string*/Var_dump (bcadd (\$ left = 1.0321456, \$ right = 0.0243456, 2 ));// 1.04 /*** Two high-precision subtraction  * * @ Access global* @ Param float \$ left* @ Param float \$ right* @ Param int \$ the number of decimal places accurate to scale  * * @ Return string*/Var_dump (bcsub (\$ left = 1.0321456, \$ right = 3.0123456, 2 ));//-1.98  /*** Division of two high-precision data sets  * * @ Access global* @ Param float \$ left* @ Param float \$ right* @ Param int \$ the number of decimal places accurate to scale  * * @ Return string*/Var_dump (bcdiv (\$ left = 6, \$ right = 5, 2 ));// 1.20 /*** Multiply two high-precision numbers  * * @ Access global* @ Param float \$ left* @ Param float \$ right* @ Param int \$ the number of decimal places accurate to scale  * * @ Return string*/Var_dump (bcmul (\$ left = 3.1415926, \$ right = 2.4569874566, 2 ));// 7.71 /*** Set the decimal point of the bc function.  * * @ Access global* @ Param int \$ the number of decimal places accurate to scale  * * @ Return void*/Bcscale (3 );Var_dump (bcdiv ('123', '6. 123 '));// 16.007

Note: For the set number of digits, the excess part is discarded rather than rounded up.

Related Keywords:
Related Article

## E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

## Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

## Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD