Sorting out floating point computing problems in php-PHP source code

Source: Internet
Author: User
In php, we usually cannot get the desired result for floating-point computing. Why, next, we will analyze the problem and organize some useful examples for you. In php, we usually cannot get the desired result for floating-point computing. Why, next, we will analyze the problem and organize some useful examples for you.

Script ec (2); script

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:

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.

$ 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:

$ A = 0.1;
$ B = 0.7;
Var_dump (bcadd ($ a, $ B, 2) = 0.8 );

Bcadd-add two high-precision numbers
Bccomp-compare two high-precision numbers and return-1, 0, 1
Bcp-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:
/**
* 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 (bcp ($ 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 (bcp ('20140901', '6. 123 '));
// 16.007


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

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.