为什么Java这个计算不对?
Why doesn't Java calculate this calculation right?
我最近开始学习Java。我开始研究一个简单的数学项目,用 (1 + 1 / x)^x 来近似欧拉数。但是在第一步,程序 return 输入了错误的值。这是我的最小化代码,解决方案是错误的。
public class MyClass
{
public static void main(String[] args)
{
System.out.println(1 + 1 / 2);
}
}
程序returns 1. 结果在我看来很矛盾。 1 + 1 / 2 return 1 怎么可能?我期待 1.5.
我尝试了一些调试:
double x = 1 + Math.pow(2, -1);
System.out.println(x);
现在可以正常使用了。怎么会这样?
我最近开始学习Java。我开始研究一个简单的数学项目,用 (1 + 1 / x)^x 来近似欧拉数。但是在第一步,程序 return 输入了错误的值。这是我的最小化代码,解决方案是错误的。
public class MyClass
{
public static void main(String[] args)
{
System.out.println(1 + 1 / 2);
}
}
程序returns 1. 结果在我看来很矛盾。 1 + 1 / 2 return 1 怎么可能?我期待 1.5.
我尝试了一些调试:
double x = 1 + Math.pow(2, -1);
System.out.println(x);
现在可以正常使用了。怎么会这样?