Skip to content

Core\BigInt::abs

$bigInt->abs(): Core\BigInt

Returns a number without its sign, as a new Core\BigInt.

A negative number becomes positive. A positive number stays the same, and zero stays zero. The number you called abs on does not change, because every Core\BigInt method returns a new number.

Use it when you want to know how far apart two numbers are. Subtract one from the other, then call abs on the result. The order you subtract in then makes no difference to the answer.

Good to know: this works for every number, however large. Core\Math::abs on a plain int throws an error for the smallest int, because the positive value is too large for an int. A Core\BigInt has no such limit.

Return value

Returns Core\BigInt. A non-negative BigInt.

Examples

A number without its sign

<?nvs
// Shows what `abs` returns for a negative number, a positive number and zero.
var $below = Core\BigInt::of(-273);
// `abs` removes the sign. This prints 273.
echo $below->abs(), "\n";
// A positive number stays the same. This prints 273.
echo Core\BigInt::of(273)->abs(), "\n";
// Zero stays zero. This prints 0.
echo Core\BigInt::of(0)->abs(), "\n";
// `abs` returns a new number, so the one you called it on still has its sign.
// This prints -273.
echo $below, "\n";
Output
273
273
0
-273

How far apart two numbers are

<?nvs
// Shows how to measure the distance between two numbers. You subtract one from
// the other and call `abs` on the result.
var $first = Core\BigInt::parse('90000000000000000000');
var $second = Core\BigInt::parse('90000000000000000007');
// The difference carries a sign, so this prints -7.
echo $first->minus($second), "\n";
// `abs` removes it, and what is left is the distance. This prints 7.
echo $first->minus($second)->abs(), "\n";
// Subtracting the other way round gives the same distance. This prints 7.
echo $second->minus($first)->abs(), "\n";
Output
-7
7
7

Checking two totals agree

<?nvs
// Shows a check a program runs at the end of a day. Two systems report the same
// total in the smallest unit of a currency, and they may differ by a little.
class Ledger
{
// Returns true when the two totals are no further apart than $allowed.
public static function agrees(Core\BigInt $ours, Core\BigInt $theirs, Core\BigInt $allowed): bool
{
var $distance = $ours->minus($theirs)->abs();
return $distance->compareTo($allowed) <= 0;
}
}
var $allowed = Core\BigInt::of(100);
var $ours = Core\BigInt::parse('184467440737095516150');
// The other system reports 40 units less. That is inside what is allowed, so
// this prints "in agreement".
var $theirs = $ours->minus(Core\BigInt::of(40));
if (Ledger::agrees($ours, $theirs, $allowed)) {
echo "in agreement\n";
} else {
echo "too far apart\n";
}
// This time it reports 4000 units more. `abs` gives the same distance whichever
// system is the larger, so this prints "too far apart".
var $later = $ours->plus(Core\BigInt::of(4000));
if (Ledger::agrees($ours, $later, $allowed)) {
echo "in agreement\n";
} else {
echo "too far apart\n";
}
Output
in agreement
too far apart