在qt中有对于浮点数比较函数qFuzzyIsNull,其实现代码也相对简单
static inline bool qFuzzyIsNull(float f)
{
return qAbs
(f
) <= 0.00001f;
}
其中一般使用是将两个浮点数相减,其中底层大量使用,如QPointF的比较操作
inline bool operator==(const QPointF &p1
, const QPointF &p2
){
return qFuzzyIsNull
(p1
.xp
- p2
.xp
) && qFuzzyIsNull
(p1
.yp
- p2
.yp
);
}