From 9c637ccb41adb33721b209bcc53732aba240d731 Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Thu, 19 Apr 2012 12:30:45 +0100 Subject: [PATCH] Use an epsilon for float comparison We tend to use float comparison for structured data types like Vertex, Point, and Size; we should take into consideration fluctuations in the floating point representation as well. --- clutter/clutter-base-types.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/clutter/clutter-base-types.c b/clutter/clutter-base-types.c index 90f3812b3..aefd16462 100644 --- a/clutter/clutter-base-types.c +++ b/clutter/clutter-base-types.c @@ -39,6 +39,8 @@ #include +#define FLOAT_EPSILON (1e-15) + /* @@ -277,9 +279,9 @@ clutter_vertex_equal (const ClutterVertex *vertex_a, if (vertex_a == vertex_b) return TRUE; - return vertex_a->x == vertex_b->x && - vertex_a->y == vertex_b->y && - vertex_a->z == vertex_b->z; + return fabsf (vertex_a->x - vertex_b->x) < FLOAT_EPSILON && + fabsf (vertex_a->y - vertex_b->y) < FLOAT_EPSILON && + fabsf (vertex_a->z - vertex_b->z) < FLOAT_EPSILON; } static gboolean @@ -469,7 +471,8 @@ clutter_point_equals (const ClutterPoint *a, if (a == NULL || b == NULL) return FALSE; - return a->x == b->x && a->y == b->y; + return fabsf (a->x - b->x) < FLOAT_EPSILON && + fabsf (a->y - b->y) < FLOAT_EPSILON; } /** @@ -636,7 +639,8 @@ clutter_size_equals (const ClutterSize *a, if (a == NULL || b == NULL) return FALSE; - return a->width == b->width && a->height == b->height; + return fabsf (a->width - b->width) < FLOAT_EPSILON && + fabsf (a->height - b->height) < FLOAT_EPSILON; } static gboolean