java - 使用小数指数的 Math.pow

标签 java floating-point exponentiation

这是我写的第一个代码。这是我的代码的一部分,旨在在用户给出体积值后计算立方体和四面体的边长,但是输出不正确。我很确定我的方程是正确的,也许我错误地使用了 Math.pow 方法?

System.out.println("Now, please enter a volume for side or diameter calculation, then press ENTER: ");

volume = input.nextDouble();
cubeSide = Math.pow(volume, (1/3));
sphereDiameter = Math.pow(volume / PI * 6, (1/3));
tetSide = SQRT_2 * Math.pow(3 * volume, (1/3));

System.out.println("");
System.out.println("The Side of your CUBE is : " + cubeSide);
System.out.println("");
System.out.println("The Diameter of your SPHERE is : " + sphereDiameter);
System.out.println("");
System.out.println("The Side of your TETRAHEDRON is : " + tetSide);

关于如何获得正确输出的任何想法?

最佳答案

1/3 is 0 - 当被除数和除数均为整数时,/ 执行整数除法。您需要 1.0/31/3.01.0/3.0,其计算结果为 0.3333333-ish。

关于java - 使用小数指数的 Math.pow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31258674/

相关文章:

java - Swing 应用程序中的依赖注入(inject)

c - printf 浮点变量和常量有什么区别?

prolog - Power with successor arithmetic - 如何防止无限循环? [序言]

python - 将单个元素的列表或numpy数组转换为python中的 float

python - 如果将最小的正浮点值乘以一个非零数,是否保证得到非零结果?

algorithm - 浮点指数求幂算法

c++ - 使用 gmp mpz_pow_ui 的 mpz 类型溢出

java - 在 createQuery 中使用 SELECT DISTINCT 返回列表值

java - 扩展 JPanel 在添加到另一个 JPanel 时显示很小

java - OrmLite 多对多的扭曲