File: float_comparison.patch

package info (click to toggle)
o2 1.1~ds-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,416 kB
  • sloc: ansic: 11,340; python: 145; sh: 119; makefile: 73
file content (54 lines) | stat: -rw-r--r-- 1,688 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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);
         }
     }