comparing double value with DBL_EPSILON

This commit is contained in:
Alanscut
2020-04-02 16:02:24 +08:00
parent 3ece4c893c
commit 8943c73345
4 changed files with 17 additions and 9 deletions

View File

@@ -40,6 +40,8 @@
#include <stdio.h>
#include <limits.h>
#include <math.h>
#include <float.h>
#include <math.h>
#if defined(_MSC_VER)
#pragma warning (pop)
@@ -109,7 +111,8 @@ static int compare_strings(const unsigned char *string1, const unsigned char *st
/* securely comparison of floating-point variables */
static cJSON_bool compare_double(double a, double b)
{
return (fabs(a - b) <= CJSON_DOUBLE_PRECISION);
double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b);
return (fabs(a - b) <= maxVal * DBL_EPSILON);
}