Home  >  Article  >  Backend Development  >  Detailed explanation of PHP BC Math function

Detailed explanation of PHP BC Math function

PHPz
PHPzOriginal
2016-07-29 08:34:273136browse

When calculating with PHP, you will encounter problems caused by high-precision numbers. Fortunately, PHP provides BC system functions. Below is a simple example of encapsulating commonly used BC functions.

【Recommended tutorial: php video tutorial

Detailed explanation of PHP BC Math function

Code example:

<?php
/**
 * BC Math 函数示例
 * Class BCCalculate
 */
class BCCalculate
{
    private $leftNumber;// 左操作数
    private $rightNumber;// 右操作数
    public function __construct($leftNumber, $rightNumber)
    {
        $this->leftNumber = $leftNumber;
        $this->rightNumber = $rightNumber;
        $this->setScale();
    }
    /**
     * 设置数字
     * @param $name
     * @param $value
     * @return null
     */
    public function __set($name, $value)
    {
        if (!isset($this->$name)) {
            return null;
        }
        $this->$name = $value;
    }
    /**
     * 获取数字
     * @param $name
     * @return null
     */
    public function __get($name)
    {
        if (isset($this->$name)) {
            return $this->$name;
        } else {
            return null;
        }
    }
    /**
     * 执行方法
     * @param $functionName
     * @param string $arguments
     * @return null
     */
    public function __call($functionName, $arguments)
    {
        if (!method_exists($this, $functionName)) {
            return null;
        }
        // 设置小数点位数需要参数,其他不需要
        if (isset($arguments[0])) {
            return $this->$functionName($arguments[0]);
        }
        return $this->$functionName();
    }
    /**
     * 设置所有bc数学函数的默认小数点保留位数
     * http://php.net/manual/zh/function.bcscale.php
     * @param int $scale
     */
    private function setScale($scale = 2)
    {
        bcscale($scale);
    }
    /**
     * 2个任意精度数字的加法计算
     * http://php.net/manual/zh/function.bcadd.php
     * @return string
     */
    private function add()
    {
        return bcadd($this->leftNumber, $this->rightNumber);
    }
    /**
     * 2个任意精度数字的减法
     * http://php.net/manual/zh/function.bcsub.php
     * @return string
     */
    private function sub()
    {
        return bcsub($this->leftNumber, $this->rightNumber);
    }
    /**
     * 2个任意精度数字乘法计算
     * http://php.net/manual/zh/function.bcmul.php
     * @return string
     */
    private function mul()
    {
        return bcmul($this->leftNumber, $this->rightNumber);
    }
    /**
     * 2个任意精度的数字除法计算
     * http://php.net/manual/zh/function.bcdiv.php
     * @return string
     */
    private function div()
    {
        return bcdiv($this->leftNumber, $this->rightNumber);
    }
    /**
     * 比较两个任意精度的数字
     * 相等返回 0 ;左大于右返回 1 ;右大于左返回 -1
     * http://php.net/manual/zh/function.bccomp.php
     * @return int
     */
    private function comp()
    {
        return bccomp($this->leftNumber, $this->rightNumber);
    }
    /**
     * 对一个任意精度数字取模
     * http://php.net/manual/zh/function.bcmod.php
     * @return string
     */
    private function mod()
    {
        return bcmod($this->leftNumber, $this->rightNumber);
    }
    /**
     * 任意精度数字的乘方
     * http://php.net/manual/zh/function.bcpow.php
     * @return string
     */
    private function pow()
    {
        return bcpow($this->leftNumber, $this->rightNumber);
    }
    /**
     * 任意精度数字的二次方根
     * http://php.net/manual/zh/function.bcsqrt.php
     * @return string
     */
    private function sqrt()
    {
        return bcsqrt($this->leftNumber);
    }
}
$bc = new BCCalculate(3.45, 5.61);
var_dump($bc->leftNumber);// 获取数字 float(3.45)
echo &#39;<br />&#39;;
$bc->leftNumber = 24.08;
var_dump($bc->leftNumber);// 修改数字 float(24.08)
echo &#39;<br />&#39;;
var_dump($bc->add());// 注意返回值是字符串 string(5) "29.69"
echo &#39;<br />&#39;;
$bc->setScale(3);// 修改小数点后位数
var_dump($bc->sub());// string(6) "18.470"
echo &#39;<br />&#39;;
var_dump($bc->mul());// string(7) "135.088"
echo &#39;<br />&#39;;
var_dump($bc->div());// string(5) "4.292"
echo &#39;<br />&#39;;
var_dump($bc->comp());// int(1)
echo &#39;<br />&#39;;
$bc->leftNumber = 10;
$bc->rightNumber = 4;
var_dump($bc->mod());// string(1) "2"
echo &#39;<br />&#39;;
var_dump($bc->pow());// string(5) "10000"
echo &#39;<br />&#39;;
$bc->leftNumber = 16;
var_dump($bc->sqrt());// string(5) "4.000" 
echo &#39;<br />&#39;;


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn