mirror of
https://github.com/ThrowTheSwitch/Unity.git
synced 2026-10-10 18:57:23 +00:00
Round ties to even by default, many C libraries follow this
Linux gcc & clang and OSX clang produce output with ties round to even Windows mingw gcc does not Example 0.0078125 prints '0.007812'
This commit is contained in:
@@ -262,6 +262,9 @@ static void UnityPrintDecimalAndNumberWithLeadingZeros(_US32 fraction_part, _US3
|
|||||||
if (fraction_part == 0) break; /* Truncate trailing 0's */
|
if (fraction_part == 0) break; /* Truncate trailing 0's */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#define ROUND_TIES_TO_EVEN(num_int, num) \
|
||||||
|
if ((num_int & 1) == 1 && num_int > (num)) /* Odd and was rounded up */ \
|
||||||
|
if ((num) - (_US32)(num) <= 0.5) num_int -= 1 /* and remainder was 0.5, a tie */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* char buffer[19];
|
* char buffer[19];
|
||||||
@@ -288,6 +291,7 @@ void UnityPrintFloat(_UD number)
|
|||||||
_UU32 integer_part = (_UU32)number;
|
_UU32 integer_part = (_UU32)number;
|
||||||
_US32 fraction_part = (_US32)((number - integer_part)*1000000.0 + 0.5);
|
_US32 fraction_part = (_US32)((number - integer_part)*1000000.0 + 0.5);
|
||||||
/* Double precision calculation gives best performance for six rounded decimal places */
|
/* Double precision calculation gives best performance for six rounded decimal places */
|
||||||
|
ROUND_TIES_TO_EVEN(fraction_part, (number - integer_part)*1000000.0);
|
||||||
|
|
||||||
if (fraction_part == 1000000)
|
if (fraction_part == 1000000)
|
||||||
{
|
{
|
||||||
@@ -312,6 +316,7 @@ void UnityPrintFloat(_UD number)
|
|||||||
}
|
}
|
||||||
integer_part = (_US32)(number / divide + 0.5);
|
integer_part = (_US32)(number / divide + 0.5);
|
||||||
/* Double precision calculation required for float, to produce 9 rounded digits */
|
/* Double precision calculation required for float, to produce 9 rounded digits */
|
||||||
|
ROUND_TIES_TO_EVEN(integer_part, number / divide);
|
||||||
|
|
||||||
UNITY_OUTPUT_CHAR('0' + integer_part / divisor);
|
UNITY_OUTPUT_CHAR('0' + integer_part / divisor);
|
||||||
UnityPrintDecimalAndNumberWithLeadingZeros(integer_part % divisor, divisor / 10);
|
UnityPrintDecimalAndNumberWithLeadingZeros(integer_part % divisor, divisor / 10);
|
||||||
|
|||||||
@@ -3243,6 +3243,8 @@ void testFloatVerbosePrinting(void)
|
|||||||
float smallest = 0.0000005f;
|
float smallest = 0.0000005f;
|
||||||
*(int*)&smallest += 1;
|
*(int*)&smallest += 1;
|
||||||
TEST_ASSERT_EQUAL_PRINT_FLOATING("0.000001", smallest);
|
TEST_ASSERT_EQUAL_PRINT_FLOATING("0.000001", smallest);
|
||||||
|
TEST_ASSERT_EQUAL_PRINT_FLOATING("0.007812", 0.0078125f); /*not if ties round away from 0*/
|
||||||
|
TEST_ASSERT_EQUAL_PRINT_FLOATING("0.976562", 0.9765625f); /*not if ties round away from 0*/
|
||||||
TEST_ASSERT_EQUAL_PRINT_FLOATING("0.100469", 0.100469499f);
|
TEST_ASSERT_EQUAL_PRINT_FLOATING("0.100469", 0.100469499f);
|
||||||
TEST_ASSERT_EQUAL_PRINT_FLOATING("1.0", 0.9999995f); /*Rounding to int place*/
|
TEST_ASSERT_EQUAL_PRINT_FLOATING("1.0", 0.9999995f); /*Rounding to int place*/
|
||||||
TEST_ASSERT_EQUAL_PRINT_FLOATING("1.0", 1.0f);
|
TEST_ASSERT_EQUAL_PRINT_FLOATING("1.0", 1.0f);
|
||||||
@@ -3259,6 +3261,7 @@ void testFloatVerbosePrinting(void)
|
|||||||
TEST_ASSERT_EQUAL_PRINT_FLOATING("8.3099991e+09", 8309999104.0f);
|
TEST_ASSERT_EQUAL_PRINT_FLOATING("8.3099991e+09", 8309999104.0f);
|
||||||
TEST_ASSERT_EQUAL_PRINT_FLOATING("1.0e+10", 1.0e+10f);
|
TEST_ASSERT_EQUAL_PRINT_FLOATING("1.0e+10", 1.0e+10f);
|
||||||
TEST_ASSERT_EQUAL_PRINT_FLOATING("1.0e+10", 10000000000.0f);
|
TEST_ASSERT_EQUAL_PRINT_FLOATING("1.0e+10", 10000000000.0f);
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_PRINT_FLOATING("1.00005499e+10", 1.000055e+10f);
|
TEST_ASSERT_EQUAL_PRINT_FLOATING("1.00005499e+10", 1.000055e+10f);
|
||||||
TEST_ASSERT_EQUAL_PRINT_FLOATING("1.10000006e+38", 1.10000005e+38f);
|
TEST_ASSERT_EQUAL_PRINT_FLOATING("1.10000006e+38", 1.10000005e+38f);
|
||||||
TEST_ASSERT_EQUAL_PRINT_FLOATING("1.63529943e+10", 1.63529943e+10f);
|
TEST_ASSERT_EQUAL_PRINT_FLOATING("1.63529943e+10", 1.63529943e+10f);
|
||||||
@@ -3275,6 +3278,9 @@ void testFloatVerbosePrinting(void)
|
|||||||
TEST_ASSERT_EQUAL_PRINT_FLOATING("4.2949673e+09", 4294967295.9999995);
|
TEST_ASSERT_EQUAL_PRINT_FLOATING("4.2949673e+09", 4294967295.9999995);
|
||||||
TEST_ASSERT_EQUAL_PRINT_FLOATING("4.2949673e+09", 4294967296.0);
|
TEST_ASSERT_EQUAL_PRINT_FLOATING("4.2949673e+09", 4294967296.0);
|
||||||
TEST_ASSERT_EQUAL_PRINT_FLOATING("1.0e+10", 9999999995.0);
|
TEST_ASSERT_EQUAL_PRINT_FLOATING("1.0e+10", 9999999995.0);
|
||||||
|
TEST_ASSERT_EQUAL_PRINT_FLOATING("1.0e+10", 10000000050.0); /*not if ties round away from 0*/
|
||||||
|
TEST_ASSERT_EQUAL_PRINT_FLOATING("9.00719924e+15", 9007199245000000.0); /*not if ties round away from 0*/
|
||||||
|
TEST_ASSERT_EQUAL_PRINT_FLOATING("9.00719925e+15", 9007199254740990.0);
|
||||||
TEST_ASSERT_EQUAL_PRINT_FLOATING("7.0e+100", 7.0e+100);
|
TEST_ASSERT_EQUAL_PRINT_FLOATING("7.0e+100", 7.0e+100);
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_PRINT_FLOATING("Inf", 1.7976931348623157e308*10.0);
|
TEST_ASSERT_EQUAL_PRINT_FLOATING("Inf", 1.7976931348623157e308*10.0);
|
||||||
|
|||||||
Reference in New Issue
Block a user