Is it correct to compare a double to zero if you previously initialized it to zero?

后端 未结 1 1118
暗喜
暗喜 2021-02-05 13:25

I learnt that comparing a double using == is not a wise practice. However I was wondering if checking if a double has been initialized could be dangerous or not. Fo

1条回答
  •  伪装坚强ぢ
    2021-02-05 14:01

    In IEEE-754, long story short:

    double d;
    
    d = 0.0;
    d == 0.0   // guaranteed to evaluate to true, 0.0 can be represented exactly in double
    

    but

    double d;
    
    d = 0.1;
    d == 0.1   // not guaranteed to evaluate to true
    

    0 讨论(0)
提交回复
热议问题