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

Proper comparison of every BigNumber against all rational number formats

This commit is contained in:
Benjamin Morel 2015-06-22 22:42:10 +02:00
parent 1ade407e23
commit 77610f5fdc
5 changed files with 39 additions and 6 deletions

View File

@ -541,10 +541,19 @@ final class BigDecimal extends BigNumber implements \Serializable
*/
public function compareTo($that)
{
$that = BigDecimal::of($that);
$this->scaleValues($this, $that, $a, $b);
$that = BigNumber::of($that);
return Calculator::get()->cmp($a, $b);
if ($that instanceof BigInteger) {
$that = $that->toBigDecimal();
}
if ($that instanceof BigDecimal) {
$this->scaleValues($this, $that, $a, $b);
return Calculator::get()->cmp($a, $b);
}
return - $that->compareTo($this);
}
/**

View File

@ -395,9 +395,13 @@ final class BigInteger extends BigNumber implements \Serializable
*/
public function compareTo($that)
{
$that = BigInteger::of($that);
$that = BigNumber::of($that);
return Calculator::get()->cmp($this->value, $that->value);
if ($that instanceof BigInteger) {
return Calculator::get()->cmp($this->value, $that->value);
}
return - $that->compareTo($this);
}
/**

View File

@ -1801,7 +1801,15 @@ class BigDecimalTest extends AbstractTestCase
['0', '-0.000000000000000000000000000000000000000000000000001', 1],
['0', '0.000000000000000000000000000000000000000000000000001', -1],
['0', '0.000000000000000000000000000000000000000000000000000', 0]
['0', '0.000000000000000000000000000000000000000000000000000', 0],
['123.9999999999999999999999999999999999999', 124, -1],
['124.0000000000000000000000000000000000000', '124', 0],
['124.0000000000000000000000000000000000001', 124.0, 1],
['123.9999999999999999999999999999999999999', '1508517100733469660019804/12165460489786045645321', -1],
['124.0000000000000000000000000000000000000', '1508517100733469660019804/12165460489786045645321', 0],
['124.0000000000000000000000000000000000001', '1508517100733469660019804/12165460489786045645321', 1],
];
}

View File

@ -970,6 +970,14 @@ class BigIntegerTest extends AbstractTestCase
[ '11111111111111111111111111111111111111111111', '-9999999999999999999999999', 1],
['-11111111111111111111111111111111111111111111', '9999999999999999999999999', -1],
['-11111111111111111111111111111111111111111111','-9999999999999999999999999', -1],
['123', '123.000000000000000000000000000000000000000000000000000000001', -1],
['123', '123.000000000000000000000000000000000000000000000000000000000', 0],
['123', '122.999999999999999999999999999999999999999999999999999999999', 1],
['123', '246/2', 0],
['123', '245/2', 1],
['123', '247/2', -1],
];
}

View File

@ -529,6 +529,10 @@ class BigRationalTest extends AbstractTestCase
['1', '999999999999999999999999999999/1000000000000000000000000000000', 1],
['999999999999999999999999999999/1000000000000000000000000000000', '999/1000', 1],
['-999999999999999999999999999999/1000000000000000000000000000000', '-999/1000', -1],
['-999999999999999999999999999999/1000000000000000000000000000000', -1, 1],
['-999999999999999999999999999999/1000000000000000000000000000000', '-10e-1', 1],
['-999999999999999999999999999999/1000000000000000000000000000000', '-0.999999999999999999999999999999', 0],
['-999999999999999999999999999999/1000000000000000000000000000000', '-0.999999999999999999999999999998', -1],
];
}