我是 Java 新手。我正在编写一个小程序来计算一个数的幂次方的值,当时我遇到了一个负数的小数指数问题。
System.out.println(Math.pow(-8, 1/3f));
输出是 NaN 而我期待的是 -2?
我做错了什么?是否有其他方法可以计算此类问题?
感谢任何帮助。
最佳答案
此案例在 documentation 中有描述.
If the first argument is finite and less than zero. <...> if the second argument is finite and not an integer, then the result is NaN.
据我所知,Java 标准库中没有方法可以做到这一点,因此您必须手动实现它。
关于java - 不确定如何使用 Math.pow(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35312372/