mirror of
https://github.com/danog/math.git
synced 2024-11-26 20:04:46 +01:00
Add basic arithmetic operations to BigNumber
- BigInteger::dividedBy() previous implementation is now quotient() - BigDecimal::dividedBy() previous implementation is now dividedByWithRounding()
This commit is contained in:
parent
eae8ddc49f
commit
1b8b3e43bb
@ -1,7 +1,7 @@
|
||||
Brick\Math
|
||||
==========
|
||||
|
||||
A library to work with arbitrary precision rational numbers.
|
||||
A library to work with arbitrary precision numbers.
|
||||
|
||||
[![Build Status](https://secure.travis-ci.org/brick/math.svg?branch=master)](http://travis-ci.org/brick/math)
|
||||
[![Coverage Status](https://coveralls.io/repos/brick/math/badge.svg?branch=master)](https://coveralls.io/r/brick/math?branch=master)
|
||||
|
@ -89,13 +89,7 @@ final class BigDecimal extends BigNumber implements \Serializable
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of this number and the given one.
|
||||
*
|
||||
* The result has a scale of `max($this->scale, $that->scale)`.
|
||||
*
|
||||
* @param BigDecimal|number|string $that
|
||||
*
|
||||
* @return BigDecimal
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function plus($that)
|
||||
{
|
||||
@ -114,13 +108,7 @@ final class BigDecimal extends BigNumber implements \Serializable
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the difference of this number and the given one.
|
||||
*
|
||||
* The result has a scale of `max($this->scale, $that->scale)`.
|
||||
*
|
||||
* @param BigDecimal|number|string $that
|
||||
*
|
||||
* @return BigDecimal
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function minus($that)
|
||||
{
|
||||
@ -139,18 +127,17 @@ final class BigDecimal extends BigNumber implements \Serializable
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result of the multiplication of this number and the given one.
|
||||
*
|
||||
* The result has a scale of `$this->scale + $that->scale`.
|
||||
*
|
||||
* @param BigDecimal|number|string $that
|
||||
*
|
||||
* @return BigDecimal
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function multipliedBy($that)
|
||||
{
|
||||
$that = BigDecimal::of($that);
|
||||
$that = BigNumber::of($that);
|
||||
|
||||
if ($that instanceof BigInteger) {
|
||||
$that = $that->toBigDecimal();
|
||||
}
|
||||
|
||||
if ($that instanceof BigDecimal) {
|
||||
if ($that->value === '1' && $that->scale === 0) {
|
||||
return $this;
|
||||
}
|
||||
@ -161,6 +148,17 @@ final class BigDecimal extends BigNumber implements \Serializable
|
||||
return new BigDecimal($value, $scale);
|
||||
}
|
||||
|
||||
return $that->multipliedBy($this)->toBigDecimal();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function dividedBy($that)
|
||||
{
|
||||
return $this->toBigRational()->dividedBy($that)->toBigDecimal();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result of the division of this number and the given one.
|
||||
*
|
||||
@ -174,7 +172,7 @@ final class BigDecimal extends BigNumber implements \Serializable
|
||||
* @throws RoundingNecessaryException If RoundingMode::UNNECESSARY is provided and rounding was necessary.
|
||||
* @throws \InvalidArgumentException If any of the arguments is not valid.
|
||||
*/
|
||||
public function dividedBy($that, $roundingMode = RoundingMode::UNNECESSARY, $scale = null)
|
||||
public function dividedByWithRounding($that, $roundingMode = RoundingMode::UNNECESSARY, $scale = null)
|
||||
{
|
||||
$that = BigDecimal::of($that);
|
||||
|
||||
@ -372,7 +370,7 @@ final class BigDecimal extends BigNumber implements \Serializable
|
||||
return $this;
|
||||
}
|
||||
|
||||
return $this->dividedBy(1, $roundingMode, $scale);
|
||||
return $this->dividedByWithRounding(1, $roundingMode, $scale);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -572,7 +570,7 @@ final class BigDecimal extends BigNumber implements \Serializable
|
||||
if ($this->scale === 0) {
|
||||
$zeroScaleDecimal = $this;
|
||||
} else {
|
||||
$zeroScaleDecimal = $this->dividedBy(1, RoundingMode::UNNECESSARY, 0);
|
||||
$zeroScaleDecimal = $this->dividedByWithRounding(1, RoundingMode::UNNECESSARY, 0);
|
||||
}
|
||||
|
||||
return BigInteger::of($zeroScaleDecimal->value);
|
||||
|
@ -4,6 +4,7 @@ namespace Brick\Math;
|
||||
|
||||
use Brick\Math\Exception\ArithmeticException;
|
||||
use Brick\Math\Exception\DivisionByZeroException;
|
||||
use Brick\Math\Exception\RoundingNecessaryException;
|
||||
use Brick\Math\Internal\Calculator;
|
||||
|
||||
/**
|
||||
@ -136,11 +137,7 @@ final class BigInteger extends BigNumber implements \Serializable
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of this number and the given one.
|
||||
*
|
||||
* @param BigInteger|int|string $that
|
||||
*
|
||||
* @return BigInteger
|
||||
* {@inheritdoc
|
||||
*/
|
||||
public function plus($that)
|
||||
{
|
||||
@ -156,11 +153,7 @@ final class BigInteger extends BigNumber implements \Serializable
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the difference of this number and the given one.
|
||||
*
|
||||
* @param BigInteger|int|string $that
|
||||
*
|
||||
* @return BigInteger
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function minus($that)
|
||||
{
|
||||
@ -176,16 +169,13 @@ final class BigInteger extends BigNumber implements \Serializable
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result of the multiplication of this number and the given one.
|
||||
*
|
||||
* @param BigInteger|int|string $that
|
||||
*
|
||||
* @return BigInteger
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function multipliedBy($that)
|
||||
{
|
||||
$that = BigInteger::of($that);
|
||||
$that = BigNumber::of($that);
|
||||
|
||||
if ($that instanceof BigInteger) {
|
||||
if ($that->value === '1') {
|
||||
return $this;
|
||||
}
|
||||
@ -195,16 +185,47 @@ final class BigInteger extends BigNumber implements \Serializable
|
||||
return new BigInteger($value);
|
||||
}
|
||||
|
||||
return $that->multipliedBy($this)->toBigInteger();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function dividedBy($that)
|
||||
{
|
||||
$that = BigNumber::of($that);
|
||||
|
||||
if ($that instanceof BigInteger) {
|
||||
if ($that->value === '1') {
|
||||
return $this;
|
||||
}
|
||||
|
||||
if ($that->value === '0') {
|
||||
throw DivisionByZeroException::divisionByZero();
|
||||
}
|
||||
|
||||
list ($quotient, $remainder) = Calculator::get()->div($this->value, $that->value);
|
||||
|
||||
if ($remainder !== '0') {
|
||||
throw RoundingNecessaryException::roundingNecessary();
|
||||
}
|
||||
|
||||
return new BigInteger($quotient);
|
||||
}
|
||||
|
||||
return $this->toBigRational()->dividedBy($that)->toBigInteger();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the quotient of the division of this number and the given one.
|
||||
*
|
||||
* @param BigInteger|int|string $that
|
||||
* @param BigNumber|number|string $that The divisor. Must be convertible to an integer number.
|
||||
*
|
||||
* @return BigInteger
|
||||
*
|
||||
* @throws DivisionByZeroException If the divisor is zero.
|
||||
*/
|
||||
public function dividedBy($that)
|
||||
public function quotient($that)
|
||||
{
|
||||
$that = BigInteger::of($that);
|
||||
|
||||
@ -221,6 +242,28 @@ final class BigInteger extends BigNumber implements \Serializable
|
||||
return new BigInteger($quotient);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the remainder of the division of this number and the given one.
|
||||
*
|
||||
* @param BigInteger|int|string $that
|
||||
*
|
||||
* @return BigInteger
|
||||
*
|
||||
* @throws DivisionByZeroException If the divisor is zero.
|
||||
*/
|
||||
public function remainder($that)
|
||||
{
|
||||
$that = BigInteger::of($that);
|
||||
|
||||
if ($that->value === '0') {
|
||||
throw DivisionByZeroException::divisionByZero();
|
||||
}
|
||||
|
||||
list (, $remainder) = Calculator::get()->div($this->value, $that->value);
|
||||
|
||||
return new BigInteger($remainder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the quotient and remainder of the division of this number and the given one.
|
||||
*
|
||||
@ -230,7 +273,7 @@ final class BigInteger extends BigNumber implements \Serializable
|
||||
*
|
||||
* @throws DivisionByZeroException If the divisor is zero.
|
||||
*/
|
||||
public function divideAndRemainder($that)
|
||||
public function quotientAndRemainder($that)
|
||||
{
|
||||
$that = BigInteger::of($that);
|
||||
|
||||
@ -246,22 +289,6 @@ final class BigInteger extends BigNumber implements \Serializable
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the remainder of the division of this number and the given one.
|
||||
*
|
||||
* @param BigInteger|int|string $that
|
||||
*
|
||||
* @return BigInteger
|
||||
*/
|
||||
public function remainder($that)
|
||||
{
|
||||
$that = BigInteger::of($that);
|
||||
|
||||
list (, $remainder) = Calculator::get()->div($this->value, $that->value);
|
||||
|
||||
return new BigInteger($remainder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this number exponentiated.
|
||||
*
|
||||
|
@ -235,12 +235,67 @@ abstract class BigNumber
|
||||
return $number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of this number and the given one.
|
||||
*
|
||||
* @param BigNumber|number|string $that
|
||||
*
|
||||
* @return static
|
||||
*
|
||||
* @throws ArithmeticException If the number is not valid, or the result cannot be represented by the current type.
|
||||
*/
|
||||
abstract public function plus($that);
|
||||
|
||||
/**
|
||||
* Returns the difference of this number and the given one.
|
||||
*
|
||||
* @param BigNumber|number|string $that
|
||||
*
|
||||
* @return static
|
||||
*
|
||||
* @throws ArithmeticException If the number is not valid, or the result cannot be represented by the current type.
|
||||
*/
|
||||
abstract public function minus($that);
|
||||
|
||||
/**
|
||||
* Returns the product of this number and the given one.
|
||||
*
|
||||
* @param BigNumber|number|string $that
|
||||
*
|
||||
* @return static
|
||||
*
|
||||
* @throws ArithmeticException If the number is not valid, or the result cannot be represented by the current type.
|
||||
*/
|
||||
abstract public function multipliedBy($that);
|
||||
|
||||
/**
|
||||
* Returns the exact result of the division of this number by the given one.
|
||||
*
|
||||
* @param BigNumber|number|string $that
|
||||
*
|
||||
* @return static
|
||||
*
|
||||
* @throws ArithmeticException If the number is not valid, or the result cannot be represented by the current type, or the divisor is zero.
|
||||
*/
|
||||
abstract public function dividedBy($that);
|
||||
|
||||
/**
|
||||
* @param int $exponent
|
||||
*
|
||||
* @return static
|
||||
*
|
||||
* @throws ArithmeticException If the number is not valid.
|
||||
*/
|
||||
abstract public function power($exponent);
|
||||
|
||||
/**
|
||||
* Compares this number to the given one.
|
||||
*
|
||||
* @param BigNumber|number|string $that
|
||||
*
|
||||
* @return int [-1,0,1]
|
||||
*
|
||||
* @throws ArithmeticException If the number is not valid.
|
||||
*/
|
||||
abstract public function compareTo($that);
|
||||
|
||||
|
@ -154,6 +154,24 @@ final class BigRational extends BigNumber implements \Serializable
|
||||
return new BigRational($numerator, $denominator, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function power($exponent)
|
||||
{
|
||||
$exponent = (int) $exponent;
|
||||
|
||||
if ($exponent === 1) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
return new BigRational(
|
||||
$this->numerator->power($exponent),
|
||||
$this->denominator->power($exponent),
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the reciprocal of this BigRational.
|
||||
*
|
||||
@ -197,8 +215,8 @@ final class BigRational extends BigNumber implements \Serializable
|
||||
{
|
||||
$gcd = $this->numerator->gcd($this->denominator);
|
||||
|
||||
$numerator = $this->numerator->dividedBy($gcd);
|
||||
$denominator = $this->denominator->dividedBy($gcd);
|
||||
$numerator = $this->numerator->quotient($gcd);
|
||||
$denominator = $this->denominator->quotient($gcd);
|
||||
|
||||
return new BigRational($numerator, $denominator, false);
|
||||
}
|
||||
@ -214,7 +232,7 @@ final class BigRational extends BigNumber implements \Serializable
|
||||
|
||||
foreach ([2, 5] as $divisor) {
|
||||
do {
|
||||
list ($quotient, $remainder) = $denominator->divideAndRemainder($divisor);
|
||||
list ($quotient, $remainder) = $denominator->quotientAndRemainder($divisor);
|
||||
|
||||
if ($remainderIsZero = $remainder->isZero()) {
|
||||
$denominator = $quotient;
|
||||
@ -272,7 +290,7 @@ final class BigRational extends BigNumber implements \Serializable
|
||||
|
||||
foreach ([2, 5] as $divisor) {
|
||||
do {
|
||||
list ($quotient, $remainder) = $denominator->divideAndRemainder($divisor);
|
||||
list ($quotient, $remainder) = $denominator->quotientAndRemainder($divisor);
|
||||
|
||||
if ($remainderIsZero = $remainder->isZero()) {
|
||||
$denominator = $quotient;
|
||||
@ -289,7 +307,7 @@ final class BigRational extends BigNumber implements \Serializable
|
||||
$maxDecimalPlaces = max($counts[2], $counts[5]);
|
||||
|
||||
$result = BigDecimal::of($simplified->numerator)
|
||||
->dividedBy($simplified->denominator, RoundingMode::UNNECESSARY, $maxDecimalPlaces);
|
||||
->dividedByWithRounding($simplified->denominator, RoundingMode::UNNECESSARY, $maxDecimalPlaces);
|
||||
|
||||
return $result->stripTrailingZeros();
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase
|
||||
* @param string $expected The expected value as a string.
|
||||
* @param BigInteger $actual The BigInteger instance to test.
|
||||
*/
|
||||
protected function assertBigIntegerEquals($expected, BigInteger $actual)
|
||||
final protected function assertBigIntegerEquals($expected, BigInteger $actual)
|
||||
{
|
||||
$this->assertSame($expected, (string) $actual);
|
||||
}
|
||||
@ -25,7 +25,7 @@ abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase
|
||||
* @param int $scale The expected scale.
|
||||
* @param BigDecimal $actual The BigDecimal instance to test.
|
||||
*/
|
||||
protected function assertBigDecimalEquals($unscaledValue, $scale, BigDecimal $actual)
|
||||
final protected function assertBigDecimalEquals($unscaledValue, $scale, BigDecimal $actual)
|
||||
{
|
||||
$this->assertSame($unscaledValue, $actual->getUnscaledValue());
|
||||
$this->assertSame($scale, $actual->getScale());
|
||||
@ -36,9 +36,19 @@ abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase
|
||||
* @param string $denominator
|
||||
* @param BigRational $actual
|
||||
*/
|
||||
protected function assertBigRationalEquals($numerator, $denominator, BigRational $actual)
|
||||
final protected function assertBigRationalEquals($numerator, $denominator, BigRational $actual)
|
||||
{
|
||||
$this->assertSame($numerator, (string) $actual->getNumerator());
|
||||
$this->assertSame($denominator, (string) $actual->getDenominator());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
final protected function isException($name)
|
||||
{
|
||||
return substr($name, -9) === 'Exception';
|
||||
}
|
||||
}
|
||||
|
@ -576,7 +576,7 @@ class BigDecimalTest extends AbstractTestCase
|
||||
*/
|
||||
public function testDividedBy($a, $b, $scale, $roundingMode, $unscaledValue, $expectedScale)
|
||||
{
|
||||
$decimal = BigDecimal::of($a)->dividedBy($b, $roundingMode, $scale);
|
||||
$decimal = BigDecimal::of($a)->dividedByWithRounding($b, $roundingMode, $scale);
|
||||
$this->assertBigDecimalEquals($unscaledValue, $expectedScale, $decimal);
|
||||
}
|
||||
|
||||
@ -611,7 +611,7 @@ class BigDecimalTest extends AbstractTestCase
|
||||
*/
|
||||
public function testDividedByZeroThrowsException($zero)
|
||||
{
|
||||
BigDecimal::of(1)->dividedBy($zero);
|
||||
BigDecimal::of(1)->dividedByWithRounding($zero);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -690,7 +690,7 @@ class BigDecimalTest extends AbstractTestCase
|
||||
*/
|
||||
public function testDividedByWithRoundingNecessaryThrowsException($a, $b, $scale)
|
||||
{
|
||||
BigDecimal::of($a)->dividedBy($b, RoundingMode::UNNECESSARY, $scale);
|
||||
BigDecimal::of($a)->dividedByWithRounding($b, RoundingMode::UNNECESSARY, $scale);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -710,7 +710,7 @@ class BigDecimalTest extends AbstractTestCase
|
||||
*/
|
||||
public function testDividedByWithNegativeScaleThrowsException()
|
||||
{
|
||||
BigDecimal::of(1)->dividedBy(2, RoundingMode::UNNECESSARY, -1);
|
||||
BigDecimal::of(1)->dividedByWithRounding(2, RoundingMode::UNNECESSARY, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -718,7 +718,7 @@ class BigDecimalTest extends AbstractTestCase
|
||||
*/
|
||||
public function testDividedByWithInvalidRoundingModeThrowsException()
|
||||
{
|
||||
BigDecimal::of(1)->dividedBy(2, -1);
|
||||
BigDecimal::of(1)->dividedByWithRounding(2, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -753,7 +753,7 @@ class BigDecimalTest extends AbstractTestCase
|
||||
$this->setExpectedException(RoundingNecessaryException::class);
|
||||
}
|
||||
|
||||
$actual = $number->dividedBy($divisor, $roundingMode, $scale);
|
||||
$actual = $number->dividedByWithRounding($divisor, $roundingMode, $scale);
|
||||
|
||||
if ($expected !== null) {
|
||||
$this->assertBigDecimalEquals($expected, $scale, $actual);
|
||||
|
@ -3,6 +3,8 @@
|
||||
namespace Brick\Math\Tests;
|
||||
|
||||
use Brick\Math\BigInteger;
|
||||
use Brick\Math\Exception\DivisionByZeroException;
|
||||
use Brick\Math\Exception\RoundingNecessaryException;
|
||||
|
||||
/**
|
||||
* Unit tests for class BigInteger.
|
||||
@ -523,19 +525,60 @@ class BigIntegerTest extends AbstractTestCase
|
||||
/**
|
||||
* @dataProvider providerDividedBy
|
||||
*
|
||||
* @param string $a The base number.
|
||||
* @param string $b The number to divide.
|
||||
* @param string $r The expected result.
|
||||
* @param string $number The base number.
|
||||
* @param string $divisor The divisor.
|
||||
* @param string $expected The expected result, or a class name if an exception is expected.
|
||||
*/
|
||||
public function testDividedBy($a, $b, $r)
|
||||
public function testDividedBy($number, $divisor, $expected)
|
||||
{
|
||||
$this->assertBigIntegerEquals($r, BigInteger::of($a)->dividedBy($b));
|
||||
$number = BigInteger::of($number);
|
||||
|
||||
if ($this->isException($expected)) {
|
||||
$this->setExpectedException($expected);
|
||||
}
|
||||
|
||||
$actual = $number->dividedBy($divisor);
|
||||
|
||||
if (! $this->isException($expected)) {
|
||||
$this->assertInstanceOf(BigInteger::class, $actual);
|
||||
$this->assertSame($expected, (string) $actual);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function providerDividedBy()
|
||||
{
|
||||
return [
|
||||
['123456789098765432101234567890987654321', 1, '123456789098765432101234567890987654321'],
|
||||
['123456789098765432101234567890987654321', 2, RoundingNecessaryException::class],
|
||||
['123456789098765432101234567890987654321', 0, DivisionByZeroException::class],
|
||||
['123456789098765432101234567890987654321', 0.0, DivisionByZeroException::class],
|
||||
['123456789098765432101234567890987654321', 0.1, '1234567890987654321012345678909876543210'],
|
||||
['123456789098765432101234567890987654322', '2', '61728394549382716050617283945493827161'],
|
||||
['61728394549382716050617283945493827161', '0.5', '123456789098765432101234567890987654322'],
|
||||
['61728394549382716050617283945493827161', '0.2', '308641972746913580253086419727469135805'],
|
||||
['61728394549382716050617283945493827161', '1/2', '123456789098765432101234567890987654322'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerQuotient
|
||||
*
|
||||
* @param string $a The base number.
|
||||
* @param string $b The divisor.
|
||||
* @param string $r The expected result.
|
||||
*/
|
||||
public function testQuotient($a, $b, $r)
|
||||
{
|
||||
$this->assertBigIntegerEquals($r, BigInteger::of($a)->quotient($b));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function providerQuotient()
|
||||
{
|
||||
return [
|
||||
['123456789098765432101234567890987654321', '1', '123456789098765432101234567890987654321'],
|
||||
@ -552,22 +595,22 @@ class BigIntegerTest extends AbstractTestCase
|
||||
/**
|
||||
* @expectedException \Brick\Math\Exception\DivisionByZeroException
|
||||
*/
|
||||
public function testDividedByZeroThrowsException()
|
||||
public function testQuotientOfZeroThrowsException()
|
||||
{
|
||||
BigInteger::of(1)->dividedBy(0);
|
||||
BigInteger::of(1)->quotient(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerDivideAndRemainder
|
||||
* @dataProvider providerQuotientAndRemainder
|
||||
*
|
||||
* @param string $dividend The dividend.
|
||||
* @param string $divisor The divisor.
|
||||
* @param string $quotient The expected quotient.
|
||||
* @param string $remainder The expected remainder.
|
||||
*/
|
||||
public function testDivideAndRemainder($dividend, $divisor, $quotient, $remainder)
|
||||
public function testQuotientAndRemainder($dividend, $divisor, $quotient, $remainder)
|
||||
{
|
||||
list ($q, $r) = BigInteger::of($dividend)->divideAndRemainder($divisor);
|
||||
list ($q, $r) = BigInteger::of($dividend)->quotientAndRemainder($divisor);
|
||||
|
||||
$this->assertSame($quotient, (string) $q);
|
||||
$this->assertSame($remainder, (string) $r);
|
||||
@ -576,7 +619,7 @@ class BigIntegerTest extends AbstractTestCase
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function providerDivideAndRemainder()
|
||||
public function providerQuotientAndRemainder()
|
||||
{
|
||||
return [
|
||||
['1', '123', '0', '1'],
|
||||
@ -630,13 +673,13 @@ class BigIntegerTest extends AbstractTestCase
|
||||
/**
|
||||
* @expectedException \Brick\Math\Exception\DivisionByZeroException
|
||||
*/
|
||||
public function testDivideAndRemainderByZeroThrowsException()
|
||||
public function testQuotientAndRemainderByZeroThrowsException()
|
||||
{
|
||||
BigInteger::of(1)->divideAndRemainder(0);
|
||||
BigInteger::of(1)->quotientAndRemainder(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerDivideAndRemainder
|
||||
* @dataProvider providerQuotientAndRemainder
|
||||
*
|
||||
* @param string $dividend The dividend.
|
||||
* @param string $divisor The divisor.
|
||||
@ -645,7 +688,7 @@ class BigIntegerTest extends AbstractTestCase
|
||||
*/
|
||||
public function testRemainder($dividend, $divisor, $quotient, $remainder)
|
||||
{
|
||||
list (, $r) = BigInteger::of($dividend)->divideAndRemainder($divisor);
|
||||
list (, $r) = BigInteger::of($dividend)->quotientAndRemainder($divisor);
|
||||
|
||||
$this->assertSame($remainder, (string) $r);
|
||||
}
|
||||
|
@ -307,6 +307,73 @@ class BigRationalTest extends AbstractTestCase
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerPower
|
||||
*
|
||||
* @param string $number The base number.
|
||||
* @param int $exponent The exponent to apply.
|
||||
* @param string $expected The expected result.
|
||||
*/
|
||||
public function testPower($number, $exponent, $expected)
|
||||
{
|
||||
$actual = BigRational::of($number)->power($exponent);
|
||||
|
||||
$this->assertInstanceOf(BigRational::class, $actual);
|
||||
$this->assertSame($expected, (string) $actual);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function providerPower()
|
||||
{
|
||||
return [
|
||||
['-3', 0, '1'],
|
||||
['-2/3', 0, '1'],
|
||||
['-1/2', 0, '1'],
|
||||
['0', 0, '1'],
|
||||
['1/3', 0, '1'],
|
||||
['2/3', 0, '1'],
|
||||
['3/2', 0, '1'],
|
||||
|
||||
['-3/2', 1, '-3/2'],
|
||||
['-2/3', 1, '-2/3'],
|
||||
['-1/3', 1, '-1/3'],
|
||||
['0', 1, '0'],
|
||||
['1/3', 1, '1/3'],
|
||||
['2/3', 1, '2/3'],
|
||||
['3/2', 1, '3/2'],
|
||||
|
||||
['-3/4', 2, '9/16'],
|
||||
['-2/3', 2, '4/9'],
|
||||
['-1/2', 2, '1/4'],
|
||||
['0', 2, '0'],
|
||||
['1/2', 2, '1/4'],
|
||||
['2/3', 2, '4/9'],
|
||||
['3/4', 2, '9/16'],
|
||||
|
||||
['-3/4', 3, '-27/64'],
|
||||
['-2/3', 3, '-8/27'],
|
||||
['-1/2', 3, '-1/8'],
|
||||
['0', 3, '0'],
|
||||
['1/2', 3, '1/8'],
|
||||
['2/3', 3, '8/27'],
|
||||
['3/4', 3, '27/64'],
|
||||
|
||||
['0', 1000000, '0'],
|
||||
['1', 1000000, '1'],
|
||||
|
||||
['-2/3', 99, '-633825300114114700748351602688/171792506910670443678820376588540424234035840667'],
|
||||
['-2/3', 100, '1267650600228229401496703205376/515377520732011331036461129765621272702107522001'],
|
||||
|
||||
['-123/33', 25, '-17685925284953355608333258649989090388842388168292443/91801229324973413645775482048441660193'],
|
||||
[ '123/33', 26, '2175368810049262739824990813948658117827613744699970489/3029440567724122650310590907598574786369'],
|
||||
|
||||
['-123456789/2', 8, '53965948844821664748141453212125737955899777414752273389058576481/256'],
|
||||
['9876543210/3', 7, '9167159269868350921847491739460569765344716959834325922131706410000000/2187']
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerReciprocal
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user