Core\Decimal::power
Core\Decimal::power(decimal $base, uint $exponent): decimalCore\Decimal::power( decimal $base, uint $exponent, ): decimal
Multiplies a number by itself as many times as you say, and keeps every digit of the answer.
Core\Decimal::power takes the base and the number of times to multiply. Core\Decimal::power(2, 10) is 1024, and an exponent of 0 gives 1. The exponent is a whole number and is never negative. The answer is exact: 2.50 squared is 6.2500, because the digits after the point of both numbers are added together. When the result does not fit in a decimal, the call throws an ArithmeticError.
Use it where the value is money or a measurement, and a rounded answer would be wrong. Examples are interest over a number of years, a growth factor applied many times, and a unit converted by a scale factor. The ** operator does not work with a decimal base, so use this method to raise a decimal.
Good to know: when the base has digits after the point, the result gets long quickly. Core\Decimal::power(1.05, 14) already has 28 digits after the point. That is the most a decimal can store, so an exponent of 15 throws. To go further, round the result with Core\Decimal::round and raise that.
Parameters
$basedecimalThe value to raise.$exponentuintHow many times to multiply it by itself. Zero gives1. A negative exponent is not allowed. To get$bto the power-$n, divide 1 by the power:Core\Decimal::divideRounded(1, Core\Decimal::power($b, $n), $scale, $mode).
Return value
Returns decimal. The exact power. The digits after the point add up with each multiplication, so 2.50 squared is 6.2500.
Errors
ArithmeticErrorWhen the power does not fit in adecimal. That happens with more than 28 digits after the point, or more than about 28 digits in total.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Decimal::divideExact, divideRounded and power write their words in full. The old names were divExact, divRound and pow. |
Examples
Raise a number to a power
<?nvs// Shows how to multiply a number by itself. The second argument is how many// times.
// This prints 1024.echo Core\Decimal::power(2, 10), "\n";
// The answer keeps every digit. 2.50 has two digits after the point, so the// square has four. This prints 6.2500.echo Core\Decimal::power(2.50, 2), "\n";
// An exponent of 0 gives 1, whatever the base is. This prints 1.echo Core\Decimal::power(7.25, 0), "\n";10246.25001A power that is too wide
<?nvs// Shows the point where a power stops fitting. A `decimal` holds 28 digits// after the point, and every multiplication of 1.05 adds two more.
array<uint> $years = [10, 14, 15];
foreach ($years as uint $count) { try { decimal $grown = Core\Decimal::power(1.05, $count); echo $count, ': ', $grown, "\n"; } catch (ArithmeticError $tooWide) { // 15 multiplications need 30 digits after the point, so this one // prints the error instead of a number. echo $count, ': ', $tooWide->message, "\n"; }}10: 1.6288946267774414062514: 1.979931599439397388305664062515: Core\Decimal::power cannot answer 1.05 to the power 15: a `decimal` holds 28 fractional digits and a 96-bit mantissaWhat savings grow to
<?nvs// Shows what an amount grows to when interest is added every year. The growth// factor is raised to the number of years, and the balance is rounded to// cents at the end.
decimal $deposit = 2500.00;decimal $factor = 1.05;array<uint> $years = [2, 5, 10];
foreach ($years as uint $count) { decimal $grown = $deposit * Core\Decimal::power($factor, $count); // Round once, at the end. Rounding every year would move the answer. echo $count, ' years: ', Core\Decimal::divideRounded($grown, 1, 2, Core\RoundMode::HalfUp), "\n";}2 years: 2756.255 years: 3190.7010 years: 4072.24