PHPackages                             brick/math - 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. brick/math

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

brick/math
==========

Arbitrary-precision arithmetic library

0.17.2(2w ago)2.1k532.9M↓34.9%9420MITPHPPHP ^8.2CI passing

Since Aug 31Pushed 2w ago23 watchersCompare

[ Source](https://github.com/brick/math)[ Packagist](https://packagist.org/packages/brick/math)[ GitHub Sponsors](https://github.com/BenMorel)[ RSS](/packages/brick-math/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (10)Dependencies (9)Versions (85)Used By (20)

Brick\\Math
-----------

[](#brickmath)

[![](https://raw.githubusercontent.com/brick/brick/master/logo.png)](https://raw.githubusercontent.com/brick/brick/master/logo.png)

A PHP library to work with arbitrary precision numbers.

[![Build Status](https://github.com/brick/math/workflows/CI/badge.svg)](https://github.com/brick/math/actions)[![Coverage Status](https://camo.githubusercontent.com/21050e994b3fb820f9b65ba0fe31df7d6f4e6230caac509218a688aa35e86b46/68747470733a2f2f636f6465636f762e696f2f6769746875622f627269636b2f6d6174682f67726170682f62616467652e737667)](https://codecov.io/github/brick/math)[![Latest Stable Version](https://camo.githubusercontent.com/28386a5fb27903fe7f933c260b86687702e6ab17486f94f5a8d0893e2156271c/68747470733a2f2f706f7365722e707567782e6f72672f627269636b2f6d6174682f762f737461626c65)](https://packagist.org/packages/brick/math)[![Total Downloads](https://camo.githubusercontent.com/e68f42db240bf16d78799e830dda3fa1785afc8918639a720b62bc2f1cebd61f/68747470733a2f2f706f7365722e707567782e6f72672f627269636b2f6d6174682f646f776e6c6f616473)](https://packagist.org/packages/brick/math)[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](http://opensource.org/licenses/MIT)

### Installation

[](#installation)

This library is installable via [Composer](https://getcomposer.org/):

```
composer require brick/math
```

### Requirements

[](#requirements)

This library requires PHP 8.2 or later.

For PHP 8.1 compatibility, you can use version `0.13`. For PHP 8.0, you can use version `0.11`. For PHP 7.4, you can use version `0.10`. For PHP 7.1, 7.2 &amp; 7.3, you can use version `0.9`. Note that [PHP versions &lt; 8.1 are EOL](http://php.net/supported-versions.php) and not supported anymore. If you're still using one of these PHP versions, you should consider upgrading as soon as possible.

Although the library can work seamlessly on any PHP installation, it is highly recommended that you install the [GMP](http://php.net/manual/en/book.gmp.php) or [BCMath](http://php.net/manual/en/book.bc.php) extension to speed up calculations. The fastest available calculator implementation will be automatically selected at runtime.

### Project status &amp; release process

[](#project-status--release-process)

While this library is still under development, it is well tested and considered stable enough to use in production environments.

The current releases are numbered `0.x.y`. When a non-breaking change is introduced (adding new methods, optimizing existing code, etc.), `y` is incremented.

**When a breaking change is introduced, a new `0.x` version cycle is always started.**

It is therefore safe to lock your project to a given release cycle, such as `^0.17`.

If you need to upgrade to a newer release cycle, check the [release history](https://github.com/brick/math/releases)for a list of changes introduced by each further `0.x.0` version.

### Package contents

[](#package-contents)

This library provides the following public classes in the `Brick\Math` namespace:

- [BigNumber](https://github.com/brick/math/blob/0.17.1/src/BigNumber.php): base class for `BigInteger`, `BigDecimal` and `BigRational`
- [BigInteger](https://github.com/brick/math/blob/0.17.1/src/BigInteger.php): represents an arbitrary-precision integer number.
- [BigDecimal](https://github.com/brick/math/blob/0.17.1/src/BigDecimal.php): represents an arbitrary-precision decimal number.
- [BigRational](https://github.com/brick/math/blob/0.17.1/src/BigRational.php): represents an arbitrary-precision rational number (fraction), always reduced to lowest terms.
- [RoundingMode](https://github.com/brick/math/blob/0.17.1/src/RoundingMode.php): enum representing all available rounding modes.

And [exceptions](#exceptions) in the `Brick\Math\Exception` namespace.

### Overview

[](#overview)

#### Instantiation

[](#instantiation)

The constructors of the classes are not public, you must use a factory method to obtain an instance.

All classes provide an `of()` factory method that accepts any of the following types:

- `BigNumber` instances
- `int` numbers
- `string` representations of integer, decimal and rational numbers

Example:

```
BigInteger::of(123546);
BigInteger::of('9999999999999999999999999999999999999999999');

BigDecimal::of('9.99999999999999999999999999999999999999999999');

BigRational::of('2/3');
```

Note that all `of()` methods accept all the representations above, *as long as it can be safely converted to the current type*:

```
BigInteger::of('1.00'); // 1
BigInteger::of('1.01'); // RoundingNecessaryException

BigDecimal::of('1/8'); // 0.125
BigDecimal::of('1/3'); // RoundingNecessaryException

BigRational::of('1.1'); // 11/10
BigRational::of('1.15'); // 23/20 (reduced to lowest terms)
```

Note

The `of()` factory method does not accept `float` values, because casting a float to string can be lossy. To convert a float to a `BigDecimal`, use one of the dedicated methods:

```
// Exact IEEE-754 representation — the value the float actually holds:
BigDecimal::fromFloatExact(0.1); // 0.1000000000000000055511151231257827021181583404541015625

// Shortest decimal that round-trips back to the same float:
BigDecimal::fromFloatShortest(0.1); // 0.1
```

#### Immutability &amp; chaining

[](#immutability--chaining)

The `BigInteger`, `BigDecimal` and `BigRational` classes are immutable: their value never changes, so that they can be safely passed around. All methods that return a `BigInteger`, `BigDecimal` or `BigRational`return a new object, leaving the original object unaffected:

```
$ten = BigInteger::of(10);

echo $ten->plus(5); // 15
echo $ten->multipliedBy(3); // 30
```

The methods can be chained for better readability:

```
echo BigInteger::of(10)->plus(5)->multipliedBy(3); // 45
```

#### Parameter types

[](#parameter-types)

All methods that accept a number: `plus()`, `minus()`, `multipliedBy()`, etc. accept the same types as `of()`. For example, given the following number:

```
$integer = BigInteger::of(123);
```

The following lines are equivalent:

```
$integer->multipliedBy(123);
$integer->multipliedBy('123');
$integer->multipliedBy($integer);
```

Just like `of()`, other types of `BigNumber` are acceptable, as long as they can be safely converted to the current type:

```
echo BigInteger::of(2)->multipliedBy(BigDecimal::of('2.0')); // 4
echo BigInteger::of(2)->multipliedBy(BigDecimal::of('2.5')); // RoundingNecessaryException
echo BigDecimal::of(2.5)->multipliedBy(BigInteger::of(2)); // 5.0
```

#### Division &amp; rounding

[](#division--rounding)

##### BigInteger

[](#biginteger)

By default, dividing a `BigInteger` returns the exact result of the division, or throws an exception if the remainder of the division is not zero:

```
echo BigInteger::of(999)->dividedBy(3); // 333
echo BigInteger::of(1000)->dividedBy(3); // RoundingNecessaryException
```

You can pass an optional [rounding mode](https://github.com/brick/math/blob/0.17.1/src/RoundingMode.php) to round the result, if necessary:

```
echo BigInteger::of(1000)->dividedBy(3, RoundingMode::Down); // 333
echo BigInteger::of(1000)->dividedBy(3, RoundingMode::Up); // 334
```

If you're into quotients and remainders, there are methods for this, too:

```
echo BigInteger::of(1000)->quotient(3); // 333
echo BigInteger::of(1000)->remainder(3); // 1
```

You can even get both at the same time:

```
[$quotient, $remainder] = BigInteger::of(1000)->quotientAndRemainder(3);
```

##### BigDecimal

[](#bigdecimal)

Dividing a `BigDecimal` always requires a scale to be specified. If the exact result of the division does not fit in the given scale, a [rounding mode](https://github.com/brick/math/blob/0.17.1/src/RoundingMode.php) must be provided.

```
echo BigDecimal::of(1)->dividedBy('8', 3); // 0.125
echo BigDecimal::of(1)->dividedBy('8', 2); // RoundingNecessaryException
echo BigDecimal::of(1)->dividedBy('8', 2, RoundingMode::HalfDown); // 0.12
echo BigDecimal::of(1)->dividedBy('8', 2, RoundingMode::HalfUp); // 0.13
```

If you know that the division yields a finite number of decimals places, you can use `dividedByExact()`, which will automatically compute the required scale to fit the result, or throw an exception if the division yields an infinite repeating decimal:

```
echo BigDecimal::of(1)->dividedByExact(256); // 0.00390625
echo BigDecimal::of(1)->dividedByExact(11); // RoundingNecessaryException
```

##### BigRational

[](#bigrational)

The result of the division of a `BigRational` can always be represented exactly:

```
echo BigRational::of('13/99')->dividedBy('7'); // 13/693
echo BigRational::of('13/99')->dividedBy('9/8'); // 104/891
```

#### Bitwise operations

[](#bitwise-operations)

`BigInteger` supports bitwise operations:

- `and()`
- `or()`
- `xor()`
- `not()`

and bit shifting:

- `shiftedLeft()`
- `shiftedRight()`

#### Exceptions

[](#exceptions)

All exceptions thrown by this library implement the `MathException` interface. This means that you can safely catch all exceptions thrown by this library using a single catch clause:

```
use Brick\Math\BigDecimal;
use Brick\Math\Exception\MathException;

try {
    $number = BigInteger::of(1)->dividedBy(3);
} catch (MathException $e) {
    // ...
}
```

If you need more granular control over the exceptions thrown, you can catch the specific exception classes:

- `DivisionByZeroException`
- `IntegerOverflowException`
- `InvalidArgumentException`
- `NegativeNumberException`
- `NoInverseException`
- `NumberFormatException`
- `RoundingNecessaryException`

#### Serialization

[](#serialization)

`BigInteger`, `BigDecimal` and `BigRational` can be safely serialized on a machine and unserialized on another, even if these machines do not share the same set of PHP extensions.

For example, serializing on a machine with GMP support and unserializing on a machine that does not have this extension installed will still work as expected.

#### PHPStan extension

[](#phpstan-extension)

A third-party [PHPStan extension](https://github.com/simPod/phpstan-brick-math) is available for this library. It provides more specific throw type narrowing for brick/math methods, so that PHPStan can infer the exact exception classes thrown. Note that this extension is not maintained by the author of brick/math.

###  Health Score

81

—

ExcellentBetter than 100% of packages

Maintenance97

Actively maintained with recent releases

Popularity82

Widely adopted with strong download metrics

Community51

Growing community involvement

Maturity81

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 95.8% 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 ~55 days

Recently: every ~17 days

Total

79

Last Release

15d ago

Major Versions

0.17.0 → v1.0.x-dev2026-04-19

PHP version history (9 changes)0.1.0PHP &gt;=5.5

0.2.0PHP &gt;=5.6

0.6.0PHP &gt;=7.1

0.8.15PHP ^7.1|^8.0

0.9.2PHP ^7.1 || ^8.0

0.10.0PHP ^7.4 || ^8.0

0.11.0PHP ^8.0

0.12.0PHP ^8.1

0.14.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/57189121968030f0770811b461cc92f9c19c08f5c4767292f2ede48b7277cfad?d=identicon)[BenMorel](/maintainers/BenMorel)

---

Top Contributors

[![BenMorel](https://avatars.githubusercontent.com/u/1952838?v=4)](https://github.com/BenMorel "BenMorel (711 commits)")[![Pablo1Gustavo](https://avatars.githubusercontent.com/u/34045117?v=4)](https://github.com/Pablo1Gustavo "Pablo1Gustavo (5 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (3 commits)")[![alexeyshockov](https://avatars.githubusercontent.com/u/203120?v=4)](https://github.com/alexeyshockov "alexeyshockov (3 commits)")[![julien-boudry](https://avatars.githubusercontent.com/u/4020317?v=4)](https://github.com/julien-boudry "julien-boudry (2 commits)")[![reedy](https://avatars.githubusercontent.com/u/67615?v=4)](https://github.com/reedy "reedy (2 commits)")[![vudaltsov](https://avatars.githubusercontent.com/u/2552865?v=4)](https://github.com/vudaltsov "vudaltsov (2 commits)")[![SebastienDug](https://avatars.githubusercontent.com/u/140097888?v=4)](https://github.com/SebastienDug "SebastienDug (2 commits)")[![olsavmic](https://avatars.githubusercontent.com/u/3778875?v=4)](https://github.com/olsavmic "olsavmic (1 commits)")[![simPod](https://avatars.githubusercontent.com/u/327717?v=4)](https://github.com/simPod "simPod (1 commits)")[![tomtomsen](https://avatars.githubusercontent.com/u/359547?v=4)](https://github.com/tomtomsen "tomtomsen (1 commits)")[![TRowbotham](https://avatars.githubusercontent.com/u/4984601?v=4)](https://github.com/TRowbotham "TRowbotham (1 commits)")[![williamdes](https://avatars.githubusercontent.com/u/7784660?v=4)](https://github.com/williamdes "williamdes (1 commits)")[![Copilot](https://avatars.githubusercontent.com/in/1143301?v=4)](https://github.com/Copilot "Copilot (1 commits)")[![DASPRiD](https://avatars.githubusercontent.com/u/233300?v=4)](https://github.com/DASPRiD "DASPRiD (1 commits)")[![gharlan](https://avatars.githubusercontent.com/u/330436?v=4)](https://github.com/gharlan "gharlan (1 commits)")[![GrahamCampbell](https://avatars.githubusercontent.com/u/2829600?v=4)](https://github.com/GrahamCampbell "GrahamCampbell (1 commits)")[![Izumi-kun](https://avatars.githubusercontent.com/u/1062584?v=4)](https://github.com/Izumi-kun "Izumi-kun (1 commits)")[![JesterIruka](https://avatars.githubusercontent.com/u/49654905?v=4)](https://github.com/JesterIruka "JesterIruka (1 commits)")[![mrkh995](https://avatars.githubusercontent.com/u/52885484?v=4)](https://github.com/mrkh995 "mrkh995 (1 commits)")

---

Tags

arbitrary-precisionbcmathbigdecimalbigintegerbigrationalgmpphpbrickBigIntegermathematicsdecimalmathbignumarithmeticintegerbigdecimalArbitrary-precisionbignumberBigRationalrational

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/brick-math/health.svg)

```
[![Health](https://phpackages.com/badges/brick-math/health.svg)](https://phpackages.com/packages/brick-math)
```

###  Alternatives

[markrogoyski/math-php

Math Library for PHP. Features descriptive statistics and regressions; Continuous and discrete probability distributions; Linear algebra with matrices and vectors, Numerical analysis; special mathematical functions; Algebra

2.4k7.4M47](/packages/markrogoyski-math-php)[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)[rubix/tensor

A library and extension that provides objects for scientific computing in PHP.

2801.5M5](/packages/rubix-tensor)[php-decimal/php-decimal

Correctly-rounded arbitrary precision decimal floating point

781.1M10](/packages/php-decimal-php-decimal)[prestashop/decimal

Object-oriented wrapper/shim for BC Math PHP extension. Allows for arbitrary-precision math operations.

178.7M7](/packages/prestashop-decimal)

PHPackages © 2026

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