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

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

aza/math
========

AzaMath - Anizoptera CMF mathematic component. Arbitrary precision arithmetic (for huge integers; BCMath wrapper) and universal convertor between positional numeral systems (supported bases from 2 to 62 inclusive, and systems with custom alphabet; pure PHP realisation, can use GMP and core PHP functions for speed optimization).

v1.0.3(12y ago)1921.9k↓40%11MITPHPPHP &gt;=5.3.3

Since Feb 9Pushed 9y ago4 watchersCompare

[ Source](https://github.com/Anizoptera/Math)[ Packagist](https://packagist.org/packages/aza/math)[ Docs](https://github.com/Anizoptera/Math)[ RSS](/packages/aza-math/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (5)Used By (1)

AzaMath
=======

[](#azamath)

Anizoptera CMF mathematic component.

[![Build Status](https://camo.githubusercontent.com/25328e49ea4c680024132faa974c1a8ba660c7851117b69a66e1b2315db61472/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f416e697a6f70746572612f4d6174682e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/Anizoptera/Math)

Table of Contents
-----------------

[](#table-of-contents)

1. [Introduction](#introduction)
2. [Requirements](#requirements)
3. [Installation](#installation)
4. [Examples](#examples)
    - [Numeral systems conversions](#example-1---numeral-systems-conversions)
    - [Custom numeral system](#example-2---custom-numeral-system)
    - [Arbitrary precision arithmetic](#example-3---arbitrary-precision-arithmetic)
    - [Input filtration](#example-4---input-filtration)
    - [Do some operations and then convert to base62](#example-5---do-some-operations-and-then-convert-to-base62)
5. [Tests](#tests)
6. [Credits](#credits)
7. [License](#license)
8. [Links](#links)

Introduction
------------

[](#introduction)

Provides functionality to work with large numbers with arbitrary precision (using BCMath). and universal convertor between positional numeral systems (supported bases from 2 to 62 inclusive, and custom systems; pure PHP realisation, but can use GMP and core PHP functions for speed optimization).

Features:

- Functionality to work with large numbers (integers, floats) with arbitrary precision (requires [BCMath](http://php.net/bcmath)). Can work with floats (E notation too!) and without loosing precision (as far as possible). It supports all basic arithmetic, exponentiation, square root, modulus, bit shift, rounding, comparison, and some other operations.
- Very simple math for big integers (only integers!, has a native PHP realization and can use BCMath or GMP for speedup).
- Universal number (and huge number!) convertor between positional numeral systems (supported bases from 2 to 62 inclusive, and systems with custom alphabet; pure PHP realisation, but can use [GMP](http://php.net/gmp) and [core PHP](http://php.net/math) functions for speed optimization). Negative and huge integers are supported.
- Convenient, fully documented and test covered API.

Requirements
------------

[](#requirements)

- PHP 5.3.3 (or later);
- [BCMath (Binary Calculator Arbitrary Precision Mathematics)](http://php.net/bcmath) - Required only to work with arbitrary precision arithmetic operations;
- [GMP (GNU Multiple Precision)](http://php.net/gmp) - Recommended. Used to speed up number systems conversions and arbitrary precision arithmetic operations;

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

[](#installation)

The recommended way to install AzaMath is [through composer](http://getcomposer.org). You can see [package information on Packagist](https://packagist.org/packages/aza/math).

```
{
    "require": {
        "aza/math": "~1.0"
    }
}
```

Examples
--------

[](#examples)

You can use [examples/example.php](examples/example.php) to run all examples.

#### Example #1 - Numeral systems conversions

[](#example-1---numeral-systems-conversions)

```
$res = NumeralSystem::convert('WIKIPEDIA', 36, 10);
echo $res . PHP_EOL; // 91730738691298

$res = NumeralSystem::convert('9173073869129891730738691298', 10, 16);
echo $res . PHP_EOL; // 1da3c9f2dd3133d4ed04bce2

$res = NumeralSystem::convertTo('9173073869129891730738691298', 62);
echo $res . PHP_EOL; // BvepB3yk4UBFhGew

$res = NumeralSystem::convertFrom('BvepB3yk4UBFhGew', 62);
echo $res . PHP_EOL; // 9173073869129891730738691298
```

#### Example #2 - Custom numeral system

[](#example-2---custom-numeral-system)

```
// Add new system with custom alphabet
// Each char must appear only once.
// It should use only one byte characters.
$alphabet = '!@#$%^&*()_+=-'; // base 14 equivalent
$system   = 'StrangeSystem';
NumeralSystem::setSystem($system, $alphabet);

$number = '9999';
$res = NumeralSystem::convertTo($number, $system);
echo $res . PHP_EOL; // $)!$

$res = NumeralSystem::convertFrom($res, $system);
echo $res . PHP_EOL; // 9999

// Full binary alphabet
$system = 'binary';
NumeralSystem::setSystem($system, $alphabet);
// Examples with it
$var = 'example';
$expected_hex = ltrim(sha1($var), '0'); // sha1 can return hex value padded with zeros
$expected_bin = sha1($var, true);       // raw sha1 hash (binary representation)
$result_hex   = NumeralSystem::convert($expected_bin, $system, 16);
$result_bin   = NumeralSystem::convert($expected_hex, 16, $system);
echo $expected_hex . PHP_EOL; // c3499c2729730a7f807efb8676a92dcb6f8a3f8f
echo $result_hex . PHP_EOL;   // c3499c2729730a7f807efb8676a92dcb6f8a3f8f
echo ($expected_bin === $result_bin) . PHP_EOL; // 1
```

#### Example #3 - Arbitrary precision arithmetic

[](#example-3---arbitrary-precision-arithmetic)

```
// Create new big number with the specified precision for operations - 20 (default is 100)
$number = new BigNumber('118059162071741130342591466421', 20);

// Divide number
$number->divide(12345678910);
echo $number . PHP_EOL; // 9562792207086578954.49764831288650451382

// Divide again and round with the specified precision and algorithm
// Three round algorithms a supported: HALF_UP, HALF_DOWN, CUT.
// You can use them as BigNumber::ROUND_* or PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN.
// Default is HALF_UP.
$number->divide(9876543210)->round(3, PHP_ROUND_HALF_DOWN);
echo $number . PHP_EOL; // 968232710.955

// Comparisions
$number = new BigNumber(10);
echo ($number->compareTo(20) < 0) . PHP_EOL; // 1
echo $number->isLessThan(20) . PHP_EOL; // 1

$number = new BigNumber(20);
echo ($number->compareTo(10) > 0) . PHP_EOL; // 1
echo $number->isGreaterThan(10) . PHP_EOL; // 1

$number = new BigNumber(20);
echo ($number->compareTo(20) === 0) . PHP_EOL; // 1
echo $number->isLessThanOrEqualTo(20) . PHP_EOL; // 1
```

#### Example #4 - Input filtration

[](#example-4---input-filtration)

```
// The arguments of all functions are also filtered.
$number = new BigNumber("9,223 372`036'854,775.808000");
echo $number . PHP_EOL; // 9223372036854775.808
```

#### Example #5 - Do some operations and then convert to base62

[](#example-5---do-some-operations-and-then-convert-to-base62)

```
$number = new BigNumber('9223372036854775807');
$number = $number->pow(2)->convertToBase(62);
echo $number . PHP_EOL; // 1wlVYJaWMuw53lV7Cg98qn
```

Tests
-----

[](#tests)

Tests are in the `Tests` folder and reach 100% code-coverage. To run them, you need PHPUnit. Example:

```
$ phpunit --configuration phpunit.xml.dist

```

Or with coverage report:

```
$ phpunit --configuration phpunit.xml.dist --coverage-html code_coverage/

```

Credits
-------

[](#credits)

AzaMath is a part of [Anizoptera CMF](https://github.com/Anizoptera), written by [Amal Samally](http://azagroup.ru/about/#amal) (amal.samally at gmail.com) and [AzaGroup](http://azagroup.ru/) team.
Arbitrary precision arithmetic part is partially based on [Moontoast Math Library](https://github.com/moontoast/math).

License
-------

[](#license)

Released under the [MIT](LICENSE.md) license.

Links
-----

[](#links)

- [Composer package](https://packagist.org/packages/aza/math)
- [Last build on the Travis CI](https://travis-ci.org/Anizoptera/Math)
- [Project profile on the Ohloh](https://www.ohloh.net/p/AzaMath)
- (RU) [AzaMath — Cистемы счисления (включая кастомные) + арифметика произвольной точности на PHP](http://habrahabr.ru/post/168935/)
- Other Anizoptera CMF components on the [GitHub](https://github.com/Anizoptera) / [Packagist](https://packagist.org/packages/aza)
- (RU) [AzaGroup team blog](http://azagroup.ru/)

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity34

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 97.2% 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 ~35 days

Total

4

Last Release

4738d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/356b06126f37416ba259cc00868ae0960bad500f459186a25a7cc3b90c80fcb5?d=identicon)[amal](/maintainers/amal)

---

Top Contributors

[![art-shen](https://avatars.githubusercontent.com/u/423417?v=4)](https://github.com/art-shen "art-shen (35 commits)")[![marthamoniz](https://avatars.githubusercontent.com/u/5338982?v=4)](https://github.com/marthamoniz "marthamoniz (1 commits)")

---

Tags

anizoptera-cmfarithmeticbcmathcomposercustom-numeralgmpmathnumeral-systemnumeral-systems-conversionspackagistphpphp-librarypositional-numeral-systemstravisconvertmathematicsdecimalmathbignumarithmeticintegercalculationbasenumberbase 62precisionbcmathgmparbitrarybigbigintbinary calculatorfractionbase 36radixhugepositional

### Embed Badge

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

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

###  Alternatives

[brick/math

Arbitrary-precision arithmetic library

2.1k504.0M277](/packages/brick-math)[ionux/phactor

Phactor is a high-performance PHP implementation of the elliptic curve math functions required to generate &amp; verify private/public (asymmetric) EC keypairs and ECDSA signatures based on secp256k1 curve parameters. This library also includes a class to generate Service Identification Numbers (SINs) based on the published Identity Protocol v1 spec.

5275.0k30](/packages/ionux-phactor)[ikr/money-math

gmp-based arbitrary precision operations on currency amounts "XXX.YY"; because floats are BAD for representing money

3115.0k1](/packages/ikr-money-math)[php-decimal/php-decimal

Correctly-rounded arbitrary precision decimal floating point

781.0M9](/packages/php-decimal-php-decimal)[prestashop/decimal

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

178.2M5](/packages/prestashop-decimal)[rtlopez/decimal

An object oriented immutable arbitrary-precision arithmetic library for PHP

27262.8k2](/packages/rtlopez-decimal)

PHPackages © 2026

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