Files
opm-core/opm/core/utility/memcmp_double.c
T
Markus Blatt 7db80ba2c6 Fix complaint about initial declaration in foor loop.
gcc-4.7.2 (Debian 4.7.2-5) complained about:
"‘for’ loop initial declarations are only allowed in C99 mode
note: use option -std=c99 or -std=gnu99 to compile your code"
when seeing a loop like
for(int i=0; i<end; ++i)

This is fixed by moving the declaration before the for loop with
this commit. Altenatively, we could use the above option.
2015-09-03 14:03:25 +02:00

31 lines
819 B
C

#include <stdlib.h>
#include <math.h>
#include <string.h>
/**
Ahhh - the joys of comparing floating point numbers ....
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
*/
int memcmp_double(const double * p1 , const double *p2 , size_t num_elements) {
if (memcmp(p1 , p2 , num_elements * sizeof * p1) == 0)
return 0;
else {
const double abs_epsilon = 1e-8;
const double rel_epsilon = 1e-5;
size_t index;
for (index = 0; index < num_elements; index++) {
double diff = fabs(p1[index] - p2[index]);
if (diff > abs_epsilon) {
double sum = fabs(p1[index]) + fabs(p2[index]);
if (diff > sum * rel_epsilon)
return 1;
}
}
return 0;
}
}