PHPackages                             tiny-blocks/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. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. tiny-blocks/math

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

tiny-blocks/math
================

Value Objects for handling arbitrary precision numbers.

3.6.0(4mo ago)322.4k—4.5%MITPHPPHP ^8.5CI passing

Since Jul 26Pushed 2mo agoCompare

[ Source](https://github.com/tiny-blocks/math)[ Packagist](https://packagist.org/packages/tiny-blocks/math)[ Docs](https://github.com/tiny-blocks/math)[ RSS](/packages/tiny-blocks-math/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (18)Used By (0)

Math
====

[](#math)

[![License](https://camo.githubusercontent.com/f8df3091bbe1149f398a5369b2c39e896766f9f6efba3477c63e9b4aa940ef14/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e)](LICENSE)

- [Overview](#overview)
- [Installation](#installation)
- [How to use](#how-to-use)
- [License](#license)
- [Contributing](#contributing)

Overview
--------

[](#overview)

Value Objects for handling arbitrary precision numbers.

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

[](#installation)

```
composer require tiny-blocks/math
```

How to use
----------

[](#how-to-use)

The library exposes some concrete implementations for arbitrary precision numbers. Concrete implementations implement the `BigNumber` interface, which provides the behaviors for the respective **BigNumbers**.

### Using the fromString method

[](#using-the-fromstring-method)

With the `fromString` method, a new instance of type `BigNumber` is created from a valid string numeric value.

```
BigDecimal::fromString(value: '10');
BigDecimal::fromString(value: '-123.456');
```

It is possible to set a `scale` for the object through this method.

```
BigDecimal::fromString(value: '10', scale: 2);
```

Always prefer to instantiate from a string, which supports an unlimited number of digits and ensures no loss of precision.

### Using the fromFloat method

[](#using-the-fromfloat-method)

With the `fromFloat` method, a new instance of type `BigNumber` is created from a valid float value.

```
BigDecimal::fromFloat(value: 10.0);
BigDecimal::fromFloat(value: -123.456);
```

It is also possible to set a `scale` for the object through this method.

```
BigDecimal::fromFloat(value: 10.0, scale: 2);
```

### Using the methods of mathematical operations

[](#using-the-methods-of-mathematical-operations)

#### Addition

[](#addition)

Performs an addition operation between this value and another value.

```
$augend = BigDecimal::fromString(value: '1');
$addend = BigDecimal::fromFloat(value: 1.0);

$result = $augend->add(addend: $addend);

$result->toString(); # 2
```

#### Subtraction

[](#subtraction)

Performs a subtraction operation between this value and another value.

```
$minuend = BigDecimal::fromString(value: '1');
$subtrahend = BigDecimal::fromFloat(value: 1.0);

$result = $minuend->subtract(subtrahend: $subtrahend);

$result->toString(); # 0
```

#### Multiplication

[](#multiplication)

Performs a multiplication operation between this value and another value.

```
$multiplicand = BigDecimal::fromString(value: '1');
$multiplier = BigDecimal::fromFloat(value: 1.0);

$result = $multiplicand->multiply(multiplier: $multiplier);

$result->toString(); # 1
```

#### Division

[](#division)

Performs a division operation between this value and another value.

```
$dividend = BigDecimal::fromString(value: '1');
$divisor = BigDecimal::fromFloat(value: 1.0);

$result = $dividend->divide(divisor: $divisor);

$result->toString(); # 1
```

### Using other resources

[](#using-other-resources)

If you need to perform rounding, you can use the `withRounding` method.

Use one of the following constants to specify the [mode](https://www.php.net/manual/en/function.round.php) in which rounding occurs:

- `HALF_UP`: Round number away from zero when halfway.

    ```
    $value = BigDecimal::fromFloat(value: 0.9950, scale: 2);

    $result = $value->withRounding(mode: RoundingMode::HALF_UP);

    $result->toString(); # 1
    ```
- `HALF_DOWN`: Round number to zero when halfway.

    ```
    $value = BigDecimal::fromFloat(value: 0.9950, scale: 2);

    $result = $value->withRounding(mode: RoundingMode::HALF_DOWN);

    $result->toString(); # 0.99
    ```
- `HALF_EVEN`: Round number to the nearest even value when halfway.

    ```
    $value = BigDecimal::fromFloat(value: 0.9950, scale: 2);

    $result = $value->withRounding(mode: RoundingMode::HALF_EVEN);

    $result->toString(); # 1
    ```
- `HALF_ODD`: Round number to the nearest odd value when halfway.

    ```
    $value = BigDecimal::fromFloat(value: 0.9950, scale: 2);

    $result = $value->withRounding(mode: RoundingMode::HALF_ODD);

    $result->toString(); # 0.99
    ```

#### Others

[](#others)

Check out other available resources by looking at the [BigNumber](src/BigNumber.php) interface.

License
-------

[](#license)

Math is licensed under [MIT](LICENSE).

Contributing
------------

[](#contributing)

Please follow the [contributing guidelines](https://github.com/tiny-blocks/tiny-blocks/blob/main/CONTRIBUTING.md) to contribute to the project.

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance81

Actively maintained with recent releases

Popularity29

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 94.4% 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 ~79 days

Recently: every ~108 days

Total

17

Last Release

123d ago

Major Versions

1.0.1 → 2.0.02023-06-08

2.1.0 → 3.0.02024-10-07

PHP version history (5 changes)1.0.0PHP ^8.1

2.0.0PHP ^8.1||^8.2

2.1.0PHP ^8.2

3.5.0PHP ^8.3

3.6.0PHP ^8.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/b00a83241d2af272255f1755bf6aff48e77821c59ef623520176d9e828977241?d=identicon)[gustavofreze](/maintainers/gustavofreze)

---

Top Contributors

[![gustavofreze](https://avatars.githubusercontent.com/u/22873481?v=4)](https://github.com/gustavofreze "gustavofreze (34 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")

---

Tags

arbitrary-precisionbignumberhacktoberfestmathnumberopen-sourcephptiny-blocksvalue-objectpsrValue ObjectvomatharithmeticArbitrary-precisiontiny-blocksbig number

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[psr/container

Common Container Interface (PHP FIG PSR-11)

10.0k1.0B3.7k](/packages/psr-container)[brick/math

Arbitrary-precision arithmetic library

2.1k504.0M277](/packages/brick-math)[psr/clock

Common interface for reading the clock.

642332.9M343](/packages/psr-clock)[lctrs/psalm-psr-container-plugin

Let Psalm understand better psr11 containers

17648.1k13](/packages/lctrs-psalm-psr-container-plugin)[mayflower/mo4-coding-standard

PHP CodeSniffer ruleset implementing the MO4 coding standards extending the Symfony coding standards.

17508.3k5](/packages/mayflower-mo4-coding-standard)

PHPackages © 2026

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