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