Is checking a double for equality ever safe?

前端 未结 5 1192
时光说笑
时光说笑 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条回答
  •  Happy的楠姐
    2020-12-03 05:24

    double has positive and negative zero. == between positive zero and negative zero returns false. Also, == betwen two NaNs returns false.

提交回复
热议问题