Calculating nth root in Java using power method

后端 未结 9 2646
礼貌的吻别
礼貌的吻别 2021-02-18 22:08

I was trying to get a cubic root in java using Math.pow(n, 1.0/3) but because it divides doubles, it doesn\'t return the exact answer. For example, with 125, this g

9条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-18 22:19

    You can use some tricks come from mathematics field, to havemore accuracy. Like this one x^(1/n) = e^(lnx/n).

    Check the implementation here: https://www.baeldung.com/java-nth-root

提交回复
热议问题