Use a more forgiving threshold for qFuzzyIsNull(qfloat16)

Analysis of problems with the new test for qFuzzyIsNull() revealed
that, where its version for double uses approximately 4500 * epsilon
and for float used 84 * epsilon as threshold, the qfloat16 version's
value was barely more than epsilon, with the result that the test had
to use a different value than the threshold to pass. (Converting the
threshold from float to qfloat16 and back made it bigger; in effect,
the threshold value was not <= itself.)

Furthermore, comparison with qFuzzyCompare() implied a value of
1/102.5 should be used, roughly 10 * epsilon, for consistency.  When
1/102.5 is rounded to three significant digits (the precision we use
in QTest::toString(), for example), to give 0.00976f as threshold, we
get a value that, after conversion to qfloat16 and back to float, does
give a result <= what we started with. So change qFuzzyIsNull() and
its test to use this as qfloat16's threshold value.

[ChangeLog][QtCore][QFloat16] The qfloat16 threshold value for
qFuzzyIsNull() has changed from 1e-3 to 9.76e-3, almost a factor of
ten increase, for consistency with qFuzzyCompare()'s tolerance. Values
between these would previously have had qFuzzyIsNull(f) false despite
qFuzzyCompre(f, 1+f) being true.

Change-Id: I35816dce78da34a3e2339c8bc42d5bd03714a3f6
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Andreas Buhr <andreas.buhr@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Edward Welbourne 2021-04-15 17:47:05 +02:00
parent 6dfec83051
commit 9bad096c09
2 changed files with 2 additions and 2 deletions

View File

@ -239,7 +239,7 @@ Q_CORE_EXPORT void qFloatFromFloat16(float *, const qfloat16 *, qsizetype length
*/ */
[[nodiscard]] inline bool qFuzzyIsNull(qfloat16 f) noexcept [[nodiscard]] inline bool qFuzzyIsNull(qfloat16 f) noexcept
{ {
return qAbs(static_cast<float>(f)) <= 0.001f; return qAbs(f) < 0.00976f; // 1/102.5 to 3 significant digits; see qFuzzyCompare()
} }
[[nodiscard]] inline bool qIsNull(qfloat16 f) noexcept [[nodiscard]] inline bool qIsNull(qfloat16 f) noexcept

View File

@ -118,7 +118,7 @@ void tst_qfloat16::fuzzyIsNull_data()
QTest::addColumn<qfloat16>("value"); QTest::addColumn<qfloat16>("value");
QTest::addColumn<bool>("isNull"); QTest::addColumn<bool>("isNull");
using Bounds = std::numeric_limits<qfloat16>; using Bounds = std::numeric_limits<qfloat16>;
const qfloat16 one(1), huge(1000), tiny(0.00099f); const qfloat16 one(1), huge(1000), tiny(0.00976f);
QTest::newRow("zero") << qfloat16(0.0f) << true; QTest::newRow("zero") << qfloat16(0.0f) << true;
QTest::newRow("min") << Bounds::min() << true; QTest::newRow("min") << Bounds::min() << true;