added floating point comparators

Wed, 27 Feb 2013 10:57:40 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 27 Feb 2013 10:57:40 +0100
changeset 92
7625a8efcc97
parent 91
91595a45fad6
child 93
a6a99e721660

added floating point comparators

ucx/comparator.c file | annotate | diff | comparison | revisions
ucx/comparator.h file | annotate | diff | comparison | revisions
     1.1 --- a/ucx/comparator.c	Wed Feb 27 10:35:42 2013 +0100
     1.2 +++ b/ucx/comparator.c	Wed Feb 27 10:57:40 2013 +0100
     1.3 @@ -1,4 +1,5 @@
     1.4  #include "comparator.h"
     1.5 +#include "math.h"
     1.6  
     1.7  int ucx_strcmp(void *s1, void *s2, void *data) {
     1.8      return strcmp((char*)s1, (char*)s2);
     1.9 @@ -18,6 +19,28 @@
    1.10     }
    1.11  }
    1.12  
    1.13 +int ucx_floatcmp(void *f1, void *f2, void *epsilon) {
    1.14 +   float a = *((float*) f1);
    1.15 +   float b = *((float*) f2);
    1.16 +   float e = !epsilon ? 1e-6f : *((float*)epsilon);
    1.17 +   if (fabsf(a - b) < e) {
    1.18 +       return 0;
    1.19 +   } else {
    1.20 +       return a < b ? -1 : 1;
    1.21 +   }
    1.22 +}
    1.23 +
    1.24 +int ucx_doublecmp(void *d1, void *d2, void *epsilon) {
    1.25 +   double a = *((float*) d1);
    1.26 +   double b = *((float*) d2);
    1.27 +   double e = !epsilon ? 1e-14 : *((double*)epsilon);
    1.28 +   if (fabs(a - b) < e) {
    1.29 +       return 0;
    1.30 +   } else {
    1.31 +       return a < b ? -1 : 1;
    1.32 +   }
    1.33 +}
    1.34 +
    1.35  int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) {
    1.36      if (ptr1 == ptr2) {
    1.37          return 0;
     2.1 --- a/ucx/comparator.h	Wed Feb 27 10:35:42 2013 +0100
     2.2 +++ b/ucx/comparator.h	Wed Feb 27 10:57:40 2013 +0100
     2.3 @@ -38,6 +38,28 @@
     2.4  int ucx_intcmp(void *i1, void *i2, void *data);
     2.5  
     2.6  /**
     2.7 + * Compares two real numbers of type float.
     2.8 + * @param f1 pointer to float one
     2.9 + * @param f2 pointer to float two
    2.10 + * @param if provided: a pointer to precision (default: 1e-6f)
    2.11 + * @return -1, if *f1 is less than *f2, 0 if both are equal,
    2.12 + * 1 if *f1 is greater than *f2
    2.13 + */
    2.14 +
    2.15 +int ucx_floatcmp(void *f1, void *f2, void *data);
    2.16 +
    2.17 +/**
    2.18 + * Compares two real numbers of type double.
    2.19 + * @param f1 pointer to double one
    2.20 + * @param f2 pointer to double two
    2.21 +* @param if provided: a pointer to precision (default: 1e-14)
    2.22 + * @return -1, if *d1 is less than *d2, 0 if both are equal,
    2.23 + * 1 if *d1 is greater than *d2
    2.24 + */
    2.25 +
    2.26 +int ucx_doublecmp(void *d1, void *d2, void *data);
    2.27 +
    2.28 +/**
    2.29   * Compares two pointers.
    2.30   * @param ptr1 pointer one
    2.31   * @param ptr2 pointer two

mercurial