Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Bcmath Polyfill

Bcmath Polyfill Laravel Package

nanasess/bcmath-polyfill

Pure-PHP polyfill for PHP's bcmath extension, providing bcadd, bcmul and the rest of the arbitrary-precision math functions when the native extension isn't installed. Drop-in compatibility for libraries and apps that rely on bcmath, with no server configuration changes needed.

View on GitHub
Deep Wiki
Context7

nanasess/bcmath-polyfill is a Composer-ready polyfill that provides BCMath-compatible arbitrary-precision arithmetic when PHP’s ext-bcmath extension is unavailable. It supplies userland bc* functions so applications and dependencies expecting BCMath can run in constrained environments without code changes.

  • Drop-in BCMath-style functions for common decimal operations
  • Arbitrary-precision calculations without ext-bcmath
  • Composer autoloading for easy integration
  • Useful for shared hosting, containers, and CI environments
  • Helps maintain compatibility with libraries that require BCMath
Frequently asked questions about Bcmath Polyfill
What is nanasess/bcmath-polyfill and what does it do?
It is a pure-PHP implementation of the bcmath extension's arbitrary-precision math functions, built on top of phpseclib/bcmath_compat. When the native bcmath extension is not loaded, it declares functions such as bcadd, bcsub, bcmul, bcdiv, bcpow, bcsqrt, bcmod, bccomp and bcscale in the global namespace so existing code keeps working. It also adds the newer PHP 8.4 bcmath functions that the original compat library does not cover.
How do I install bcmath-polyfill in a Laravel project?
Install it with Composer, typically as a production dependency: composer require nanasess/bcmath-polyfill. There is no service provider, facade or config file to publish, because the package only conditionally defines global functions. Once the autoloader is in place, calls to bc* functions in your application resolve to the polyfill on hosts without the extension.
Does bcmath-polyfill require the bcmath PHP extension?
No, the whole point is that it works without ext-bcmath. It ships a Composer polyfill-style autoload that only defines the bc* functions when the real extension is missing, so environments that already have bcmath keep using the fast native implementation. This makes it safe to require unconditionally across your development machines and production servers.
Which bcmath functions are supported by the polyfill?
The core set is covered: bcadd, bcsub, bcmul, bcdiv, bcmod, bcpow, bcpowmod, bcsqrt, bccomp and bcscale. On top of the phpseclib/bcmath_compat baseline, this package adds support for the new PHP 8.4 functions such as bcceil, bcfloor and bcround. Check the repository README for the exact list and any argument-handling caveats.
Which PHP and Laravel versions does this package support?
It targets modern PHP 8 runtimes since the added functions mirror the PHP 8.4 bcmath API, and it installs cleanly into Laravel applications on any framework version that meets that PHP constraint. Always confirm the exact version requirements in the package's composer.json on Packagist before upgrading, as the constraint may change between releases.
Is the polyfill slower than the native bcmath extension?
Yes. The native extension is written in C, while the polyfill performs arbitrary-precision arithmetic in userland PHP, so heavy loops of bcmath calls will be noticeably slower. For typical use cases like price calculations, tax rounding or unit conversions the difference is negligible. If you compute thousands of high-precision operations per request, benchmark before relying on the fallback in production.
How is this different from phpseclib/bcmath_compat?
It is based on phpseclib/bcmath_compat but extends it with the bcmath functions introduced in PHP 8.4, such as bcceil, bcfloor and bcround. If you only need the classic bcadd/bcmul family, the upstream compat package is sufficient; choose this one when your code targets the newer API and must still run on hosts without ext-bcmath.
Will the polyfill conflict with the real bcmath extension if both are present?
No. The package guards its function declarations, so when ext-bcmath is loaded the native functions are left untouched and the polyfill definitions are skipped. That means a shared codebase behaves identically on a local machine with the extension enabled and on a constrained host without it. It also means you cannot accidentally shadow native behaviour by installing the package.
How can I test my Laravel app against the polyfill instead of the native extension?
Run your test suite on a PHP build that has ext-bcmath disabled, for example in a CI job or a Docker image without the extension, and the polyfill will be exercised automatically. Add assertions for edge cases like division by zero, negative scales and rounding at the boundary, since userland implementations can differ subtly from the C extension. Keeping one CI job without bcmath is the simplest guard against regressions.
What are the alternatives to bcmath-polyfill for arbitrary-precision math in Laravel?
You can use brick/math for an object-oriented, immutable API with explicit rounding modes, or phpseclib's BigInteger directly if you want to avoid global function shims. The phpseclib/bcmath_compat package is the closest drop-in alternative but lacks the PHP 8.4 functions. The cleanest option is still enabling ext-bcmath on your servers and using this polyfill only as a safety net for environments you cannot control.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
novasa/laravel-language-center
opencage/geocode
adolfoholzer/parameters
livenetworks/ln-starter
shitoudev/permissionmanager
johannesclimacus/laravel-tui
hocvt/seotools
vitalybaev/sypexgeo
amphibee/themosis-ignition
gito-me/fast-excel
frankyso/laravel-ipaymu
geolize-studio/laravel-chile
boomerangui/chat
jinas/dhivatar
store-doc/laravel-storage-driver
plus-it/laravel-translation-manager
diephp/perhaps
diephp/telegraph
osmaviation/spreadsheet
microsoft/fluentui-system-icons