Math.cbrt
func Math.cbrt(x: float64): float64Math.cbrt returns the cube root of x.
Details
- A negative
xhas a negative cube root, soMath.cbrt(-8.0)is-2.0, notNaN. Math.cbrtnever panics:NaNgivesNaN, and an infinity gives the infinity of the same sign.
Basic examples
Math.cbrt(27.0) // 3.0
Math.cbrt(2.0) // 1.2599210498948732A negative number has a real cube root:
Math.cbrt(-8.0) // -2.0Scope
Zero keeps its sign, and a positive fraction below 1 gives a larger result:
Math.cbrt(0.0) // 0.0
Math.cbrt(-0.0) // -0.0
Math.cbrt(0.001) // 0.1Infinities and NaN pass through:
Math.cbrt(Math.Infinity) // Infinity
Math.cbrt(-Math.Infinity) // -Infinity
Math.cbrt(Math.NaN) // NaNThe largest float64 has a finite cube root:
Math.cbrt(Math.maxFloat64) // 5.643803094122362e+102An integer goes through float64 first:
let n = 125
Math.cbrt(float64(n)) // 5.0Pass Math.cbrt as a function value, or reach a field of a record in a lambda:
[1.0, 8.0, 27.0].map(Math.cbrt) // [1.0, 2.0, 3.0]
type Box = { volume: float64 }
let boxes = [Box{ volume: 8.0 }, Box{ volume: 1000.0 }]
boxes.map({ Math.cbrt(x.volume) }) // [2.0, 10.0]In a pipe, the value is the argument:
64.0 | Math.cbrt() // 4.0Applications
The radius of a sphere from its volume, r = ∛(3V / 4π):
func sphereRadius(volume: float64): float64 {
Math.cbrt(3.0 * volume / (4.0 * Math.PI))
}
sphereRadius(4.0 / 3.0 * Math.PI * 8.0) // 2.0
sphereRadius(1.0) // 0.6203504908994The geometric mean of three numbers:
func geometricMean(a: float64, b: float64, c: float64): float64 {
Math.cbrt(a * b * c)
}
geometricMean(2.0, 4.0, 8.0) // 4.0
geometricMean(1.0, 10.0, 100.0) // 10.0Properties and relations
For these perfect cubes, the cube of the cube root gives the number back, and negating the number negates the root:
for n in [1.0, 8.0, 27.0, 64.0, -125.0] {
let r = Math.cbrt(n)
assert(r * r * r == n)
assert(Math.cbrt(-n) == -r)
}The cube root of the square root is the sixth root:
Math.cbrt(Math.sqrt(64.0)) // 2.0
Math.sqrt(Math.cbrt(64.0)) // 2.0Possible issues
Math.pow(x, 1.0 / 3.0) is not a cube root: it gives NaN for a negative x, and 1.0 / 3.0 is not exactly a third, so a perfect cube can miss:
Math.pow(-8.0, 1.0 / 3.0) // NaN
Math.pow(27.0, 1.0 / 3.0) // 2.9999999999999996
Math.cbrt(27.0) // 3.0An int argument is a compile error, since numeric types never mix:
let side = Math.cbrt(27) // error: cannot use int where float64 is expectedConverting the result back to int panics when it has a fraction. Use Math.toInt to get null instead, or round first:
int(Math.cbrt(64.0)) // 4
int(Math.cbrt(10.0)) // panic: 2.154434690031884 has a fraction, so it does not convert to int
Math.toInt(Math.cbrt(10.0)) // null
int(Math.round(Math.cbrt(10.0))) // 2See also
Math.sqrt · Math.pow · Math.hypot · Math.exp · Math.log · Math.toInt · float
