Core\BigInt::multipliedBy
$bigInt->multipliedBy(Core\BigInt $other): Core\BigInt$bigInt->multipliedBy( Core\BigInt $other, ): Core\BigInt
Returns the product of this number and another one, as a new Core\BigInt.
The product is always exact, however large the numbers are. When the product of two int values is too large for an int, the multiplication throws an error. Use Core\BigInt when a product may grow that far.
The sign follows the usual rule: two numbers with the same sign give a positive product, and two with different signs give a negative one.
multipliedBy 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. multipliedBy is how you write it.
Parameters
$otherCore\BigIntThe value to multiply by.
Return value
Returns Core\BigInt. A BigInt equal to the product.
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
Multiplying two numbers
<?nvs// Shows how to multiply two large numbers, and which sign the product has.
var $left = Core\BigInt::parse('12345678901234567890');var $right = Core\BigInt::parse('98765432109876543210');
// Both numbers are positive, so the product is positive.echo $left->multipliedBy($right), "\n";
// One number is negative, so the product is negative.echo $left->negated()->multipliedBy($right), "\n";1219326311370217952237463801111263526900-1219326311370217952237463801111263526900A running product
<?nvs// Shows a running product: the factorial of 25, which is the number of orders// 25 items can be put in. It is far larger than an `int` can store.
var $product = Core\BigInt::of(1);int $n = 2;while ($n <= 25) { // `multipliedBy` returns the new product, so the result is kept in `$product`. $product = $product->multipliedBy(Core\BigInt::of($n)); $n = $n + 1;}
// This prints 15511210043330985984000000.echo $product, "\n";15511210043330985984000000The total of an order
<?nvs// Shows the total price of an order that is counted in the smallest unit of a// currency. One unit here is a millionth of a cent.
var $perItem = Core\BigInt::parse('2500000000000000000');uint $quantity = 1200;
// `multipliedBy` gives the total price of 1200 items. This prints// 3000000000000000000000.echo $perItem->multipliedBy(Core\BigInt::ofUint($quantity)), "\n";3000000000000000000000