double arithmetic and equality in Java

后端 未结 3 1671
-上瘾入骨i
-上瘾入骨i 2020-12-07 00:01

Here\'s an oddity (to me, at least). This routine prints true:

double x = 11.0;
double y = 10.0;
if (x-y == 1.0) {
    // print true
} else {
    // print fa         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-07 00:31

    This comes up in currency calculations all the time. Use BigDecimal if you need exact numerical representation at the cost of not having hardware enabled performance, of course.

提交回复
热议问题