Added small utility memcmp_double()
This commit is contained in:
1 parent
547018d3c7
commit
20968d9c0a
6 files changed
+121
-26
No files matched your search
+2
-1
@@ -302,7 +302,8 @@ read_grid(const char *fname);
|
||||
|
||||
|
||||
|
||||
bool
|
||||
|
||||
bool
|
||||
grid_equal(const struct UnstructuredGrid * grid1 , const struct UnstructuredGrid * grid2);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
+1
-25
@@ -19,6 +19,7 @@
|
||||
|
||||
#include "config.h"
|
||||
#include <opm/core/grid.h>
|
||||
#include <opm/core/utility/memcmp_double.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
@@ -542,31 +543,6 @@ read_grid(const char *fname)
|
||||
return G;
|
||||
}
|
||||
|
||||
/**
|
||||
Ahhh - the joys of comparing floating point numbers ....
|
||||
|
||||
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
|
||||
*/
|
||||
static 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;
|
||||
|
||||
for (size_t 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#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;
|
||||
|
||||
for (size_t 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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
Will compare all the elements in the two double pointers p1 and
|
||||
p2. If all elements are 'sufficiently' the function will return 0,
|
||||
otherwise it will return 1.
|
||||
*/
|
||||
|
||||
#ifndef MEMCMP_DOUBLE_H
|
||||
#define MEMCMP_DOUBLE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
int memcmp_double(const double * p1 , const double *p2 , size_t num_elements);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user