From 41d6933b39a50ddff628c0c697581b0246f37992 Mon Sep 17 00:00:00 2001 From: Mark VanderVoord Date: Mon, 6 Jul 2026 14:47:41 -0400 Subject: [PATCH] Remove situations where we're comparing floats with `==` (Fixes #819) --- examples/example_3/rakefile_helper.rb | 2 +- src/unity.c | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/examples/example_3/rakefile_helper.rb b/examples/example_3/rakefile_helper.rb index 8d42e70..1aabc1b 100644 --- a/examples/example_3/rakefile_helper.rb +++ b/examples/example_3/rakefile_helper.rb @@ -134,7 +134,7 @@ def compile(file, defines = []) if $cfg[:tools][:test_compiler][:executable] =~ /gcc/ $cfg[:tools][:test_compiler][:arguments] |= ['-Wno-misleading-indentation'] end - cmd_str = build_command_string($cfg[:tools][:test_compiler], [file, out_file], defines) + cmd_str = build_command_string($cfg[:tools][:test_compiler], [file, out_file], defines) execute(cmd_str) out_file end diff --git a/src/unity.c b/src/unity.c index c6f6d59..d8a8dd3 100644 --- a/src/unity.c +++ b/src/unity.c @@ -445,13 +445,17 @@ void UnityPrintFloat(const UNITY_DOUBLE input_number) } /* round to nearest integer */ - n = ((UNITY_INT32)(number + number) + 1) / 2; + { + UNITY_DOUBLE two_number = number + number; + UNITY_INT32 two_n_trunc = (UNITY_INT32)two_number; + n = (two_n_trunc + 1) / 2; #ifndef UNITY_ROUND_TIES_AWAY_FROM_ZERO - /* round to even if exactly between two integers */ - if ((n & 1) && (UNITY_ABS((UNITY_DOUBLE)n - number - 0.5f) < epsilon)) - n--; + /* round to even if exactly between two integers */ + if ((n & 1) && (two_n_trunc & 1) && !((UNITY_DOUBLE)two_n_trunc < two_number)) + n--; #endif + } n += n_int;