Skip to content

update RoundingMode to be compatiable with already upgraded brick/math #588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

kurund
Copy link

@kurund kurund commented Feb 19, 2025

Fixes #586

@kurund kurund requested a review from ramsey as a code owner February 19, 2025 12:35
@ramsey
Copy link
Owner

ramsey commented Mar 3, 2025

This seems like a BC break. I don't think I can include this in the current 4.x series without potentially breaking a lot of users' applications.

@ramsey ramsey mentioned this pull request May 25, 2025
7 tasks
@ramsey
Copy link
Owner

ramsey commented Jun 1, 2025

@kurund, thanks for your contribution!

After more investigation, I don't think there's a need for this change, since this array performs the translation between this library's RoundingMode and brick/math's RoundingMode:

private const ROUNDING_MODE_MAP = [
RoundingMode::UNNECESSARY => BrickMathRounding::UNNECESSARY,
RoundingMode::UP => BrickMathRounding::UP,
RoundingMode::DOWN => BrickMathRounding::DOWN,
RoundingMode::CEILING => BrickMathRounding::CEILING,
RoundingMode::FLOOR => BrickMathRounding::FLOOR,
RoundingMode::HALF_UP => BrickMathRounding::HALF_UP,
RoundingMode::HALF_DOWN => BrickMathRounding::HALF_DOWN,
RoundingMode::HALF_CEILING => BrickMathRounding::HALF_CEILING,
RoundingMode::HALF_FLOOR => BrickMathRounding::HALF_FLOOR,
RoundingMode::HALF_EVEN => BrickMathRounding::HALF_EVEN,
];

That translation allows code like this to work on both older versions of brick/math and newer versions that have the enum:

public function divide(
int $roundingMode,
int $scale,
NumberInterface $dividend,
NumberInterface ...$divisors
): NumberInterface {
$brickRounding = $this->getBrickRoundingMode($roundingMode);
$quotient = BigDecimal::of($dividend->toString());
foreach ($divisors as $divisor) {
$quotient = $quotient->dividedBy($divisor->toString(), $scale, $brickRounding);
}
if ($scale === 0) {
return new IntegerObject((string) $quotient->toBigInteger());
}
return new Decimal((string) $quotient);
}

If we change this library's RoundingMode to be an enum, then this library will no longer be compatible with versions of brick/math earlier than 0.12.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Outdated version of RoundingMode.php results in fatal
2 participants