Core\BigInt::compareTo
$bigInt->compareTo(Core\BigInt $other): int$bigInt->compareTo( Core\BigInt $other, ): int
Compares this number with another one and returns -1, 0 or 1.
The result is -1 when this number is the smaller of the two, 0 when they are equal, and 1 when this number is the larger. Numbers of any size are compared exactly.
You can also write $a < $b, $a > $b, $a <= $b, $a >= $b and $a <=> $b on two Core\BigInt values, and Core\Arr::sort puts a list of them in order for you. All of those use compareTo, so they always agree with it. Reach for compareTo itself when you want the three-way answer.
Good to know: a negative number is always the smaller one, however many digits it has.
Parameters
$otherCore\BigIntThe value to compare with.
Return value
Returns int. -1 when this value is the smaller, 0 when they are equal, 1 when it is larger.
Examples
Which number is larger
<?nvs// Shows the three answers `compareTo` gives.
var $smaller = Core\BigInt::parse('99999999999999999999');var $larger = Core\BigInt::parse('100000000000000000000');
// The number you call it on is the smaller one, so this prints -1.echo $smaller->compareTo($larger), "\n";
// The other way round, so this prints 1.echo $larger->compareTo($smaller), "\n";
// The two numbers are equal, so this prints 0.echo $smaller->compareTo(Core\BigInt::parse('99999999999999999999')), "\n";
// A negative number is the smaller one however many digits it has. This// prints -1.echo $larger->negated()->compareTo(Core\BigInt::of(0)), "\n";-110-1The comparison operators
<?nvs// Shows that the comparison operators work on two `Core\BigInt` values, and// that they give the same answer as `compareTo`.
var $left = Core\BigInt::parse('18446744073709551616');var $right = Core\BigInt::parse('18446744073709551617');
// `<=>` gives the same three-way answer as the method. This prints -1 -1.echo $left <=> $right, ' ', $left->compareTo($right), "\n";
// `<` is true when the answer is below zero. This prints yes.if ($left < $right) { echo "yes\n";} else { echo "no\n";}
// `>=` is true when the answer is zero or above. This prints yes.if ($right >= $left) { echo "yes\n";} else { echo "no\n";}-1 -1yesyesThe largest amount in a list
<?nvs// Shows a job a program does with a list of amounts that are too large for an// `int`: put them in order, and pick out the largest one.
array<string> $reported = [ '9223372036854775808', '170141183460469231731687303715884105728', '18446744073709551616',];
array<Core\BigInt> $amounts = [];foreach ($reported as string $text) { $amounts[] = Core\BigInt::parse($text);}
// `Core\Arr::sort` puts the amounts in order through `compareTo`.// The smallest comes first, so this prints 9223372036854775808.array<Core\BigInt> $ordered = Core\Arr::sort($amounts);echo $ordered[0], "\n";
// The largest one found without sorting the whole list.// This prints 170141183460469231731687303715884105728.var $largest = $amounts[0];foreach ($amounts as Core\BigInt $amount) { if ($amount->compareTo($largest) > 0) { $largest = $amount; }}echo $largest, "\n";9223372036854775808170141183460469231731687303715884105728