PHP中是否有BigInteger类?

时间:2021-10-03 16:48:47

Is there a BigInteger class in PHP? If so, how do I access it or use it?

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

2 个解决方案

#1


14  

Hopefully helpfull links :

希望有用的链接:

EDIT: Math_BigInteger

Example from http://phpseclib.sourceforge.net/documentation/math.html :

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

Implements an arbitrary precision integer arithmetic library. Uses gmp or bcmath, if available, and an internal implementation, otherwise.

实现任意精度整数算术库。如果可用,使用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
?>

#2


7  

Even though this question is old, it comes up as first result when Googling for BigInteger PHP, so for anyone interested, I open-sourced a library called Brick\Math offering BigInteger, BigDecimal and BigRational classes.

虽然这个问题很老,但是当Googling for BigInteger PHP时它出现了第一个结果,所以对于任何有兴趣的人,我开源了一个名为Brick \ Math的库,提供BigInteger,BigDecimal和BigRational类。


Usage

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

Addition:

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

Subtraction:

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

Multiplication:

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

Division:

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

Exponentiation:

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

You can easily chain method calls:

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

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

Installation

Just install with Composer and you're done:

只需使用Composer安装即可完成:

composer require brick/math

The library automatically uses the GMP and BCMath extensions when available, to speed up calculations, but will also work without them thanks to a pure PHP implementation.

该库在可用时自动使用GMP和BCMath扩展,以加速计算,但由于纯PHP实现,它们也可以在没有它们的情况下工作。

#1


14  

Hopefully helpfull links :

希望有用的链接:

EDIT: Math_BigInteger

Example from http://phpseclib.sourceforge.net/documentation/math.html :

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

Implements an arbitrary precision integer arithmetic library. Uses gmp or bcmath, if available, and an internal implementation, otherwise.

实现任意精度整数算术库。如果可用,使用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
?>

#2


7  

Even though this question is old, it comes up as first result when Googling for BigInteger PHP, so for anyone interested, I open-sourced a library called Brick\Math offering BigInteger, BigDecimal and BigRational classes.

虽然这个问题很老,但是当Googling for BigInteger PHP时它出现了第一个结果,所以对于任何有兴趣的人,我开源了一个名为Brick \ Math的库,提供BigInteger,BigDecimal和BigRational类。


Usage

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

Addition:

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

Subtraction:

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

Multiplication:

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

Division:

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

Exponentiation:

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

You can easily chain method calls:

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

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

Installation

Just install with Composer and you're done:

只需使用Composer安装即可完成:

composer require brick/math

The library automatically uses the GMP and BCMath extensions when available, to speed up calculations, but will also work without them thanks to a pure PHP implementation.

该库在可用时自动使用GMP和BCMath扩展,以加速计算,但由于纯PHP实现,它们也可以在没有它们的情况下工作。