Chris Murphy Chris Murphy - 4 months ago 11
Java Question

sqrt, cbrt - what if don't want squared or cubed root, but any root?

In maths libraries in Java and Clojure you can usually find a power (or exponent) function. In

java.lang.Math
it is
exp
. But I haven't been able to find a generic function for finding any root. In
java.lang.Math
you can get the squared root with
sqrt
, and the cubed root with
cbrt
. What if I want to find the 4th root? I am looking for an implementation or the formula.

Answer

Use Math.pow(a, 1/b) to compute the bth root of a.

Comments