fix warnings about double/float comparisons in eina vector
authorCarsten Haitzler (Rasterman) <raster@rasterman.com>
Fri, 20 May 2016 11:55:48 +0000 (20:55 +0900)
committerCarsten Haitzler (Rasterman) <raster@rasterman.com>
Fri, 20 May 2016 12:46:50 +0000 (21:46 +0900)
this should fix T3245

this is basicall where we go double a == double b and due to precision
issues this may not always be right, but this means that the
equivalent now checks for "really close values" rather than perfectly
exact.

@fix

src/lib/eina/eina_inline_vector.x
src/lib/eina/eina_types.h

index 040c054..67f2e33 100644 (file)
@@ -412,7 +412,9 @@ static inline Eina_Bool
 eina_vector3_equivalent(Eina_Vector3 *a, const Eina_Vector3 *b)
 {
    /* Assume "v" is a directional vector. (v->w == 0.0) */
-   return ((a->x == b->x) &&  (a->y == b->y) && (a->z == b->z));
+   return (EINA_DOUBLE_EQUAL(a->x, b->x) &&
+           EINA_DOUBLE_EQUAL(a->y, b->y) &&
+           EINA_DOUBLE_EQUAL(a->z, b->z));
 }
 
 static inline Eina_Bool
@@ -420,11 +422,16 @@ eina_vector3_triangle_equivalent(Eina_Vector3 *v0, Eina_Vector3 *v1,
                                  Eina_Vector3 *v2, Eina_Vector3 *w0,
                                  Eina_Vector3 *w1, Eina_Vector3 *w2)
 {
-   if (((v0->x == w0->x) && (v0->y == w0->y) && (v0->z == w0->z)) &&
-       ((v1->x == w1->x) && (v1->y == w1->y) && (v1->z == w1->z)) &&
-       ((v2->x == w2->x) && (v2->y == w2->y) && (v2->z == w2->z)))
+   if ((EINA_DOUBLE_EQUAL(v0->x, w0->x) &&
+        EINA_DOUBLE_EQUAL(v0->y, w0->y) &&
+        EINA_DOUBLE_EQUAL(v0->z, w0->z)) &&
+       (EINA_DOUBLE_EQUAL(v1->x, w1->x) &&
+        EINA_DOUBLE_EQUAL(v1->y, w1->y) &&
+        EINA_DOUBLE_EQUAL(v1->z, w1->z)) &&
+       (EINA_DOUBLE_EQUAL(v2->x, w2->x) &&
+        EINA_DOUBLE_EQUAL(v2->y, w2->y) &&
+        EINA_DOUBLE_EQUAL(v2->z, w2->z)))
      return EINA_TRUE;
-
    return EINA_FALSE;
 }
 
index 2e0c4d7..b7bce49 100644 (file)
@@ -410,6 +410,16 @@ typedef void (*Eina_Free_Cb)(void *data);
 #define EINA_C_ARRAY_LENGTH(arr) (sizeof(arr) / sizeof((arr)[0]))
 
 /**
+ * @def EINA_DOUBLE_EQUAL
+ * Macro to compare 2 double floating point values and deal with precision
+ * loss issues.
+ * 
+ * @since 1.18
+ */
+#define EINA_DOUBLE_EQUAL(x, y) \
+   (fabs((x) - (y)) <= (2.2204460492503131e-16) * fabs((x)))
+
+/**
  * @}
  */