PHP High-precision computing problem

Source: Internet
Author: User
Tags square root

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.

0.58* +); will print 57, instead of 58, this is actually the bottom binary system of the computer can not accurately represent a floating point number of a bug, is a cross-language, I also encountered 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//  true

The usual high-precision functions are as follows:

Bcadd-adds two high -precision numbers bccomp-compares two high -precision numbers, returns101  bcdiv-divides two high-precision numbers bcmod-to find high-precision number The word remainder bcmul-multiply two high-precision numbers by multiplying bcpow-to find high-precision digital powers bcpowmod-to find high-precision digital exponentiation, the number of numbers is very common bcscale-configuration default decimal point, equivalent to the Linux BC "scale=< c5> "bcsqrt-The high-precision number square root bcsub-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 comparisons * * @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 * @param float $left * @param float $right * @param int $scale The exact number of decimal places * * @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 $scale The exact number of decimal places * * @return String*/Var_dump(bcsub($left=1.0321456,$right=3.0123456, 2));//-1.98   /** * Two high-precision division * * @access Global * @param float $left * @param float $right * @param int $scale The exact number of decimal places * * @return String*/Var_dump(Bcdiv($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_dump(Bcdiv(' 105 ', ' 6.55957 ')); //16.007

This will solve the problem of floating-point arithmetic ....

PHP High-precision computing problem

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.