Home  >  Article  >  Backend Development  >  Usage example of Math function in PHP's BC (code)

Usage example of Math function in PHP's BC (code)

不言
不言forward
2019-01-31 09:56:141996browse

The content of this article is about the usage examples (code) of the Math function in PHP's BC. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

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.

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.bcp.php
     * @return string
     */
    private function p()
    {
        return bcp($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 '<br />';
$bc->leftNumber = 24.08;
var_dump($bc->leftNumber);// 修改数字 float(24.08)
echo '<br />';
var_dump($bc->add());// 注意返回值是字符串 string(5) "29.69"
echo '<br />';
$bc->setScale(3);// 修改小数点后位数
var_dump($bc->sub());// string(6) "18.470"
echo '<br />';
var_dump($bc->mul());// string(7) "135.088"
echo '<br />';
var_dump($bc->p());// string(5) "4.292"
echo '<br />';
var_dump($bc->comp());// int(1)
echo '<br />';
$bc->leftNumber = 10;
$bc->rightNumber = 4;
var_dump($bc->mod());// string(1) "2"
echo '<br />';
var_dump($bc->pow());// string(5) "10000"
echo '<br />';
$bc->leftNumber = 16;
var_dump($bc->sqrt());// string(5) "4.000" 
echo '<br />';

The above is the detailed content of Usage example of Math function in PHP's BC (code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete