comparing doubles properly
i have a problem with a piece of code:
- qreal a = 0.0f;
- Q_ASSERT(qAbs(a) == 0)
this causes sometimes to throw the assert, and sometimes not. but why? in my real there is a value like 6.0*10^-311 so its almost 0 but it is not 0, so the assert is right. but why isn’t it 0 when i initialized it with 0?
This is due to limited precision of floating point numbers. Use qFuzzyCompare() [doc.qt.nokia.com] instead.
I also found the method qFuzzyIsNull() its not in the doc, but compares the parameter with 0.0 thats exactly what i need
qFuzzyIsNull is declared internal in the sources. Usually that’s for a reason…
You might want to open a request to make it officially public in the bug tracker [bugreports.qt.nokia.com] though.