Is it wrong to compare a double to 0 like this: doubleVariable==0?

后端 未结 6 764
夕颜
夕颜 2021-01-04 04:57

It is ok to do this?

double doubleVariable=0.0;
if (doubleVariable==0) {
   ...
}

Or this code would suffer from potential rounding problem

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-04 05:47

    You should not use double for such comparision. double creates problem.
    e.g double n1=0.55 double n2=100 then double ans=n1*n2 should be 55.0
    but when you debug ans is 55.000000000000007. if(ans==55.0)
    will fail. in such case you can face a problem.

提交回复
热议问题