Print small floats instead of 0 by APX403 · Pull Request #856 · ThrowTheSwitch/Unity · GitHub
Skip to content

Print small floats instead of 0 - #856

Open
APX403 wants to merge 1 commit into
ThrowTheSwitch:masterfrom
APX403:print-small-floats
Open

APX403 wants to merge 1 commit into
ThrowTheSwitch:masterfrom
APX403:print-small-floats

Conversation

@APX403

@APX403 APX403 commented Oct 17, 2026

Copy link
Copy Markdown

🍍

Since 2.7.0, UnityPrintFloat() prints 0 for every value whose magnitude is below UNITY_FLOAT_PRECISION (1e-5 by default), or below UNITY_DOUBLE_PRECISION (1e-12) when UNITY_INCLUDE_DOUBLE is defined. In a default build,

TEST_ASSERT_EQUAL_FLOAT(0.000001f, 0.000003f);

fails with Expected 0 Was 0, where 2.6.1 printed Expected 1e-06 Was 3e-06.

8c26b85 (#770) changed the zero check to UNITY_ABS(number) < epsilon to avoid == on floating point values, and took the epsilon from the precision macros. Those macros are the tolerance of the equality asserts, not a print threshold. The check is now (number >= 0.0f) && (number <= 0.0f), which is true only for zero, false for NaN, and does not trigger -Wfloat-equal with gcc or clang.

testDoublePrinting gains values below the old threshold. The float-only printing tests already expect 4.99e-07 and 5e-07, but CI does not build that configuration. Built that way, testFloatPrinting still fails on its -0 line, which has been stale since 60b23dc changed only the double test.

Since 8c26b85 (ThrowTheSwitch#770), UnityPrintFloat() prints 0 for every value whose
magnitude is below UNITY_FLOAT_PRECISION, or below
UNITY_DOUBLE_PRECISION when doubles are enabled. Those macros are the
relative tolerance of the equality asserts, not a print threshold, so
a failing TEST_ASSERT_EQUAL_FLOAT(1e-6f, 3e-6f) in a default build
reports "Expected 0 Was 0".

Test for an exact zero with two ordered comparisons instead. They are
false for NaN and keep -Wfloat-equal quiet, which was the reason for
the epsilon in the first place.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant