nanasess / bcmath-polyfill
PHP 8.4 bcmath functions polyfill with fallback compatibility for environments without bcmath extension. Based on phpseclib/bcmath_compat with additional support for new PHP 8.4 bcmath functions.
Requires
- php: >=8.1
- phpseclib/phpseclib: ^3.0
Requires (Dev)
- phpunit/phpunit: ^10.5
- squizlabs/php_codesniffer: ^3.0
Suggests
- ext-gmp: Will enable faster math operations
Provides
- ext-bcmath: 8.1.0
This package is auto-updated.
Last update: 2025-07-17 02:27:03 UTC
README
PHP 8.4 bcmath functions polyfill with fallback compatibility for environments without bcmath extension.
🚀 Features
- ✅ Complete bcmath extension polyfill for PHP 8.1+
- ✅ Supports all PHP 8.4 bcmath functions including
bcfloor()
,bcceil()
, andbcround()
- ✅ Zero dependencies in production (uses phpseclib for arbitrary precision math)
- ✅ Seamless fallback when bcmath extension is not available
- ✅ 100% compatible with native bcmath functions
📦 Installation
Install via Composer:
composer require nanasess/bcmath-polyfill
🔧 Usage
Simply include the polyfill in your project and use bcmath functions as you normally would:
// The polyfill will automatically load when bcmath extension is not available require_once 'vendor/autoload.php'; // All bcmath functions work identically to the native extension echo bcadd('1.234', '5.678', 2); // 6.91 echo bcmul('2.5', '3.5', 1); // 8.7 echo bcpow('2', '8'); // 256 // PHP 8.4 functions are also supported echo bcfloor('4.7'); // 4 echo bcceil('4.3'); // 5 echo bcround('3.14159', 2); // 3.14
📋 Supported Functions
Classic bcmath Functions
bcadd()
- Add two arbitrary precision numbersbcsub()
- Subtract one arbitrary precision number from anotherbcmul()
- Multiply two arbitrary precision numbersbcdiv()
- Divide two arbitrary precision numbersbcmod()
- Get modulus of an arbitrary precision numberbcpow()
- Raise an arbitrary precision number to anotherbcsqrt()
- Get the square root of an arbitrary precision numberbcscale()
- Set/get default scale parameterbccomp()
- Compare two arbitrary precision numbersbcpowmod()
- Raise an arbitrary precision number to another, reduced by a specified modulus
PHP 8.4 Functions (Added in PR #6)
bcfloor()
- Round down to the nearest integerbcceil()
- Round up to the nearest integerbcround()
- Round to a specified precision with configurable rounding modes
⚡ Performance
This polyfill uses phpseclib's BigInteger class for arbitrary precision arithmetic, providing reliable performance for applications that cannot use the native bcmath extension.
⚠️ Known Limitations
Extension Detection
extension_loaded('bcmath')
will returnfalse
when using the polyfill- Recommended approach: Don't check for the extension, just use the functions
Configuration Options
ini_set('bcmath.scale', ...)
won't work without the native extension- Use
bcscale()
instead to set the scale globally - To get the current scale:
- PHP >= 7.3.0: Use
bcscale()
without arguments - PHP < 7.3.0: Use
max(0, strlen(bcadd('0', '0')) - 2)
- PHP >= 7.3.0: Use
🔄 Key Differences from phpseclib/bcmath_compat
Feature | phpseclib/bcmath_compat | bcmath-polyfill |
---|---|---|
PHP 8.4 functions | ❌ Not supported | ✅ Full support |
bcfloor() |
❌ | ✅ |
bcceil() |
❌ | ✅ |
bcround() |
❌ | ✅ |
PHP 8.2+ deprecations | ⚠️ Warnings | ✅ Fixed |
Test suite pollution | ⚠️ Issues | ✅ Fixed |
Active maintenance | ❌ Limited | ✅ Active |
CI/CD (PHP versions) | GitHub Actions (8.1, 8.2, 8.3) | GitHub Actions (8.1, 8.2, 8.3, 8.4) |
Migration from phpseclib/bcmath_compat
Switching is seamless - no code changes required:
# Remove old package composer remove phpseclib/bcmath_compat # Install bcmath-polyfill composer require nanasess/bcmath-polyfill
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📄 License
This project is licensed under the MIT License - see the LICENSE.md file for details.
🙏 Credits
This project is a fork of phpseclib/bcmath_compat, originally created by the phpseclib team. We've extended it with PHP 8.4 function support and continue to maintain compatibility with all PHP versions.
Made with ❤️ for the PHP community