1
0
mirror of https://github.com/danog/math.git synced 2024-11-30 04:19:31 +01:00

Remove BigDecimal::dividedByExact(), now implemented by dividedBy()

This commit is contained in:
Benjamin Morel 2015-06-26 23:47:44 +02:00
parent d0a91c6be1
commit 7376e828c2
2 changed files with 21 additions and 39 deletions

View File

@ -156,7 +156,15 @@ final class BigDecimal extends BigNumber implements \Serializable
*/
public function dividedBy($that)
{
return $this->toBigRational()->dividedBy($that)->toBigDecimal();
$that = BigDecimal::of($that);
$result = $this->toBigRational()->dividedBy($that->toBigRational())->toBigDecimal();
if ($result->scale < $this->scale) {
$result = $result->withScale($this->scale);
}
return $result;
}
/**
@ -268,32 +276,6 @@ final class BigDecimal extends BigNumber implements \Serializable
return new BigDecimal($result, $scale);
}
/**
* Returns the result of the division of this number and the given one.
*
* The result has a minimum scale of `$this->scale`, expandable to accommodate the exact result of the division.
*
* If the result cannot be represented as a finite decimal number, an exception is thrown.
*
* @param BigDecimal|number|string $that The divisor.
*
* @return BigDecimal
*
* @throws RoundingNecessaryException If the result cannot be represented as a finite decimal number.
*/
public function dividedByExact($that)
{
$that = BigDecimal::of($that);
$result = $this->toBigRational()->dividedBy($that->toBigRational())->toBigDecimal();
if ($result->scale < $this->scale) {
$result = $result->withScale($this->scale);
}
return $result;
}
/**
* Returns the quotient and remainder of the division of this number and the given one.
*

View File

@ -565,7 +565,7 @@ class BigDecimalTest extends AbstractTestCase
}
/**
* @dataProvider providerDividedBy
* @dataProvider providerDividedByWithRounding
*
* @param string $a The base number.
* @param string $b The number to multiply.
@ -574,7 +574,7 @@ class BigDecimalTest extends AbstractTestCase
* @param string $unscaledValue The expected unscaled value of the result.
* @param int $expectedScale The expected scale of the result.
*/
public function testDividedBy($a, $b, $scale, $roundingMode, $unscaledValue, $expectedScale)
public function testDividedByWithRounding($a, $b, $scale, $roundingMode, $unscaledValue, $expectedScale)
{
$decimal = BigDecimal::of($a)->dividedByWithRounding($b, $roundingMode, $scale);
$this->assertBigDecimalEquals($unscaledValue, $expectedScale, $decimal);
@ -583,7 +583,7 @@ class BigDecimalTest extends AbstractTestCase
/**
* @return array
*/
public function providerDividedBy()
public function providerDividedByWithRounding()
{
return [
[ '7', '0.2', null, RoundingMode::UNNECESSARY, '35', 0],
@ -604,12 +604,12 @@ class BigDecimalTest extends AbstractTestCase
}
/**
* @dataProvider providerDividedByZeroThrowsException
* @dataProvider providerDividedByZeroWithRoundingThrowsException
* @expectedException \Brick\Math\Exception\DivisionByZeroException
*
* @param string|number $zero
*/
public function testDividedByZeroThrowsException($zero)
public function testDividedByZeroWithRoundingThrowsException($zero)
{
BigDecimal::of(1)->dividedByWithRounding($zero);
}
@ -617,7 +617,7 @@ class BigDecimalTest extends AbstractTestCase
/**
* @return array
*/
public function providerDividedByZeroThrowsException()
public function providerDividedByZeroWithRoundingThrowsException()
{
return [
[0],
@ -627,19 +627,19 @@ class BigDecimalTest extends AbstractTestCase
}
/**
* @dataProvider providerDividedByExact
* @dataProvider providerDividedBy
*
* @param string|number $number The number to divide.
* @param string|number $divisor The divisor.
* @param string|null $expected The expected result, or null if an exception is expected.
*/
public function testDividedByExact($number, $divisor, $expected)
public function testDividedBy($number, $divisor, $expected)
{
if ($expected === null) {
$this->setExpectedException(RoundingNecessaryException::class);
}
$actual = BigDecimal::of($number)->dividedByExact($divisor);
$actual = BigDecimal::of($number)->dividedBy($divisor);
if ($expected !== null) {
$this->assertSame($expected, (string) $actual);
@ -649,7 +649,7 @@ class BigDecimalTest extends AbstractTestCase
/**
* @return array
*/
public function providerDividedByExact()
public function providerDividedBy()
{
return [
[1, 1, '1'],
@ -675,9 +675,9 @@ class BigDecimalTest extends AbstractTestCase
/**
* @expectedException \Brick\Math\Exception\DivisionByZeroException
*/
public function testDividedByExactZero()
public function testDividedByZero()
{
BigDecimal::of(1)->dividedByExact(0);
BigDecimal::of(1)->dividedBy(0);
}
/**