Core\BigInt::minus
$bigInt->minus(Core\BigInt $other): Core\BigInt$bigInt->minus( Core\BigInt $other, ): Core\BigInt
Returns the result of taking another number away from this one, as a new Core\BigInt.
The difference is always exact, however large the numbers are. Two numbers that each fit in an int can have a difference that does not. Use Core\BigInt when a value may grow that large.
The result can be negative. When $other is the larger of the two numbers, the result is negative.
minus does not change either number. It returns the result as a new number, so keep that result in a variable.
Good to know: $a - $b does not work on two Core\BigInt values, because - is only for plain numbers. minus is how you write it. $a->minus($b) and $a->plus($b->negated()) give the same result.
Parameters
$otherCore\BigIntThe value to subtract.
Return value
Returns Core\BigInt. A BigInt equal to the difference.
Changelog
| Version | Change |
|---|---|
0.0.6 | Each Core\BigInt method that computes a new number is named for its result: plus, minus, multipliedBy, dividedBy, remainder, raisedTo, raisedToModulo, negated, shiftedLeft and shiftedRight. The old names were add, sub, mul, div, mod, pow, powMod, neg, shl and shr. |
Examples
Taking one number away from another
<?nvs// Shows how to take one number away from another. `minus` returns a new number// and leaves the one you called it on as it was.
var $stock = Core\BigInt::of(1000);var $sold = Core\BigInt::of(325);
// This prints 675.echo $stock->minus($sold), "\n";
// `$stock` still has the same value. This prints 1000.echo $stock, "\n";
// When the second number is the larger one, the result is negative. This// prints -675.echo $sold->minus($stock), "\n";6751000-675A difference no int can hold
<?nvs// Shows a difference that no `int` can hold. Each of the two numbers below// fits in an `int`. What is between them does not.
var $highest = Core\BigInt::parse('9223372036854775807');var $lowest = Core\BigInt::parse('-9223372036854775808');
// The distance between the two ends of the `int` range. This prints// 18446744073709551615.echo $highest->minus($lowest), "\n";
// The other way round the result is just as large, with a minus sign. This// prints -18446744073709551615.echo $lowest->minus($highest), "\n";
// The result is exact at every size. This prints// 340282366920938463463374607431768211456.var $huge = Core\BigInt::parse('170141183460469231731687303715884105728');echo $huge->minus($huge->negated()), "\n";18446744073709551615-18446744073709551615340282366920938463463374607431768211456The bytes sent between two readings
<?nvs// Shows how far a counter moved between two readings. A network interface// counts every byte it has sent since the machine started. That number grows// past what an `int` holds, so it arrives as text.
array<string> $readings = ['18446744073712000000', '18446744073800000000'];
var $previous = Core\BigInt::parse('18446744073709551615');var $current = $previous;foreach ($readings as string $reading) { $current = Core\BigInt::parse($reading); // The bytes sent since the reading before is the new number minus the old // one. This prints 2448385 and then 88000000. echo $current->minus($previous), "\n"; $previous = $current;}244838588000000