From a5e46fdaffe01f0041ea4129139ce8cfc32d375b Mon Sep 17 00:00:00 2001 From: APX403 Date: Sat, 17 Oct 2026 19:12:03 +0300 Subject: [PATCH] Print small floats instead of 0 Since 8c26b85 (#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. --- docs/UnityChangeLog.md | 1 + src/unity.c | 4 +--- test/tests/test_unity_doubles.c | 4 ++++ 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/UnityChangeLog.md b/docs/UnityChangeLog.md index 6e73e27c..a6ae8c0b 100644 --- a/docs/UnityChangeLog.md +++ b/docs/UnityChangeLog.md @@ -19,6 +19,7 @@ Significant Bugfixes: - Default `UNITY_INCLUDE_EXEC_TIME` macros compile as ISO C99, are statement-safe, and accept `-Wsign-conversion` (#838) - `unity_test_summary.rb` honors its own default result directory and root path again. @youdie006 + - Floats and doubles smaller than `UNITY_FLOAT_PRECISION` or `UNITY_DOUBLE_PRECISION` print as their value again instead of `0`. @APX403 ### Unity 2.7.0 (July 2026) diff --git a/src/unity.c b/src/unity.c index 68e833cc..fc981865 100644 --- a/src/unity.c +++ b/src/unity.c @@ -367,18 +367,16 @@ void UnityPrintFloat(const UNITY_DOUBLE input_number) static const int sig_digits = 9; static const UNITY_INT32 min_scaled = 100000000; static const UNITY_INT32 max_scaled = 1000000000; - static const UNITY_DOUBLE epsilon = UNITY_DOUBLE_PRECISION; #else static const int sig_digits = 7; static const UNITY_INT32 min_scaled = 1000000; static const UNITY_INT32 max_scaled = 10000000; - static const UNITY_DOUBLE epsilon = UNITY_FLOAT_PRECISION; #endif UNITY_DOUBLE number = input_number; /* handle zero, NaN, and +/- infinity */ - if (UNITY_ABS(number) < epsilon) + if ((number >= 0.0f) && (number <= 0.0f)) { UnityPrint("0"); } diff --git a/test/tests/test_unity_doubles.c b/test/tests/test_unity_doubles.c index 0462bb56..c79670fb 100644 --- a/test/tests/test_unity_doubles.c +++ b/test/tests/test_unity_doubles.c @@ -1249,11 +1249,15 @@ void testDoublePrinting(void) TEST_ASSERT_EQUAL_PRINT_FLOATING("7e+100", 7.0e+100); TEST_ASSERT_EQUAL_PRINT_FLOATING("3e+200", 3.0e+200); TEST_ASSERT_EQUAL_PRINT_FLOATING("9.23456789e+300", 9.23456789e+300); + TEST_ASSERT_EQUAL_PRINT_FLOATING("4.99e-13", 4.99e-13); + TEST_ASSERT_EQUAL_PRINT_FLOATING("7e-100", 7.0e-100); TEST_ASSERT_EQUAL_PRINT_FLOATING("-0.1004695", -0.10046949999999999); TEST_ASSERT_EQUAL_PRINT_FLOATING("-4.2949673e+09", -4294967295.9); TEST_ASSERT_EQUAL_PRINT_FLOATING("-4.2949673e+09", -4294967296.0); TEST_ASSERT_EQUAL_PRINT_FLOATING("-7e+100", -7.0e+100); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-4.99e-13", -4.99e-13); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-7e-100", -7.0e-100); #endif }