Is checking a double for equality ever safe?

前端 未结 5 1194
时光说笑
时光说笑 2020-12-03 04:42

I have the following code:

double x = 0;

{ ...do stuff ...}

if(x == 0){

}

I was always taught that you shouldn\'t check floats for equal

5条回答
  •  日久生厌
    2020-12-03 05:18

    It is safe if the 0 you're trying to catch is the original 0 set at initialization. However, it isn't safe if you're expecting a 0 from a mathematical operation.

提交回复
热议问题