[英]How can I get the 5th root in Java?
I am trying for my program to get the 5th root of a number. 我正在尝试为我的程序获取数字的第5个根。 I have used the Math.pow(a, b);
我使用过Math.pow(a, b);
method to get this, however it isn't working for some reason. 得到这个的方法,但由于某种原因它不起作用。
Lets say I do something like double z = Math.pow(5, 5)
. 假设我做了像double z = Math.pow(5, 5)
这样的事情。 I do a System.out
on this once it gets the value and it will print a result of 3125
. 一旦获得值,我就对它执行System.out
,它将打印3125
的结果。 When I do z = Math.Pow(z, 1/5)
afterwards on it and do a System.out, it doesn't give me a result of 5, but rather 1. Can anyone explain to me why this is happening? 当我之后执行z = Math.Pow(z, 1/5)
并执行System.out时,它不会给我5的结果,而是1.可以有人向我解释为什么会发生这种情况吗?
Thanks! 谢谢!
try z = Math.Pow(z, 1.0/5)
尝试z = Math.Pow(z, 1.0/5)
1/5 == 0
in java java中的1/5 == 0
It's because you are doing an integer division: 1/5
is 0 hence the result (x^0 is always 1). 这是因为你正在进行整数除法: 1/5
为0因此结果(x ^ 0总是1)。
Try: z = Math.Pow(z, 1.0d/5);
尝试: z = Math.Pow(z, 1.0d/5);
1/5 rounds to 0 as an integer division. 1/5轮到0作为整数除法。
Try 1.0/5, or just put 0.2. 尝试1.0 / 5,或者只是0.2。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.