Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在Math类的静态方法round()中,我注意到一件令我难以理解的事情:
Math.round(0.4999999999999999); // is 0 Math.round(0.49999999999999999); // is 1
为什么?
0.49999999999999999有效数字太多,一个double变量不能全部存储。所以隐式舍入发生在编译期间。当您调用Math.round()时,参数已经是 0.5(检查自己:0.49999999999999999 == 0.5yield true)。
0.49999999999999999
double
Math.round()
0.49999999999999999 == 0.5
true