PHP Floating-point precision operation

Source: Internet
Author: User
Tags functions mathematical functions string square root

BC is the abbreviation of binary calculator. The arguments for the bc* function are the operands plus an optional [int scale], such as String Bcadd (String $left _operand, String $right _operand[, int $scale]), if scale does not provide , the default value of Bcscale is used. The large number is directly represented by a string of 0-9, and the result returns a string.


Bcadd-adds two high precision numbers
bccomp-compares two high-precision digits, returns-1, 0, 1
bcdiv-divides two high-precision digits
bcmod-to find high precision digit remainder
Bcmul-multiplies two high precision numbers
bcpow-to find high precision digital exponentiation
Bcpowmod-to seek high-precision digital exponentiation, the number theory is very common
bcscale-Configure the default decimal point number, which is equivalent to the "scale=" in Linux BC
bcsqrt-High precision Digital square root
bcsub-subtracts two high-precision digits

First look at a piece of code:

$a = 0.1;
$b = 0.7;
Var_dump (($a + $b) = = 0.8);

The printed value is Boolean false

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

Warning
Floating-point precision
Obviously 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 confusing results: for example, Floor (0.1+0.7) *10 usually returns 7 rather than the expected 8 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 points with a finite number of digits. For example, the decimal 1/3 becomes 0.3333333. . .。
So never believe that floating-point numbers are accurate to the last one, and never compare two floating-point numbers for equality. If you do need higher precision, you should use arbitrary precision mathematical functions or GMP functions

So the formula above should be rewritten as

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

So we can solve the problem of floating point counting.



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.