Skip to content

Add support for additional RoundingMode enums (TowardsZero, AwayFromZero, NegativeInfinity) #36

@nanasess

Description

@nanasess

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:

  1. TowardsZero: Always round towards zero (truncate)

    • Positive numbers: round down (floor)
    • Negative numbers: round up (ceil)
  2. AwayFromZero: Always round away from zero

    • Positive numbers: round up (ceil)
    • Negative numbers: round down (floor)
  3. 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

  • RoundingMode::TowardsZero works correctly for positive and negative numbers
  • RoundingMode::AwayFromZero works correctly for positive and negative numbers
  • RoundingMode::NegativeInfinity works correctly for positive and negative numbers
  • All edge cases are handled (zero, very small numbers, large numbers)
  • Test coverage includes precision scenarios and boundary conditions
  • Documentation is updated to reflect the new supported modes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions