Skip to content

Commit

Permalink
Fix issue with Calculator::divQR() when bcscale() is non-zero
Browse files Browse the repository at this point in the history
Fixes #55.
  • Loading branch information
BenMorel committed Jan 20, 2021
1 parent a49cf5d commit a1c7e0e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Internal/Calculator/BcMathCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ public function divR(string $a, string $b) : string
public function divQR(string $a, string $b) : array
{
$q = \bcdiv($a, $b, 0);
$r = \bcmod($a, $b);

if (version_compare(PHP_VERSION, '7.2') >= 0) {
$r = \bcmod($a, $b, 0);
} else {
$r = \bcmod($a, $b);
}

assert($q !== null);
assert($r !== null);
Expand Down
17 changes: 17 additions & 0 deletions tests/BigDecimalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Brick\Math\Exception\NegativeNumberException;
use Brick\Math\Exception\NumberFormatException;
use Brick\Math\Exception\RoundingNecessaryException;
use Brick\Math\Internal\Calculator;
use Brick\Math\RoundingMode;

/**
Expand Down Expand Up @@ -847,6 +848,22 @@ public function testExactlyDividedByZero() : void
BigDecimal::of(1)->exactlyDividedBy(0);
}

public function testExactlyDividedByWithNonZeroBcMathDefaultScale(): void
{
if (! Calculator::get() instanceof Calculator\BcMathCalculator) {
self::markTestSkipped('This test targets the BCMath calculator only.');
}

$previousScale = bcscale();
bcscale(8);

try {
self::assertSame('0.1', (string) BigDecimal::of(1)->exactlyDividedBy(10));
} finally {
bcscale($previousScale);
}
}

/**
* @dataProvider providerDividedByWithRoundingNecessaryThrowsException
*
Expand Down

0 comments on commit a1c7e0e

Please sign in to comment.