How PHP handles high-precision computational functions

Source: Internet
Author: User
This article is mainly to share with you how PHP handles high-precision computing functions, the main and we share the two methods, hope to help everyone.

Method 1:

PHP provides a binary calculator (binary Calculator) for any mathematical calculation of arbitrary precision, and it supports numbers of any size and precision, described in string form

bcadd-addition
bccomp-comparison
Divide bcp-
bcmod-seeking remainder
bcmul-multiplication
bcpow-Second Party
bcpowmod-first, then the remainder.
bcscale-Setting the decimal precision for all functions
bcsqrt-the square root
bcsub-Subtraction

Method Two:

Recently encountered a strange problem, the mall through the payment of orders often less than a penny, after the troubleshooting is a problem caused by the accuracy of PHP floating point arithmetic

By the PHP floating-point arithmetic precision caused by, bird Brother's Bolg have detailed description. Http://www.laruence.com/2013/03/26/2884.html,
Decimal in binary notation, 0.58 for binary, is an infinitely long value

The binary representation of 0.58 is basically (52 bits) is: 0010100011110101110000101000111101011100001010001111

The binary representation of 0.57 is basically (52 bits) is: 0010001111010111000010100011110101110000101000111101

Convert to floating point number (64-bit double precision)

0.58-0.57999999999999996

0.57-0.56999999999999995

0.58*100 = 57.999999999 (int) (0.58*100) = 57

Workaround:

(int) ((0.58*1000)/10) = 58

Related recommendations:

MySQL common date and calculation function example explanation

PHP Probability calculation function

PHP how to calculate function execution Time _php tutorial

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.