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

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

asika/math
==========

Arbitrary-precision arithmetic library for PHP 5.3

0.7.0(8y ago)0828MITPHPPHP &gt;=5.3

Since Aug 31Pushed 8y ago1 watchersCompare

[ Source](https://github.com/asika32764/math)[ Packagist](https://packagist.org/packages/asika/math)[ RSS](/packages/asika-math/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (2)Versions (21)Used By (0)

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

[](#brickmath)

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

A library to work with arbitrary precision numbers.

[![Build Status](https://camo.githubusercontent.com/0ffaa51e77ef639228cd2ad8dd6705c622e3f95e4764b789744bfda0d740e1b4/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f627269636b2f6d6174682e7376673f6272616e63683d6d6173746572)](http://travis-ci.org/brick/math)[![Coverage Status](https://camo.githubusercontent.com/6e3b9544bd77f8e720183e283e798d2db8e38c89fc5b0cbc8eb9cbdb366e1f75/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f627269636b2f6d6174682f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/r/brick/math?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/28386a5fb27903fe7f933c260b86687702e6ab17486f94f5a8d0893e2156271c/68747470733a2f2f706f7365722e707567782e6f72672f627269636b2f6d6174682f762f737461626c65)](https://packagist.org/packages/brick/math)[![License](https://camo.githubusercontent.com/0f788138977e9e481d44c2e010239470601ef3b6492e4f468677db5c6406b32a/68747470733a2f2f706f7365722e707567782e6f72672f627269636b2f6d6174682f6c6963656e7365)](https://packagist.org/packages/brick/math)

For a complete list of classes and methods, check the [API documentation](http://brick.io/math/).

### Installation

[](#installation)

This library is installable via [Composer](https://getcomposer.org/). Just define the following requirement in your `composer.json` file:

```
{
    "require": {
        "asika/math": "0.7.*"
    }
}
```

### Requirements

[](#requirements)

This library requires PHP 5.3, PHP 7 or [HHVM](http://hhvm.com/).

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 should be 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.7.*`.

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](http://brick.io/math/namespace-Brick.Math.html) namespace:

- [BigNumber](http://brick.io/math/class-Brick.Math.BigNumber.html): base class for `BigInteger`, `BigDecimal` and `BigRational`
- [BigInteger](http://brick.io/math/class-Brick.Math.BigInteger.html): represents an arbitrary-precision integer number.
- [BigDecimal](http://brick.io/math/class-Brick.Math.BigDecimal.html): represents an arbitrary-precision decimal number.
- [BigRational](http://brick.io/math/class-Brick.Math.BigRational.html): represents an arbitrary-precision rational number (fraction).
- [RoundingMode](http://brick.io/math/class-Brick.Math.RoundingMode.html): holds constants for the [rounding modes](http://brick.io/math/class-Brick.Math.RoundingMode.html).

And the following exceptions in the [Brick\\Math\\Exception](http://brick.io/math/namespace-Brick.Math.Exception.html) namespace:

- [ArithmeticException](http://brick.io/math/class-Brick.Math.Exception.ArithmeticException.html): base class for all exceptions
- [DivisionByZeroException](http://brick.io/math/class-Brick.Math.Exception.DivisionByZeroException.html): thrown when a division by zero occurs
- [NumberFormatException](http://brick.io/math/class-Brick.Math.Exception.NumberFormatException.html): thrown when parsing a number string in an invalid format
- [RoundingNecessaryException](http://brick.io/math/class-Brick.Math.Exception.RoundingNecessaryException.html): thrown when the result of the operation cannot be represented without explicit rounding

### 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
- `float` numbers
- `string` representations of integer, decimal and rational numbers

Example:

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

BigDecimal::of(1.2);
BigDecimal::of('9.99999999999999999999999999999999999999999999');

BigRational::of('2/3');
BigRational::of('1.1'); // 11/10
```

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

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

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

Note about native integers: instantiating from an `int` is safe *as long as you don't exceed the maximum value for your platform* (`PHP_INT_MAX`), in which case it would be transparently converted to `float` by PHP without notice, and could result in a loss of information. In doubt, prefer instantiating from a `string`, which supports an unlimited numbers of digits:

```
echo BigInteger::of(999999999999999999999); // 1000000000000000000000
echo BigInteger::of('999999999999999999999'); // 999999999999999999999
```

Note about floating-point values: instantiating from a `float` might be unsafe, as floating-point values are imprecise by design, and could result in a loss of information. Always prefer instantiating from a `string`, which supports an unlimited number of digits:

```
echo BigDecimal::of(1.99999999999999999999); // 2
echo BigDecimal::of('1.99999999999999999999'); // 1.99999999999999999999
```

#### 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')); // ArithmeticException
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](http://brick.io/math/class-Brick.Math.RoundingMode.html) 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:

```
list ($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](http://brick.io/math/class-Brick.Math.RoundingMode.html) 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::HALF_DOWN); // 0.12
echo BigDecimal::of(1)->dividedBy('8', 2, RoundingMode::HALF_UP); // 0.13
```

If you know that the division yields a finite number of decimals places, you can use `exactlyDividedBy()`, 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)->exactlyDividedBy(256); // 0.00390625
echo BigDecimal::of(1)->exactlyDividedBy(11); // RoundingNecessaryException
```

##### BigRational

[](#bigrational)

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

```
echo BigRational::of('123/456')->dividedBy('7'); // 123/3192
echo BigRational::of('123/456')->dividedBy('9/8'); // 984/4104
```

#### 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.

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 96.1% 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 ~61 days

Recently: every ~183 days

Total

18

Last Release

3274d ago

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

0.2.0PHP &gt;=5.6

0.6.0PHP &gt;=5.4

0.7.0PHP &gt;=5.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1639206?v=4)[Simon Asika](/maintainers/asika32764)[@asika32764](https://github.com/asika32764)

---

Top Contributors

[![BenMorel](https://avatars.githubusercontent.com/u/1952838?v=4)](https://github.com/BenMorel "BenMorel (147 commits)")[![asika32764](https://avatars.githubusercontent.com/u/1639206?v=4)](https://github.com/asika32764 "asika32764 (3 commits)")[![nicholas-eden](https://avatars.githubusercontent.com/u/2496835?v=4)](https://github.com/nicholas-eden "nicholas-eden (3 commits)")

---

Tags

bcmathbcmath-polyfillbigintbigintegerdecimalgmpgmp-polyfillprecisionbrickBigIntegermatharithmeticbigdecimalArbitrary-precisionBigRational

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[brick/math

Arbitrary-precision arithmetic library

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

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

16721.2M25](/packages/phpseclib-bcmath-compat)[rubix/tensor

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

2791.5M5](/packages/rubix-tensor)[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

791.1M22](/packages/krowinski-bcmath-extended)[samsara/fermat

A library providing math and statistics operations for numbers of arbitrary size.

653.1k3](/packages/samsara-fermat)[phpmath/biginteger

A PHP library to work with big integers.

246.1k1](/packages/phpmath-biginteger)

PHPackages © 2026

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