diff --git a/src/unity.c b/src/unity.c index c755ebb367234d7053eca97131d99d5c80193546..af5f838aff280086b918c09dfa66e62cb1553621 100644 --- a/src/unity.c +++ b/src/unity.c @@ -688,11 +688,11 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected, /*-----------------------------------------------*/ /* Wrap this define in a function with variable types as float or double */ #define UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff) \ - if (isinf(expected) && isinf(actual) && (isneg(expected) == isneg(actual))) return 1; \ + if (isinf(expected) && isinf(actual) && ((expected < 0) == (actual < 0))) return 1; \ if (UNITY_NAN_CHECK) return 1; \ diff = actual - expected; \ - if (diff < 0.0f) diff = 0.0f - diff; \ - if (delta < 0.0f) delta = 0.0f - delta; \ + if (diff < 0) diff = -diff; \ + if (delta < 0) delta = -delta; \ return !(isnan(diff) || isinf(diff) || (diff > delta)) /* This first part of this condition will catch any NaN or Infinite values */ #ifndef UNITY_NAN_NOT_EQUAL_NAN @@ -793,11 +793,11 @@ void UnityAssertFloatSpecial(const UNITY_FLOAT actual, { case UNITY_FLOAT_IS_INF: case UNITY_FLOAT_IS_NOT_INF: - is_trait = isinf(actual) & ispos(actual); + is_trait = isinf(actual) && (actual > 0); break; case UNITY_FLOAT_IS_NEG_INF: case UNITY_FLOAT_IS_NOT_NEG_INF: - is_trait = isinf(actual) & isneg(actual); + is_trait = isinf(actual) && (actual < 0); break; case UNITY_FLOAT_IS_NAN: @@ -805,13 +805,9 @@ void UnityAssertFloatSpecial(const UNITY_FLOAT actual, is_trait = isnan(actual) ? 1 : 0; break; - /* A determinate number is non infinite and not NaN. (therefore the opposite of the two above) */ - case UNITY_FLOAT_IS_DET: + case UNITY_FLOAT_IS_DET: /* A determinate number is non infinite and not NaN. */ case UNITY_FLOAT_IS_NOT_DET: - if (isinf(actual) || isnan(actual)) - is_trait = 0; - else - is_trait = 1; + is_trait = !isinf(actual) && !isnan(actual); break; default: @@ -922,11 +918,11 @@ void UnityAssertDoubleSpecial(const UNITY_DOUBLE actual, { case UNITY_FLOAT_IS_INF: case UNITY_FLOAT_IS_NOT_INF: - is_trait = isinf(actual) & ispos(actual); + is_trait = isinf(actual) && (actual > 0); break; case UNITY_FLOAT_IS_NEG_INF: case UNITY_FLOAT_IS_NOT_NEG_INF: - is_trait = isinf(actual) & isneg(actual); + is_trait = isinf(actual) && (actual < 0); break; case UNITY_FLOAT_IS_NAN: @@ -934,13 +930,9 @@ void UnityAssertDoubleSpecial(const UNITY_DOUBLE actual, is_trait = isnan(actual) ? 1 : 0; break; - /* A determinate number is non infinite and not NaN. (therefore the opposite of the two above) */ - case UNITY_FLOAT_IS_DET: + case UNITY_FLOAT_IS_DET: /* A determinate number is non infinite and not NaN. */ case UNITY_FLOAT_IS_NOT_DET: - if (isinf(actual) || isnan(actual)) - is_trait = 0; - else - is_trait = 1; + is_trait = !isinf(actual) && !isnan(actual); break; default: diff --git a/src/unity_internals.h b/src/unity_internals.h index 34f0bd3a76f9b046c215af87ce0f5de55fb2499b..b0311dcdeeb0ae6ee19ed857712ac072702020f3 100644 --- a/src/unity_internals.h +++ b/src/unity_internals.h @@ -187,6 +187,7 @@ #endif typedef UNITY_FLOAT_TYPE UNITY_FLOAT; +/* isinf & isnan macros should be provided by math.h */ #ifndef isinf /* The value of Inf - Inf is NaN */ #define isinf(n) (isnan((n) - (n)) && !isnan(n)) @@ -198,14 +199,6 @@ typedef UNITY_FLOAT_TYPE UNITY_FLOAT; #define isnan(n) ((n != n) ? 1 : 0) #endif -#ifndef isneg -#define isneg(n) ((n < 0.0f) ? 1 : 0) -#endif - -#ifndef ispos -#define ispos(n) ((n > 0.0f) ? 1 : 0) -#endif - #endif /*------------------------------------------------------- @@ -243,7 +236,7 @@ typedef UNITY_FLOAT_TYPE UNITY_FLOAT; /* Double Floating Point Support */ #ifndef UNITY_DOUBLE_PRECISION - #define UNITY_DOUBLE_PRECISION (1e-12f) + #define UNITY_DOUBLE_PRECISION (1e-12) #endif #ifndef UNITY_DOUBLE_TYPE diff --git a/test/tests/testunity.c b/test/tests/testunity.c index 08c621af3425036feb14ba0d60ed0b6a5c1cec41..e81e57a45f86e7ece77c9c1eb036ffc00cda26db 100644 --- a/test/tests/testunity.c +++ b/test/tests/testunity.c @@ -3274,8 +3274,9 @@ void testFloatPrinting(void) 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", 10000000000.0f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("1.00005499e+10", 1.000055e+10f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("1.10000006e+38", 1.10000005e+38f); + /* Some compilers have trouble with inexact float constants, a float cast works generally */ + TEST_ASSERT_EQUAL_PRINT_FLOATING("1.00005499e+10", (float)1.000055e+10f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("1.10000006e+38", (float)1.10000005e+38f); TEST_ASSERT_EQUAL_PRINT_FLOATING("1.63529943e+10", 1.63529943e+10f); TEST_ASSERT_EQUAL_PRINT_FLOATING("3.40282347e+38", 3.40282346638e38f);