# 为什么添加0.1多次保持无损？

` `double sum = 0, d = 0.1; for (int i = 0; i < 3; i++) sum += d; System.out.println(sum == 0.3); // Prints false, OK` `

` `double sum = 0, d = 0.1; for (int i = 0; i < 5; i++) sum += d; System.out.println(sum == 0.5); // Prints true, WHY?` `

` `BigDecimal _0_1 = new BigDecimal(0.1); BigDecimal x = _0_1; for(int i = 1; i <= 10; i ++) { System.out.println(i+" x 0.1 is "+x+", as double "+x.doubleValue()); x = x.add(_0_1); }` `

` `0.1000000000000000055511151231257827021181583404541015625, as double 0.1 0.2000000000000000111022302462515654042363166809082031250, as double 0.2 0.3000000000000000166533453693773481063544750213623046875, as double 0.30000000000000004 0.4000000000000000222044604925031308084726333618164062500, as double 0.4 0.5000000000000000277555756156289135105907917022705078125, as double 0.5 0.6000000000000000333066907387546962127089500427246093750, as double 0.6000000000000001 0.7000000000000000388578058618804789148271083831787109375, as double 0.7000000000000001 0.8000000000000000444089209850062616169452667236328125000, as double 0.8 0.9000000000000000499600361081320443190634250640869140625, as double 0.9 1.0000000000000000555111512312578270211815834045410156250, as double 1.0` `