PHP for high-precision computing

Source: Internet
Author: User
Tags square root
This article is mainly to share with you PHP to achieve high-precision computing, engaged in the financial industry, capital operations frequently, here I met the pit .... Slightly inattentive, the user funds may lose hundreds of thousands of, even more terrible ... Go directly to the example:

Javascript

0.1 + 0.2 Why isn't it equal to 0.3? (correct result: 0.30000000000000004)

0.8 * 7 Why not equal to 5.6? (correct result: 5.6000000000000005)

Php

Var_dump (intval (0.58 * 100));

The correct result is 57, not 58

The curse of floating-point arithmetic

In fact, these results are not the language of the bug, but with the implementation of the principle of language, JS all the numbers are unified number, including shaping is actually all double-precision type.

and PHP will distinguish between int or float. No matter what language, as long as there are floating point operations, there are similar problems, use must pay attention to.

Note: If you use PHP +-*/to calculate floating-point numbers, you may encounter some errors in the calculation results, such as the above Echo intval (0.58*100), will print 57, not 58, this is actually the bottom binary system of the computer can not accurately represent a floating point number of a bug, is cross-lingual, I also encounter this problem with Python. So basically most of the language provides a precise calculation of the class library or function library, such as PHP has a BC high-precision function library, later I shaoxing some common BC high-precision function use.

Or back to the top 57, 58 questions.

Why is the output 57? PHP bug?

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

Floating-point numbers, in the case of a 64-bit length (double), take the 1-bit sign bit (E), 11 exponent (Q), and 52-bit mantissa (M) (altogether 64 bits).

Sign bit: The highest bit represents the positive or negative of the data, 0 is a positive number, and 1 indicates a negative number.

Digits: Indicates that the data is a power of 2, and the exponent is represented by an offset code

Mantissa: A valid number that represents the decimal point of the data.

Here is the key point is that the decimal in the binary representation, about how to use binary decimal notation, you can Baidu, I do not repeat here, we key to understand, 0.58 for the binary representation, is an infinitely long value (the following number omitted the implied 1):

The binary representation of 0.58 is basically (52 bits) is: The binary representation of 00101000111101011100001010001111010111000010100011110.57 is basically (52 bits) is: 001000111101011100001010001111010111000010100011110 and the two binary, if only through the 52-bit calculation, respectively: www.111cn.net

0.58, 0.579999999999999960.57, 0.5699999999999999 as for the 0.58 * 100 specific floating-point multiplication, we do not consider so fine, interested can see (floating points), We are vague in mental arithmetic to see ... 0.58 * 100 = 57.999999999

Then you intval, Nature is 57 ....

Visible, the key point of this problem is: "You seem to have a poor decimal, in the computer binary representation is infinite"

Therefore, do not think that this is a PHP bug, this is the case .....

PHP floating point type in the +-*%/there is an inaccurate problem

Keep looking at the code:

$a = 0.1; $b = 0.7;var_dump (($a + $b) = = 0.8); False

The printed value is Boolean false

Why is this? The PHP manual has the following warning message for floating-point numbers:

Warning

Floating point Accuracy

It is obvious that a simple decimal score like 0.1 or 0.7 cannot be converted to an internal binary format without losing a little bit of precision. This can result in confusion: for example, floor ((0.1+0.7) *10 typically returns 7 instead of 8 as expected because the internal representation of the result is actually similar to 7.9999999999 ....

This is related to the fact that it is impossible to accurately express certain decimal fractions with a finite number of digits. For example, the decimal 1/3 becomes 0.3333333. . .。

So never believe that the floating-point number is accurate to the last one, and never compare two floating-point numbers for equality. If you do need a higher precision, you should use any mathematical or GMP function with any precision.

So the above formula should be rewritten as

$a = 0.1; $b = 0.7;var_dump (Bcadd ($a, $b, 2) = = 0.8); True

The usual high-precision functions are as follows:

BC is the abbreviation for binary calculator

bcadd-two high-precision numbers together bccomp-compare two high-precision numbers, return 1, 0, 1 bcp-divide two high-precision numbers bcmod-to find high-precision digital remainder bcmul-multiply two high-precision numbers by bcpow-to find high-precision digital multiplication Square bcpowmod-to find high-precision digital exponentiation, number theory is very common bcscale-configuration default decimal point, equivalent to the "scale=" in the Linux BC bcsqrt-the high-precision square root bcsub-to subtract two high-precision numbers
BC High-Accuracy library contains: Add, compare, divide, subtract, balance, multiply, n-squared, configure the default number of decimal points, squared. These functions are useful when it comes to money calculations, such as the price calculation of e-commerce.
/** * Two high-precision number comparison * * @access global * @param float $left * @param float $right * @param int $scale The exact number of decimal places * * @ return int $left = = $right return 0 | $left < $right back-1 | $left > $right return 1 */var_dump (Bccomp ($left =4.45, $right = 5.54, 2))//-1/** * Two high-precision numbers added * * @access global * @par AM Float $left * @param float $right * @param int $scale The exact number of decimal places * * @return string */var_dump (Bcadd ($left =1.0321 456, $right = 0.0243456, 2));//1.05/** * Two high-precision number subtraction * * @access global * @param float $left * @param float $right *  @param int $scale The exact number of decimal places * * @return string */var_dump (Bcsub ($left =1.0321456, $right = 3.0123456, 2));//-1.98/** * Divide between two high-precision numbers * * @access global * @param float $left * @param float $right * @param int $scale The exact number of decimal places * * @retu RN string */var_dump (bcp ($left =6, $right = 5, 2))//1.20/** * Two high-precision numbers multiplied * * @access global * @param float $left * @ param float $right * @param int $scale The exact number of decimal places * * @return string */var_dump (Bcmul($left =3.1415926, $right = 2.4569874566, 2)); /7.71/** * Set the number of decimal places for the BC function * * @access global * @param int $scale The exact number of decimal places * * @return void */Bcscale (3); Var_du MP (BCP (' 105 ', ' 6.55957 ')); php7.1 16

So the usual procedure to encapsulate the method:

/** * Exact addition * @param [type] $a [description] * @param [Type] $b [description] */functi On Math_add ($a, $b, $scale = ' 2 ') {return bcadd ($a, $b, $scale);} /** * Exact subtraction * @param [type] $a [description] * @param [Type] $b [description] */function math_sub ($a, $b, $scale = ' 2 ') {RE Turn bcsub ($a, $b, $scale);} /** * Exact multiplication * @param [type] $a [description] * @param [Type] $b [description] */function Math_mul ($a, $b, $scale = ' 2 ') {RE Turn Bcmul ($a, $b, $scale);} /** * Exact Division * @param [type] $a [description] * @param [Type] $b [description] */function math_p ($a, $b, $scale = ' 2 ') {Retu RN bcp ($a, $b, $scale);} /** * Accurate redundancy/modulus * @param [type] $a [description] * @param [Type] $b [description] */function math_mod ($a, $b) {return bcmod ($a, $b);} /** * Compare size * @param [type] $a [description] * @param [Type] $b [description] * greater than return 1 equals return 0 less than return-1 */function math_comp ($a, $b, $scale = ' 5 ') {return Bccomp ($a, $b, $scale);//Compare to the number of decimal places} 

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.