Math.pow
func Math.pow(x: float64, y: float64): float64Math.pow returns x raised to the power y.
Details
- Ant has no power operator:
Math.powis the way to raise a number to a power. Math.pownever panics, and a negativexwith a fractionalygivesNaN.int(...)converts a whole result back and panics on a fraction or a value out of range; Math.toInt returnsnullinstead.
Basic examples
Math.pow(2.0, 10.0) // 1024.0
Math.pow(3.0, 2.0) // 9.0
Math.pow(1.5, 2.0) // 2.25Scope
A negative exponent gives the reciprocal, and a fractional one gives a root:
Math.pow(2.0, -1.0) // 0.5
Math.pow(10.0, -3.0) // 0.001
Math.pow(9.0, 0.5) // 3.0
Math.pow(100.0, -0.5) // 0.1A negative base works with a whole exponent, and the sign follows its parity. With a fractional exponent the result is NaN:
Math.pow(-2.0, 3.0) // -8.0
Math.pow(-2.0, 2.0) // 4.0
Math.pow(-8.0, 0.5) // NaNZero, NaN, and the infinities follow IEEE 754:
Math.pow(0.0, 0.0) // 1.0
Math.pow(Math.NaN, 0.0) // 1.0
Math.pow(1.0, Math.NaN) // 1.0
Math.pow(Math.NaN, 1.0) // NaN
Math.pow(0.0, -1.0) // Infinity
Math.pow(-0.0, -1.0) // -Infinity
Math.pow(2.0, Math.Infinity) // Infinity
Math.pow(0.5, Math.Infinity) // 0.0
Math.pow(Math.Infinity, -1.0) // 0.0Name the arguments:
Math.pow(x: 2.0, y: 8.0) // 256.0In a pipe, the value is x, the base:
2.0 | Math.pow(3.0) // 8.0Square every element of an array with a lambda:
[1.0, 2.0, 3.0].map({ Math.pow(x, 2.0) }) // [1.0, 4.0, 9.0]Applications
Grow a deposit at 5% interest compounded yearly for ten years:
let principal = 1000.0
principal * Math.pow(1.05, 10.0) // 1628.8946267774415Round to a number of decimal places:
func roundTo(x: float64, places: int): float64 {
let scale = Math.pow(10.0, float64(places))
Math.round(x * scale) / scale
}
roundTo(3.14159, 2) // 3.14
roundTo(2.71828, 3) // 2.718Format a byte count with a unit, dividing by powers of 1024:
func size(bytes: int): string {
let units = ["B", "KiB", "MiB", "GiB"]
var i = units.length() - 1
while i > 0 && float64(bytes) < Math.pow(1024.0, float64(i)) {
i -= 1
}
let value = float64(bytes) / Math.pow(1024.0, float64(i))
"${String.fromFloat(value, 1)} ${units[i]}"
}
println(size(512))
println(size(1536))
println(size(5000000))512.0 B
1.5 KiB
4.8 MiBThe geometric mean of some growth rates is the product to the power one over the count:
let rates = [1.1, 1.2, 0.9]
var product = 1.0
for r in rates {
product *= r
}
Math.pow(product, 1.0 / float64(rates.length())) // 1.059104500597819Properties and relations
Exponents add when powers multiply, and a power of a power multiplies the exponents, each up to rounding:
func close(a: float64, b: float64): bool {
Math.abs(a - b) <= 1e-12 * Math.max(Math.abs(a), Math.abs(b))
}
for [x, a, b] in [[2.0, 3.0, 4.0], [1.5, -2.0, 0.5], [10.0, 0.25, 1.75]] {
assert(close(Math.pow(x, a) * Math.pow(x, b), Math.pow(x, a + b)))
assert(close(Math.pow(Math.pow(x, a), b), Math.pow(x, a * b)))
assert(Math.pow(x, 0.0) == 1.0)
assert(Math.pow(x, 1.0) == x)
}The power 0.5 gives the square root, as Math.sqrt does, up to rounding, and Math.log takes the exponent back out:
for x in [2.0, 3.0, 10.0, 0.25] {
assert(Math.abs(Math.pow(x, 0.5) - Math.sqrt(x)) <= 1e-15 * Math.sqrt(x))
assert(Math.abs(Math.log(Math.pow(x, 3.0)) / Math.log(x) - 3.0) < 1e-12)
}Possible issues
1.0 / 3.0 is not exactly a third, so Math.pow misses the cube root by the last digit, and gives NaN for a negative base. Use Math.cbrt:
Math.pow(8.0, 1.0 / 3.0) // 1.9999999999999998
Math.pow(-8.0, 1.0 / 3.0) // NaN
Math.cbrt(-8.0) // -2.0The result overflows to Infinity and underflows to 0.0:
Math.pow(10.0, 309.0) // Infinity
Math.pow(10.0, -400.0) // 0.0A float64 holds integers exactly only up to 2⁵³, so a large integer power loses its last digits. Multiply in int when you need them all:
int(Math.pow(3.0, 39.0)) // 4052555153018976256
var p = 1
for _ in Iter.range(0, 39) {
p *= 3
}
p // 4052555153018976267Converting a result past Math.maxInt back to int panics:
int(Math.pow(2.0, 63.0)) // panic: 9223372036854776000.0 out of range for intA fractional result panics too:
int(Math.pow(10.0, -1.0)) // panic: 0.1 has a fraction, so it does not convert to intUse Math.toInt to get null instead, or round first:
Math.toInt(Math.pow(10.0, -1.0)) // null
int(Math.round(Math.pow(2.0, 0.5))) // 1The arguments are float64. An int is a compile error; convert it first:
let n = 10
let x = Math.pow(2.0, n) // error: cannot use int where float64 is expectedlet n = 10
Math.pow(2.0, float64(n)) // 1024.0Ant has no ** or ^ operator, and Math.pow is not a function on float64, so it cannot be called through .:
let x = 2.0.pow(3.0) // error: float64 has no field or function powSee also
Math.sqrt · Math.cbrt · Math.exp · Math.log · Math.hypot · Math
