Summary
Currently, the bcmath polyfill only supports 4 of the 7 RoundingMode enum values introduced in PHP 8.4:
Currently Supported:
RoundingMode::HalfAwayFromZero (maps to PHP_ROUND_HALF_UP)
RoundingMode::HalfTowardsZero (maps to PHP_ROUND_HALF_DOWN)
RoundingMode::HalfEven (maps to PHP_ROUND_HALF_EVEN)
RoundingMode::HalfOdd (maps to PHP_ROUND_HALF_ODD)
Not Yet Supported:
RoundingMode::TowardsZero
RoundingMode::AwayFromZero
RoundingMode::NegativeInfinity
These three modes currently throw a ValueError with the message "RoundingMode::{mode} is not supported" when used with BCMath::round().
Proposed Implementation
Add support for the remaining three rounding modes in the BCMath::round() method:
-
TowardsZero: Always round towards zero (truncate)
- Positive numbers: round down (floor)
- Negative numbers: round up (ceil)
-
AwayFromZero: Always round away from zero
- Positive numbers: round up (ceil)
- Negative numbers: round down (floor)
-
NegativeInfinity: Always round towards negative infinity (floor)
- Both positive and negative numbers use floor behavior
Benefits
- Complete compatibility with PHP 8.4's native RoundingMode enum
- Consistent behavior across all PHP versions (8.1-8.4+)
- Better developer experience when migrating to/from native bcmath extension
Implementation Notes
The implementation should:
- Update
BCMath::convertRoundingMode() to handle the new modes
- Update
BCMath::bcroundHelper() or create specialized logic for these modes
- Add comprehensive test coverage for all rounding scenarios
- Maintain backward compatibility with existing functionality
Acceptance Criteria
Summary
Currently, the bcmath polyfill only supports 4 of the 7 RoundingMode enum values introduced in PHP 8.4:
Currently Supported:
RoundingMode::HalfAwayFromZero(maps toPHP_ROUND_HALF_UP)RoundingMode::HalfTowardsZero(maps toPHP_ROUND_HALF_DOWN)RoundingMode::HalfEven(maps toPHP_ROUND_HALF_EVEN)RoundingMode::HalfOdd(maps toPHP_ROUND_HALF_ODD)Not Yet Supported:
RoundingMode::TowardsZeroRoundingMode::AwayFromZeroRoundingMode::NegativeInfinityThese three modes currently throw a
ValueErrorwith the message "RoundingMode::{mode} is not supported" when used withBCMath::round().Proposed Implementation
Add support for the remaining three rounding modes in the
BCMath::round()method:TowardsZero: Always round towards zero (truncate)
AwayFromZero: Always round away from zero
NegativeInfinity: Always round towards negative infinity (floor)
Benefits
Implementation Notes
The implementation should:
BCMath::convertRoundingMode()to handle the new modesBCMath::bcroundHelper()or create specialized logic for these modesAcceptance Criteria
RoundingMode::TowardsZeroworks correctly for positive and negative numbersRoundingMode::AwayFromZeroworks correctly for positive and negative numbersRoundingMode::NegativeInfinityworks correctly for positive and negative numbers