Skip to content

Core\BigInt::raisedTo

$bigInt->raisedTo(uint $exponent): Core\BigInt$bigInt->raisedTo(
    uint $exponent,
): Core\BigInt

Raises a Core\BigInt to a power.

raisedTo multiplies the number by itself as many times as the power says. The power is a whole number and is never negative. The power 0 gives 1 for every number, and the power 1 gives the number back. A negative number raised to an even power is positive.

The answer grows very quickly: 2 to the power 1000 already has 302 digits. One call may produce a number of at most 1048576 bits, which is about 315653 digits in base 10. A larger answer throws an ArithmeticError. raisedTo tests the size before it multiplies anything, so the number is never built and no memory is used for it.

To raise a number to a power and then take the remainder, use Core\BigInt::raisedToModulo. It is much faster for a large power, and it never builds the whole power.

Parameters

  • $exponentuintThe power to raise this value to. If it is 0, the result is 1 for every value.

Return value

Returns Core\BigInt. A BigInt equal to the power.

Errors

  • ArithmeticErrorThe result would be wider than 1 048 576 bits. The method throws this error before it starts to compute.

Changelog

VersionChange
0.0.6Each 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

Raising a number to a power

<?nvs
// Shows how to raise a whole number to a power.
// `raisedTo` raises the number to the power you give it. This prints 1024.
echo Core\BigInt::of(2)->raisedTo(10), "\n";
// The answer may be far larger than an `int` can store. This prints
// 18446744073709551616.
echo Core\BigInt::of(2)->raisedTo(64), "\n";
// The power 0 gives 1 for every number, and the power 1 gives the number
// back. This prints 1 and 7.
echo Core\BigInt::of(7)->raisedTo(0), "\n";
echo Core\BigInt::of(7)->raisedTo(1), "\n";
// A negative number raised to an even power is positive, and to an odd power
// is negative. This prints 9 and -27.
echo Core\BigInt::of(-3)->raisedTo(2), "\n";
echo Core\BigInt::of(-3)->raisedTo(3), "\n";
Output
1024
18446744073709551616
1
7
9
-27

How large a power may be

<?nvs
// Shows how large an answer `raisedTo` builds, and what it does when the answer
// would be too large for one call.
// The answer grows quickly with the power. 2 to the power 1000 is a number of
// 302 digits, and this prints 302.
echo Core\Str::length(Core\BigInt::of(2)->raisedTo(1000)->toString()), "\n";
// 2 to the power 500000 is a number of 150515 digits, and `raisedTo` builds it.
// This prints 150515.
echo Core\Str::length(Core\BigInt::of(2)->raisedTo(500000)->toString()), "\n";
// One call may produce at most 1048576 bits. A larger answer throws an
// `ArithmeticError`, and the number is never built.
try {
Core\BigInt::of(2)->raisedTo(600000);
} catch (ArithmeticError $tooWide) {
echo $tooWide->message, "\n";
}
Output
302
150515
Core\BigInt::raisedTo(): the answer would be 1200000 bits wide, past the 1048576 one call may produce

An amount in the smallest unit of a ledger

<?nvs
// Shows a program writing an amount into a ledger that counts in very small
// units. A token ledger stores 1 token as 10 to the power 18.
class Ledger
{
// Returns $whole tokens as the smallest unit the ledger counts in.
public static function smallestUnits(int $whole, uint $decimals): Core\BigInt
{
return Core\BigInt::of($whole)->multipliedBy(Core\BigInt::of(10)->raisedTo($decimals));
}
}
// 2500 tokens with 18 decimals is a number of 22 digits, which no `int` holds.
// This prints 2500000000000000000000.
echo Ledger::smallestUnits(2500, 18), "\n";
// A ledger with 6 decimals stores the same amount as a smaller number. This
// prints 2500000000.
echo Ledger::smallestUnits(2500, 6), "\n";
Output
2500000000000000000000
2500000000