简体   繁体   English

如何在Java中获得第5个根?

[英]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.

相关问题 如何获得字符串中的第5个字? 爪哇 - How do I get the 5th word in a string? Java 在 Java 中,我需要用户输入五个整数然后显示平均值。 如何在不输入第 6 个条目的情况下获得总和中包含的第 5 个整数? - In Java, I need the user to enter five integers then show the average. How do I get the 5th integer included in the sum without typing a 6th entry? 如何在每第 5 个数字后开始一个新行? - How can I start a new line after every 5th number? 如何在数组中每第 5 个数字后开始一个新行? - How can I start a new line after every 5th number in an array? 如何提高Java中第n个根算法的精度? - How can I increase the precision of this n-th root algorithm in Java? 如何将一个字符串的第一个字符与 Java 中另一个字符串的第 5 个字符进行比较? 并检查它们是否相同。我收到如下错误 - how to compare first character of one string to 5th char of another string in Java? and check if they are same.I am getting error as below 更改String Java中的每个第5个字符 - Changing every 5th character in a String java 第5个元素上的二进制搜索(Java)异常 - Binary Search (Java) exception on 5th element 我需要从java中给定的字符串打印第1个字符和第5个字符 - I need to print 1st and every 5th character from a given String in java 如何在 Java 中获取受信任的根证书列表? - How can I get a list of trusted root certificates in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM