PHP Accurate calculation function

Source: Internet
Author: User
This article mainly introduced the PHP accurate computation function, combined with the example form analysis PHP carries on the accurate method for subtraction and so on mathematics operation, involves Bcadd, bcsub, Bcmul, bcp and so on function's use skill, needs friend can refer to the next

Introduction: Be sure to ensure the accuracy of your data. This is the basic accomplishment of a good programmer.

<?php/** * Exact addition * @param [type] $a [description] * @param [Type] $b [description] */function 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}echo math_add (' 3.445 ', ' 3.444 '). " \ n "; Add 6.88echo math_sub (' 3.445 ', ' 3.444 '). " \ n "; //Minus 0.00echo Math_mul (' 3.445 ', ' 3.444 '). " \ n "; Multiply 11.86echo math_p (' 3.445 ', ' 3.444 '). " \ n "; Except 1.00echo math_mod (' 3.445 ', ' 3.444 '). " \ n "; Modulo 0echo math_comp (' 3.445 ', ' 3.444 '). " \ n ";//Compare 1echo Math_add (' 3.445 ', ' 3.444 ', ' 3 ')." \ n "; Add 6.889echo math_sub (' 3.445 ', ' 3.444 ', ' 3 '). " \ n "; Minus 0.001echo Math_mul (' 3.445 ', ' 3.444 ', ' 3 '). " \ n "; Multiply 11.864echo math_p (' 3.445 ', ' 3.444 ', ' 3 '). " \ n "; Except 1.000echo math_mod (' 3.445 ', ' 3.444 '). " \ n "; Modulo 0echo math_comp (' 3.445 ', ' 3.444 '). " \ n ";//Compare 1?>

The above is the whole content of this article, I hope that everyone's study has helped.


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.