PHPackages                             ez-php/bignum - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ez-php/bignum

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ez-php/bignum
=============

Arbitrary-precision integer and decimal arithmetic for the ez-php ecosystem — immutable BigInteger and BigDecimal value objects backed by bcmath (gmp optional)

1.11.1(4w ago)0781MITPHPPHP ^8.5CI passing

Since Apr 14Pushed 4w agoCompare

[ Source](https://github.com/ez-php/bignum)[ Packagist](https://packagist.org/packages/ez-php/bignum)[ Docs](https://github.com/ez-php/bignum)[ RSS](/packages/ez-php-bignum/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (6)Versions (15)Used By (1)

ez-php/bignum
=============

[](#ez-phpbignum)

Arbitrary-precision integer and decimal arithmetic for PHP 8.5+.

Provides immutable `BigInteger` and `BigDecimal` value objects backed by PHP's built-in `bcmath` extension — no external Composer dependencies, no PECL extensions required. Optionally uses `gmp` as a faster backend for integer operations.

Installation
------------

[](#installation)

```
composer require ez-php/bignum
```

Requires `ext-bcmath` (ships with PHP). Optionally install `ext-gmp` for faster `BigInteger` arithmetic.

Quick Start
-----------

[](#quick-start)

```
use EzPhp\BigNum\BigDecimal;
use EzPhp\BigNum\BigInteger;
use EzPhp\BigNum\RoundingMode;

// BigInteger — arbitrary-precision integers
$a = BigInteger::of('99999999999999999999999999999999');
$b = BigInteger::of('1');
echo $a->add($b); // 100000000000000000000000000000000

// BigDecimal — exact decimal arithmetic (no floating-point errors)
$price  = BigDecimal::of('0.1');
$tax    = BigDecimal::of('0.2');
echo $price->add($tax); // 0.3  (not 0.30000000000000004)

// Division with explicit scale and rounding
$result = BigDecimal::of('10')->dividedBy('3', 4, RoundingMode::HALF_UP);
echo $result; // 3.3333

// Banker's rounding (HALF_EVEN)
echo BigDecimal::of('2.45')->round(1, RoundingMode::HALF_EVEN); // 2.4
echo BigDecimal::of('2.55')->round(1, RoundingMode::HALF_EVEN); // 2.6
```

API
---

[](#api)

### BigInteger

[](#biginteger)

```
BigInteger::of(int|string $value): self
BigInteger::zero(): self
BigInteger::one(): self

// Arithmetic (all return new BigInteger)
->add(BigInteger|int|string $other): self
->subtract(BigInteger|int|string $other): self
->multiply(BigInteger|int|string $other): self
->divide(BigInteger|int|string $divisor): self      // integer division, truncates towards zero
->mod(BigInteger|int|string $divisor): self
->pow(int $exponent): self                           // exponent >= 0
->abs(): self
->negate(): self
->gcd(BigInteger|int|string $other): self
->sqrt(): self                                       // floor(sqrt(n))

// Comparison
->compareTo(BigInteger|int|string $other): int       // -1, 0, 1
->isEqualTo(...)  ->isLessThan(...)  ->isGreaterThan(...)
->isLessThanOrEqualTo(...)  ->isGreaterThanOrEqualTo(...)
->isZero(): bool  ->isPositive(): bool  ->isNegative(): bool

// Conversion
->toInt(): int           // throws OverflowException if too large
->toFloat(): float
->toString(): string
->toBigDecimal(): BigDecimal
```

### BigDecimal

[](#bigdecimal)

```
BigDecimal::of(int|string|float $value): self
BigDecimal::ofUnscaledValue(string $unscaledValue, int $scale): self
BigDecimal::zero(): self
BigDecimal::one(): self

// Accessors
->getScale(): int
->getUnscaledValue(): string

// Arithmetic (all return new BigDecimal)
->add(BigDecimal|BigInteger|int|string $other): self
->subtract(BigDecimal|BigInteger|int|string $other): self
->multiply(BigDecimal|BigInteger|int|string $other): self
->divide(BigDecimal|BigInteger|int|string $divisor): self       // integer division (scale 0)
->dividedBy($divisor, int $scale, RoundingMode $mode): self     // explicit scale + rounding
->mod(BigDecimal|BigInteger|int|string $divisor): self
->pow(int $exponent): self
->abs(): self
->negate(): self

// Scale and rounding
->round(int $scale, RoundingMode $mode = HALF_UP): self
->toScale(int $scale, RoundingMode $mode = HALF_UP): self

// Comparison (scale-independent)
->compareTo(...)  ->isEqualTo(...)  ->isLessThan(...)  ->isGreaterThan(...)
->isLessThanOrEqualTo(...)  ->isGreaterThanOrEqualTo(...)
->isZero(): bool  ->isPositive(): bool  ->isNegative(): bool

// Conversion
->toInt(): int           // truncates fractional part; throws OverflowException if too large
->toFloat(): float
->toString(): string
->toBigInteger(): BigInteger
->toScientific(): string // e.g. "1.23456E+2"
```

### RoundingMode

[](#roundingmode)

ModeBehaviour`UP`Round away from zero`DOWN`Round towards zero (truncate)`CEILING`Round towards positive infinity`FLOOR`Round towards negative infinity`HALF_UP`Round half away from zero (standard)`HALF_DOWN`Round half towards zero`HALF_EVEN`Round half to nearest even digit (banker's rounding)### DivisionByZeroException

[](#divisionbyzeroexception)

Thrown by `divide`, `dividedBy`, and `mod` when the divisor is zero. Extends `\ArithmeticError`.

Backend Selection
-----------------

[](#backend-selection)

`BigInteger` auto-selects the best available backend:

1. **GmpBackend** — used when `ext-gmp` is loaded (faster for large integers)
2. **BcMathBackend** — always available fallback

`BigDecimal` always uses bcmath directly (GMP does not support decimal arithmetic).

To force a specific backend:

```
use EzPhp\BigNum\Backend\BcMathBackend;

BigInteger::setDefaultBackend(new BcMathBackend());
```

Design
------

[](#design)

- **Zero framework coupling** — usable standalone without bootstrapping the application
- **Immutable** — all operations return new instances; the original is never modified
- **No `mixed`** — all parameters and return values are strictly typed
- **No external dependencies** — only `ext-bcmath` (required) and `ext-gmp` (optional)

License
-------

[](#license)

MIT

###  Health Score

46

—

FairBetter than 92% of packages

Maintenance94

Actively maintained with recent releases

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

14

Last Release

29d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/122030400?v=4)[AU9500](/maintainers/AU9500)[@AU9500](https://github.com/AU9500)

---

Top Contributors

[![AU9500](https://avatars.githubusercontent.com/u/122030400?v=4)](https://github.com/AU9500 "AU9500 (26 commits)")

---

Tags

phpBigIntegermathbignumbigdecimalArbitrary-precisionbcmathgmpez-php

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/ez-php-bignum/health.svg)

```
[![Health](https://phpackages.com/badges/ez-php-bignum/health.svg)](https://phpackages.com/packages/ez-php-bignum)
```

###  Alternatives

[brick/math

Arbitrary-precision arithmetic library

2.1k532.9M373](/packages/brick-math)[phpseclib/bcmath_compat

PHP 5.x-8.x polyfill for bcmath extension

16721.2M25](/packages/phpseclib-bcmath-compat)[krowinski/bcmath-extended

Extends php BCMath lib for missing functions like floor, ceil, round, abs, min, max, rand for big numbers. Also wraps existing BCMath functions. (more http://php.net/manual/en/book.bc.php) Supports scientific notations

801.1M22](/packages/krowinski-bcmath-extended)[rtlopez/decimal

An object oriented immutable arbitrary-precision arithmetic library for PHP

27270.4k2](/packages/rtlopez-decimal)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
