Core\BigInt::sign
$bigInt->sign(): intTells you whether a Core\BigInt is below zero, at zero, or above zero.
sign returns -1 when the number is below zero, 0 when it is exactly zero, and 1 when it is above zero. The answer is an int, so you can compare it and use it in an if straight away.
This is the same answer Core\BigInt::compareTo gives against zero. sign is the shorter way to write it, because you need no second number.
A number far too large for an int has a sign like any other number. Reading the sign builds nothing and looks at no digits, so it costs the same for a number of one digit and for a number of a hundred thousand digits.
Return value
Returns int. -1 if this value is below zero, 0 if it is zero, 1 if it is above zero.
Examples
The three answers of sign
<?nvs// Shows the three answers `sign` gives.
// `sign` returns -1 below zero, 0 at zero, and 1 above zero. This prints -1,// 0 and 1.echo Core\BigInt::of(-42)->sign(), "\n";echo Core\BigInt::of(0)->sign(), "\n";echo Core\BigInt::of(42)->sign(), "\n";
// A number far larger than an `int` holds has a sign like any other. This// prints 1 and -1.echo Core\BigInt::of(1)->shiftedLeft(200)->sign(), "\n";echo Core\BigInt::of(-1)->shiftedLeft(200)->sign(), "\n";-1011-1Using the sign in a condition
<?nvs// Shows how to use `sign` in a condition. The answer is an `int`, so you can// compare it directly.
array<Core\BigInt> $balances = [ Core\BigInt::parse('-250000000000000000000'), Core\BigInt::of(0), Core\BigInt::parse('120000000000000000000'),];
// This prints "in debt", "empty" and "in credit".foreach ($balances as Core\BigInt $balance) { int $where = $balance->sign(); if ($where < 0) { echo "in debt\n"; } elseif ($where == 0) { echo "empty\n"; } else { echo "in credit\n"; }}in debtemptyin creditRefusing a transfer that would overdraw
<?nvs// Shows a program checking that a transfer does not leave an account in debt.// The amounts are counted in the smallest unit of a token, so they are too// large for an `int`.
class Account{ // Returns true when $balance still covers $amount. public static function covers(Core\BigInt $balance, Core\BigInt $amount): bool { return $balance->minus($amount)->sign() >= 0; }}
var $balance = Core\BigInt::parse('2500000000000000000000');var $small = Core\BigInt::parse('1000000000000000000');var $large = Core\BigInt::parse('9000000000000000000000');
// The small transfer leaves 2499 tokens, so it is allowed. This prints// "allowed".echo Account::covers($balance, $small) ? "allowed\n" : "refused\n";
// The large transfer would leave the account in debt. This prints "refused".echo Account::covers($balance, $large) ? "allowed\n" : "refused\n";
// Paying out the whole balance leaves exactly zero, which is allowed. This// prints "allowed".echo Account::covers($balance, $balance) ? "allowed\n" : "refused\n";allowedrefusedallowed