mirror of
https://github.com/danog/math.git
synced 2024-11-30 04:19:31 +01:00
Added sqrt function and tests for it. Also added two() function to return a BigInteger of value 2.
This commit is contained in:
parent
0fc7723baf
commit
e2a4969440
@ -112,6 +112,22 @@ final class BigDecimal extends BigNumber implements \Serializable
|
||||
return $one;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a BigDecimal representing two, with a scale of zero.
|
||||
*
|
||||
* @return BigDecimal
|
||||
*/
|
||||
public static function two()
|
||||
{
|
||||
static $two = null;
|
||||
|
||||
if ($two === null) {
|
||||
$two = new BigDecimal('2');
|
||||
}
|
||||
|
||||
return $two;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a BigDecimal representing ten, with a scale of zero.
|
||||
*
|
||||
@ -413,6 +429,32 @@ final class BigDecimal extends BigNumber implements \Serializable
|
||||
|
||||
return [$quotient, $remainder];
|
||||
}
|
||||
/**
|
||||
* Returns the square root of this number.
|
||||
*
|
||||
* The quotient has a scale of `0`, and the remainder has a scale of `0`.
|
||||
*
|
||||
* @return BigDecimal With the square root of this number.
|
||||
*
|
||||
* @throws IsNegativeException If this number is negative.
|
||||
*/
|
||||
public function sqrt()
|
||||
{
|
||||
if ($this->isNegative()) {
|
||||
throw IsNegativeException::isNegative($this);
|
||||
}
|
||||
$two = BigDecimal::two();
|
||||
$guess = $this->quotient($two);
|
||||
while (true) {
|
||||
$last = $guess;
|
||||
$guess = $this->quotient($guess)->add($guess)->quotient($two);
|
||||
// (($n / $guess) + $guess) / 2;
|
||||
if($last->compareTo($guess) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $guess;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a BigDecimal with the current value and the specified scale.
|
||||
|
23
src/Exception/IsNegativeException.php
Normal file
23
src/Exception/IsNegativeException.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Brick\Math\Exception;
|
||||
|
||||
use Brick\Math\BigInteger;
|
||||
|
||||
/**
|
||||
* Exception thrown when a is negative, but should be positive..
|
||||
*/
|
||||
class IsNegativeException extends \RuntimeException
|
||||
{
|
||||
/**
|
||||
* @param BigInteger $value
|
||||
*
|
||||
* @return IsNegativeException
|
||||
*/
|
||||
public static function isNegative(BigInteger $value)
|
||||
{
|
||||
$message = '%s is negative, but should be positive.';
|
||||
|
||||
return new self(sprintf($message, (string) $value));
|
||||
}
|
||||
}
|
@ -1360,6 +1360,8 @@ class BigDecimalTest extends AbstractTestCase
|
||||
BigDecimal::of(1.2)->quotient(0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @expectedException \Brick\Math\Exception\DivisionByZeroException
|
||||
*/
|
||||
@ -1368,12 +1370,9 @@ class BigDecimalTest extends AbstractTestCase
|
||||
BigDecimal::of(1.2)->remainder(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Brick\Math\Exception\DivisionByZeroException
|
||||
*/
|
||||
public function testQuotientAndRemainderOfZeroThrowsException()
|
||||
public function testsqrt()
|
||||
{
|
||||
BigDecimal::of(1.2)->quotientAndRemainder(0);
|
||||
$this->assertSame(BigDecimal::of(625)->sqrt(), BigDecimal::of(25));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user