maba/math

PHP任意精度数学库

v1.0.1 2014-09-20 19:37 UTC

This package is auto-updated.

Last update: 2024-09-10 20:15:15 UTC


README

PHP任意精度数学库。

安装

composer require maba/math

为什么

var_dump(intval(9223372036854775807 + 1));
// int(-9223372036854775808)

var_dump(5.2 * 3 === 15.6);
// bool(false)

var_dump((8 + 92233720368547750) * 1500 / 1500 - 92233720368547750);
// float(16)

用法

use Maba\Component\Math\BcMath;
use Maba\Component\Math\Math;
use Maba\Component\Math\NumberFormatter;

$basicMathImplementation = new BcMath();
$math = new Math($basicMathImplementation);

var_dump($math->add('9223372036854775807', '1'));
// string(40) "9223372036854775808.00000000000000000000"
var_dump($math->isEqual($math->mul('5.2', '3'), '15.6'));
// bool(true)
var_dump($math->sub($math->div($math->mul($math->add('8', '92233720368547750'), '1500'), '1500'), '92233720368547750'));
// string(22) "8.00000000000000000000"

$result = $math->pow($math->mul('3.141592653589793', '2.71828182845904523536'), 13);

$formatter = new NumberFormatter($math);
echo $formatter->formatNumber($result, 4, '.', ' '); // prints 1 284 625 710 591.2256

BcMath 类使用 bcmath 函数。如果您需要使用其他任意精度数学实现,请实现 BasicMathInterface

运行测试

Travis status Coverage Status

composer install
vendor/bin/phpunit