PHPackages                             danhunsaker/bcmath - 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. danhunsaker/bcmath

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

danhunsaker/bcmath
==================

PHP bcmath as a static class, with several enhancements.

v1.1.3(8y ago)2334.1k↓100%2[2 issues](https://github.com/danhunsaker/bcmath/issues)[1 PRs](https://github.com/danhunsaker/bcmath/pulls)MITPHPPHP ^5.4|^7.0

Since Feb 16Pushed 7y ago4 watchersCompare

[ Source](https://github.com/danhunsaker/bcmath)[ Packagist](https://packagist.org/packages/danhunsaker/bcmath)[ RSS](/packages/danhunsaker-bcmath/feed)WikiDiscussions master Synced today

READMEChangelog (5)Dependencies (5)Versions (8)Used By (0)

BC::math
========

[](#bcmath)

[![Software License](https://camo.githubusercontent.com/26683f2d9f58e636d59716c551f25d4b458da4dbadc15490b07718e433f7c3e8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f64616e68756e73616b65722f62636d6174682e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Gitter](https://camo.githubusercontent.com/c28f19823b3b0fa470c93c1dcff5de8da59c80b848d81f7543fc98dadc07a6ac/68747470733a2f2f696d672e736869656c64732e696f2f6769747465722f726f6f6d2f64616e68756e73616b65722f62636d6174682e7376673f7374796c653d666c61742d737175617265)](https://gitter.im/danhunsaker/bcmath)

[![Latest Version](https://camo.githubusercontent.com/55065343505e7e8b0930ed5eefbeaa8a360b4ed33919c59232e8d6b7750ee5c9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f64616e68756e73616b65722f62636d6174682e7376673f7374796c653d666c61742d737175617265)](https://github.com/danhunsaker/bcmath/releases)[![Build Status](https://camo.githubusercontent.com/415a230bbd835341b9e63e4f5f5feb1ab3632fbe959271c294e48815f738dfeb/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f64616e68756e73616b65722f62636d6174682e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/danhunsaker/bcmath)[![Codecov](https://camo.githubusercontent.com/0bf4ff7ab8da3e811bca3caba8ec8213a09e45143427c9c6739d8025459aa951/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f64616e68756e73616b65722f62636d6174682e7376673f7374796c653d666c61742d737175617265)](https://codecov.io/gh/danhunsaker/bcmath)[![Total Downloads](https://camo.githubusercontent.com/733ef07054fa34ff3b8c0ea8623f5cb9efef25faef65d3b1a4d965d4f5639a64/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f64616e68756e73616b65722f62636d6174682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/danhunsaker/bcmath)

PHP bcmath as a static class, with several enhancements.

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

[](#installation)

Use Composer:

```
composer require danhunsaker/bcmath
```

Usage
-----

[](#usage)

Usage is nearly identical to the [bcmath extension](https://secure.php.net/manual/en/ref.bc.php) functions. The main difference is that the `bc` prefix is replaced by the `Danhunsaker\BC` class name (which you can easily alias in your project(s) via `use Danhunsaker\BC`).

In other words:

- [`bcadd()`](https://secure.php.net/manual/en/function.bcadd.php) becomes `BC::add()`
- [`bccomp()`](https://secure.php.net/manual/en/function.bccomp.php) becomes `BC::comp()`
- [`bcdiv()`](https://secure.php.net/manual/en/function.bcdiv.php) becomes `BC::div()`
- [`bcmod()`](https://secure.php.net/manual/en/function.bcmod.php) becomes `BC::mod()`
- [`bcmul()`](https://secure.php.net/manual/en/function.bcmul.php) becomes `BC::mul()`
- [`bcpow()`](https://secure.php.net/manual/en/function.bcpow.php) becomes `BC::pow()`
- [`bcpowmod()`](https://secure.php.net/manual/en/function.bcpowmod.php) becomes `BC::powmod()`
- [`bcscale()`](https://secure.php.net/manual/en/function.bcscale.php) becomes `BC::scale()`
- [`bcsqrt()`](https://secure.php.net/manual/en/function.bcsqrt.php) becomes `BC::sqrt()`
- [`bcsub()`](https://secure.php.net/manual/en/function.bcsub.php) becomes `BC::sub()`

There are also some additional convenience methods available, that aren't present in the extension:

- `BC::epow()` raises *e* to the argument's power.
- `BC::fact()` calculates the factorial of the argument.
- `BC::intval()` truncates the fractional portion of the argument, if any.
- `BC::ln()` gives the natural logarithm of the argument.
- `BC::log()` gives the base 10 logarithm of the argument (uses ln $val/ln 10).
- `BC::max()` returns the largest value in an array (the first argument).
- `BC::min()` returns the smallest value in an array (the first argument).
- `BC::modfrac()` behaves exactly like `BC::mod()`, except it will return the fractional part of any remainder as well as the integer part.
- `BC::powfrac()` supports fractional exponents, allowing roots other than the square to be calculated.
- `BC::root()` is a complement to `BC::powfrac()`, and is in fact just a convenience wrapper for it.
- `BC::round()` rounds a value to a given scale.

### Expression Parser

[](#expression-parser)

There's also `BC::parse()`, which lets you write your calculations as expressions instead of method calls. It doesn't (yet) support *everything*available via method calls, but this is planned for a later release. For the moment, here's a list of which ones *are* supported, and how to specify each in your expressions:

- `BC::add(a, b)` =&gt; `'a + b'`
- `BC::div(a, b)` =&gt; `'a / b'`
- `BC::div(a, b, 0)` =&gt; `'a \ b'`
- `BC::mod(a, b)` =&gt; `'a % b'`
- `BC::modfrac(a, b)` =&gt; `'a %% b'`
- `BC::mul(BC::div(a, b, 0), b)` =&gt; `'a \* b'`
- `BC::sub(a, BC::mod(a, b))` =&gt; `'a -% b'`
- `BC::mul(a, b)` =&gt; `'a * b'`
- `BC::pow(a, b)` =&gt; `'a ** b'`
- `BC::powfrac(a, b)` =&gt; `'a ^ b'`
- `BC::sub(a, b)` =&gt; `'a - b'`

There are also some logical expressions available, all of which will return a boolean value (true/false) instead of a number:

- `BC::comp(a, b) == 0` =&gt; `'a = b'` or `'a == b'`
- `BC::comp(a, b) == 1` =&gt; `'a > b'`
- `BC::comp(a, b) == -1` =&gt; `'a < b'`
- `BC::comp(a, b) >= 0` =&gt; `'a >= b'`
- `BC::comp(a, b)  0.5, 'x' => 5, 'b' => 0]);
```

Need to specify a scale for your expression? No problem, just pass it along in the third parameter:

```
BC::parse('{m} * {x} + {b}', ['m' => 0.5, 'x' => 5, 'b' => 0], 18);
```

You can, of course, skip the variable list by passing `null` as the second argument:

```
BC::parse('{m} * {x} + {b}', null, 18);

// Any unrecognized variables are assumed to be zero,
// so the above is the same as:

BC::parse('0 * 0 + 0', null, 18);
```

Contributions
-------------

[](#contributions)

Contributions are welcome at any time on [GitHub](https://github.com/danhunsaker/bcmath).

Security issues should be reported directly to [Dan Hunsaker](dan.hunsaker+bcmath@gmail.com) via email.

###  Health Score

36

—

LowBetter than 83% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity63

Established project with proven stability

 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 ~93 days

Recently: every ~116 days

Total

6

Last Release

3219d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1534396?v=4)[Hennik Hunsaker](/maintainers/danhunsaker)[@danhunsaker](https://github.com/danhunsaker)

---

Top Contributors

[![danhunsaker](https://avatars.githubusercontent.com/u/1534396?v=4)](https://github.com/danhunsaker "danhunsaker (17 commits)")

---

Tags

bcmathlarge number

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/danhunsaker-bcmath/health.svg)

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

###  Alternatives

[phpseclib/bcmath_compat

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

16620.4M11](/packages/phpseclib-bcmath-compat)[prestashop/decimal

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

177.9M3](/packages/prestashop-decimal)[rtlopez/decimal

An object oriented immutable arbitrary-precision arithmetic library for PHP

27258.1k2](/packages/rtlopez-decimal)[skleeschulte/base32

Base32 encoding and decoding class (RFC 4648, RFC 4648 extended hex, Crockford, z-base-32/Zooko).

17311.9k8](/packages/skleeschulte-base32)[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).

2021.8k1](/packages/aza-math)

PHPackages © 2026

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