mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
Merge pull request #454 from Alanscut/float-compare
comparing double value with DBL_EPSILON
This commit is contained in:
14
cJSON.c
14
cJSON.c
@ -43,6 +43,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
#include <ctype.h>
|
||||
#include <float.h>
|
||||
|
||||
#ifdef ENABLE_LOCALES
|
||||
#include <locale.h>
|
||||
@ -68,6 +69,14 @@
|
||||
#endif
|
||||
#define false ((cJSON_bool)0)
|
||||
|
||||
/* define isnan and isinf for ANSI C, if in C99 or above, isnan and isinf has been defined in math.h */
|
||||
#ifndef isinf
|
||||
#define isinf(d) (isnan((d - d)) && !isnan(d))
|
||||
#endif
|
||||
#ifndef isnan
|
||||
#define isnan(d) (d != d)
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
const unsigned char *json;
|
||||
size_t position;
|
||||
@ -522,7 +531,8 @@ static void update_offset(printbuffer * const buffer)
|
||||
/* 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);
|
||||
}
|
||||
|
||||
/* Render the number nicely from the given item into a string. */
|
||||
@ -542,7 +552,7 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
|
||||
}
|
||||
|
||||
/* This checks for NaN and Infinity */
|
||||
if ((d * 0) != 0)
|
||||
if (isnan(d) || isinf(d))
|
||||
{
|
||||
length = sprintf((char*)number_buffer, "null");
|
||||
}
|
||||
|
Reference in New Issue
Block a user