Comparing floating point values

前端 未结 3 1870
别跟我提以往
别跟我提以往 2020-11-27 21:12

I just read a statement about the floating point value comparison

Floating point values shall not be compared using either the == or != operators.

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 21:47

    Generally floating point numbers should be compared using a construct like

    if( abs((x1 - x2) < 0.001) )
    

    The reason for the warning you quoted is you may have two methods of calculating something, and they may be equal if you had no rounding error, but the rounding error makes them slightly different.

提交回复
热议问题