Description: Fix floating point comparison
 use epsilon
Author: IOhannes m zmölnig
Origin: Debian
Bug: https://github.com/rbdannenberg/o2/issues/26
Last-Update: 2022-09-30
---
This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
--- o2.orig/test/arraytest.c
+++ o2/test/arraytest.c
@@ -33,6 +33,17 @@
 #define snprintf _snprintf
 #endif
 
+
+#include <math.h>
+#include <float.h>
+static int equals(double a, double b, double epsilon) {
+	/* check whether the difference is below a scaled epsilon */
+	double diff = fabs(a-b);
+	a = fabs(a);
+	b = fabs(b);
+	return (diff <= (epsilon * (a>b?a:b)));
+}
+
 int got_the_message = FALSE;
 
 o2_blob_ptr a_blob;
@@ -499,11 +510,11 @@
                 break;
             }
             case 'f': {
-                assert(arg->v.vf[i] == 123.456F + i);
+                assert(equals(arg->v.vf[i], 123.456F + i, FLT_EPSILON));
                 break;
             }
             case 'd': {    
-                assert(arg->v.vd[i] == 1234.567 + i);
+                assert(equals(arg->v.vd[i], 1234.567 + i, DBL_EPSILON));
                 break;
             }
             default:
@@ -660,9 +671,9 @@
         switch (ytype) {
             case 'i': assert(arg->i == (int32_t) expected); break;
             case 'h': assert(arg->h == (int64_t) expected); break;
-            case 'f': assert(arg->f == (float) expected); break;
+            case 'f': assert(equals(arg->f, (float) expected, FLT_EPSILON)); break;
             case 'd': case 't':
-                assert(arg->d == expected); break;
+                assert(equals(arg->d, expected, FLT_EPSILON)); break;
             default: assert(FALSE);
         }
     }
