# HG changeset patch # User Mike Becker # Date 1361959060 -3600 # Node ID 7625a8efcc977649add95564e4ef3326636d0dae # Parent 91595a45fad6ed12c089c4d5a14e26b175c1f5e3 added floating point comparators diff -r 91595a45fad6 -r 7625a8efcc97 ucx/comparator.c --- a/ucx/comparator.c Wed Feb 27 10:35:42 2013 +0100 +++ b/ucx/comparator.c Wed Feb 27 10:57:40 2013 +0100 @@ -1,4 +1,5 @@ #include "comparator.h" +#include "math.h" int ucx_strcmp(void *s1, void *s2, void *data) { return strcmp((char*)s1, (char*)s2); @@ -18,6 +19,28 @@ } } +int ucx_floatcmp(void *f1, void *f2, void *epsilon) { + float a = *((float*) f1); + float b = *((float*) f2); + float e = !epsilon ? 1e-6f : *((float*)epsilon); + if (fabsf(a - b) < e) { + return 0; + } else { + return a < b ? -1 : 1; + } +} + +int ucx_doublecmp(void *d1, void *d2, void *epsilon) { + double a = *((float*) d1); + double b = *((float*) d2); + double e = !epsilon ? 1e-14 : *((double*)epsilon); + if (fabs(a - b) < e) { + return 0; + } else { + return a < b ? -1 : 1; + } +} + int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) { if (ptr1 == ptr2) { return 0; diff -r 91595a45fad6 -r 7625a8efcc97 ucx/comparator.h --- a/ucx/comparator.h Wed Feb 27 10:35:42 2013 +0100 +++ b/ucx/comparator.h Wed Feb 27 10:57:40 2013 +0100 @@ -38,6 +38,28 @@ int ucx_intcmp(void *i1, void *i2, void *data); /** + * Compares two real numbers of type float. + * @param f1 pointer to float one + * @param f2 pointer to float two + * @param if provided: a pointer to precision (default: 1e-6f) + * @return -1, if *f1 is less than *f2, 0 if both are equal, + * 1 if *f1 is greater than *f2 + */ + +int ucx_floatcmp(void *f1, void *f2, void *data); + +/** + * Compares two real numbers of type double. + * @param f1 pointer to double one + * @param f2 pointer to double two +* @param if provided: a pointer to precision (default: 1e-14) + * @return -1, if *d1 is less than *d2, 0 if both are equal, + * 1 if *d1 is greater than *d2 + */ + +int ucx_doublecmp(void *d1, void *d2, void *data); + +/** * Compares two pointers. * @param ptr1 pointer one * @param ptr2 pointer two