summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCraig Small <csmall@dropbear.xyz>2023-03-01 17:20:42 +1100
committerCraig Small <csmall@dropbear.xyz>2023-03-01 17:20:42 +1100
commit9b2f49166b73cef360cff0682459a0aa3e6ec8d8 (patch)
tree7fb621d11a9df6679b2406be314f26cafc6e8fe9
parent806eb270f217ff7e1e745c7bda2b002b5be74be4 (diff)
downloadprocps-ng-9b2f49166b73cef360cff0682459a0aa3e6ec8d8.tar.gz
tests: Dont compare floats with ==
Comparing floats with == is bad and I should feel bad I did this. The problem is if something is close to, but not quite the exact same fails tests. I have used an epsilon of 1 because we don't care about accuracy, just that the function works well enough. References: issue procps-ng/procps#271 https://how-to.fandom.com/wiki/Howto_compare_floating_point_numbers_in_the_C_programming_language Signed-off-by: Craig Small <csmall@dropbear.xyz>
-rw-r--r--NEWS4
-rw-r--r--src/tests/test_strtod_nol.c6
2 files changed, 10 insertions, 0 deletions
diff --git a/NEWS b/NEWS
index 4f11334..bc293a3 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,7 @@
+procps-ng-NEXT
+---------------
+ * tests: dont compare floats with == issue #271
+
procps-ng-4.0.3
---------------
* library
diff --git a/src/tests/test_strtod_nol.c b/src/tests/test_strtod_nol.c
index 0be798c..8e5c524 100644
--- a/src/tests/test_strtod_nol.c
+++ b/src/tests/test_strtod_nol.c
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
+#include <math.h>
#include "strutils.h"
struct strtod_tests {
@@ -25,6 +26,11 @@ struct strtod_tests tests[] = {
{NULL, 0.0}
};
+#define EPSILON 1.0 // Really not trying for precision here
+int dequal(const double d1, const double d2)
+{
+ return fabs(d1-d2) < EPSILON;
+}
int main(int argc, char *argv[])