PHP中是否有BigInteger类?

时间:2010-12-13 08:34:31

标签: php biginteger

PHP中是否有BigInteger类?如果是这样,我该如何访问或使用它?

2 个答案:

答案 0 :(得分:16)

希望有用的链接:

编辑:Math_BigInteger

来自http://phpseclib.sourceforge.net/documentation/math.html的示例:

实现任意精度整数算术库。如果可用,使用gmp或bcmath,否则使用内部实现。

<?php
    include('Math/BigInteger.php');

    $a = new Math_BigInteger(2);
    $b = new Math_BigInteger(3);

    $c = $a->add($b);

    echo $c->toString(); // outputs 5
?>

答案 1 :(得分:7)

尽管这个问题已经过时了,但在Google搜索BigInteger PHP时会出现第一个结果,所以对于任何有兴趣的人,我都开源了一个名为Brick\Math的库,提供BigInteger,{ {1}}和BigDecimal类。


用法

BigRational

增加:

use Brick\Math\BigInteger;
use Brick\Math\RoundingMode;

减法:

echo BigInteger::of('9999999999999999999999999')->plus(1);
// 10000000000000000000000000

乘:

echo BigInteger::of('10000000000000000000000000')->minus(1);
// 9999999999999999999999999

司:

echo BigInteger::of('3333333333333333333333333')->multipliedBy(11);
// 36666666666666666666666663

幂:

echo BigInteger::of('1000000000000000000000')->dividedBy(3, RoundingMode::UP);
// 333333333333333333334

您可以轻松地链接方法调用:

echo BigInteger::of(11)->power(50);
// 11739085287969531650666649599035831993898213898723001

安装

只需安装Composer即可完成:

echo BigInteger::of(3)->multipliedBy(7)->minus(1)->dividedBy(10);

库在可用时自动使用GMPBCMath扩展名来加速计算,但由于纯PHP实现,它们也可以在没有它们的情况下使用。

相关问题