Core\BigInt::negated
$bigInt->negated(): Core\BigIntReturns this number with its sign flipped, as a new Core\BigInt.
A positive number becomes negative, and a negative number becomes positive. Zero stays zero. The number you called negated on does not change, because every Core\BigInt method returns a new number.
Core\BigInt has no operators, so negated is how you write what -$value writes for a plain int. Calling it twice gives the number you started with. Adding a number's negated gives the same result as subtracting that number, which is useful when a program keeps one list of amounts and books some of them in the other direction.
Good to know: this works for every number, however large. Flipping the sign of the smallest int throws an error, because the positive value is too large for an int. A Core\BigInt has no such limit.
Return value
Returns Core\BigInt. A BigInt with the same absolute value and the opposite sign. For zero, the result is zero.
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
Flipping the sign
<?nvs// Shows what `negated` returns for a positive number, a negative number and zero.
var $owed = Core\BigInt::of(4500);
// `negated` flips the sign. This prints -4500.echo $owed->negated(), "\n";
// A negative number becomes positive. This prints 4500.echo Core\BigInt::of(-4500)->negated(), "\n";
// Zero stays zero. This prints 0.echo Core\BigInt::of(0)->negated(), "\n";
// `negated` returns a new number, so the one you called it on still has its own// sign. This prints 4500.echo $owed, "\n";-4500450004500Adding instead of subtracting
<?nvs// Shows two things you get from `negated`. Adding a flipped number is the same as// subtracting it, and flipping the sign twice gives the number you started// with.
var $balance = Core\BigInt::parse('184467440737095516150');var $fee = Core\BigInt::of(250);
// Both lines print 184467440737095515900.echo $balance->minus($fee), "\n";echo $balance->plus($fee->negated()), "\n";
// Flipping the sign twice gives the number you started with. This prints// 184467440737095516150.echo $balance->negated()->negated(), "\n";184467440737095515900184467440737095515900184467440737095516150Cancelling a ledger entry
<?nvs// Shows how a program cancels an entry in a ledger. The correction is the// amount with its sign flipped, so the entry and its correction add up to zero.
class Ledger{ // Returns the entry that cancels $amount. public static function reversalOf(Core\BigInt $amount): Core\BigInt { return $amount->negated(); }}
array<Core\BigInt> $entries = [ Core\BigInt::parse('120000000000000000000'), Core\BigInt::of(-4500),];
var $total = Core\BigInt::of(0);foreach ($entries as Core\BigInt $entry) { $total = $total->plus($entry)->plus(Ledger::reversalOf($entry));}
// Every entry was booked and then cancelled, so this prints 0.echo $total, "\n";
// A correction keeps the size of the entry and changes its direction. This// prints -120000000000000000000.echo Ledger::reversalOf($entries[0]), "\n";0-120000000000000000000