Skip to content

Commit

Permalink
Add BigInteger::remainder() method
Browse files Browse the repository at this point in the history
This allows to return the remainder of the division only.
  • Loading branch information
BenMorel committed Jun 11, 2015
1 parent 2c35d6f commit 1f577af
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/BigInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,22 @@ public function divideAndRemainder($that)
return [$quotient, $remainder];
}

/**
* 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.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/BigIntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,21 @@ public function testDivideAndRemainderByZeroThrowsException()
BigInteger::of(1)->divideAndRemainder(0);
}

/**
* @dataProvider providerDivideAndRemainder
*
* @param string $dividend The dividend.
* @param string $divisor The divisor.
* @param string $quotient The expected quotient (ignore for this test).
* @param string $remainder The expected remainder.
*/
public function testRemainder($dividend, $divisor, $quotient, $remainder)
{
list (, $r) = BigInteger::of($dividend)->divideAndRemainder($divisor);

$this->assertSame($remainder, (string) $r);
}

/**
* @dataProvider providerPower
*
Expand Down

0 comments on commit 1f577af

Please sign in to comment.