Reference · Math

pow

signature
pow(num | dec, num) -> num | dec

What it does

x to the power y. With two ints and a y of zero or more, the answer is an int, exact: past 64 bits it is an overflow, as * gives, which ?? can catch. A float on either side, or a negative y, gives a float. pow(2, 10) is 1024, pow(2, 0.5) is 1.4142135623730951, pow(2, -1) is 0.5. A decimal to an int's power is a decimal, worked out by squaring, so interest and loan payments need no floats: pow(1m + 0.05m / 12, 360) is 4.4677443140061322124280701433m.

Examples

print(pow(2, 10), pow(2, 0.5), pow(2.0, 10), pow(2, -1))print(pow(10, 0), pow(-2, 3), pow(1.5, 2))
output
1024 1.4142135623730951 1024.0 0.51 -8 2.25

Edge cases

print(pow(2, 62), pow(-2, 63), pow(2.0, 63))print(pow(2, 63) ?? "too big", pow(10, 30) ?? "too big")print(pow(-1, 9007199254740993), pow(0, 4294967296), pow(1, 4294967296), pow(2, 4294967296) ?? "too big")print(pow(0, 0), pow(0, -1), pow(2.0, 1024))print(pow(-8, 1.0 / 3.0), pow(-1, 0.5))print(pow(2m, 2), pow(1.5m, 3), pow(2m, -2), pow(3m, -1), pow(0m, 0))print(pow(1m + 0.05m / 12, 360), type_of(pow(1.1m, 2)), pow(10m, 29) ?? "too big")
output
4611686018427387904 -9223372036854775808 9223372036854776000too big too big-1 0 1 too big1 inf infNaN NaN4 3.375 0.25 0.3333333333333333333333333333 14.4677443140061322124280701433 dec too big

Errors

A string, null or a boolean as either argument is a runtime error, not an error value. A decimal takes only an int's power: a float or a decimal as the power is a runtime error, and float(x) with math.pow is the way to a fraction of a power. A decimal power of an int or a float is a runtime error too, and so is a missing exponent. A decimal answer too large stops with decimal overflow unless ?? catches it.

this one fails
print(pow(2, 64))
output
line 1: integer overflow
this one fails
print(pow("2", 2))
output
line 1: `pow` expects a number as argument 1, got str
this one fails
print(pow(2m, 0.5))
output
line 1: `pow` takes a dec to an int's power, got float as the power: for a fraction of a power, float(x) and math.pow
this one fails
print(pow(10m, 29))
output
line 1: decimal overflow

Chaining

In a pipeline the base comes first, so x | pow(y) is pow(x, y):

print(2 | pow(8), [2, 4] | map((x) => pow(x, 2)) | sum)print(pow(9, 0.5) == sqrt(9))
output
256 20true

See also

sqrt, math.pow, abs, round

Thread
8Useful? Like it, or add to its thread below.

Thread 0 posts

On pow(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo