动手实验:
请运行以下代码(TestDouble.java)
public class TestDouble
public static void main(String args[]){
System. out. println("0.05 + 0.01 =" + (0.05 + 0.01));
System.out. println("1.0 - 0.42 =" + (1.0 - 0.42));
System.out. println("4.015 * 100="+(4.015*100));
System.out. println("123.3/100="+(123.3/100));
}

你看到了什么样的输出,意外吗?

0.05 + 0.01 = 0.060000000000000005
1.0 - 0.42 = 0.5800000000000001
4.015 * 100 = 401.49999999999994
123.3 / 100 = 1.2329999999999999
意外,浮点数运算的精度问题需要特别注意

 posted on 2024-09-28 10:07  敝屣  阅读(0)  评论(0编辑  收藏  举报