]> git.feebdaed.xyz Git - 0xmirror/cJSON.git/commitdiff
comparing double value with DBL_EPSILON
authorAlanscut <wp_scut@163.com>
Thu, 2 Apr 2020 08:02:24 +0000 (16:02 +0800)
committerAlanscut <wp_scut@163.com>
Thu, 2 Apr 2020 08:02:24 +0000 (16:02 +0800)
Makefile
cJSON.c
cJSON.h
cJSON_Utils.c

index 08fcc22e9ddcede3fd9a7d5aca7df8e925d02bcd..0e58d5cef58ab9bfb55936e431d58682c7266eb5 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -37,7 +37,7 @@ else
 endif
 
 PIC_FLAGS = -fPIC
-R_CFLAGS = $(PIC_FLAGS) -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 -Wmissing-prototypes -Wstrict-overflow=2 -Wcast-qual -Wc++-compat -Wundef -Wswitch-default -Wconversion -Wfloat-equal $(CFLAGS)
+R_CFLAGS = $(PIC_FLAGS) -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 -Wmissing-prototypes -Wstrict-overflow=2 -Wcast-qual -Wc++-compat -Wundef -Wswitch-default -Wconversion $(CFLAGS)
 
 uname := $(shell sh -c 'uname -s 2>/dev/null || echo false')
 
diff --git a/cJSON.c b/cJSON.c
index b0e744e42b357458ff2c05eca76b80d58760aa49..9fc4ffbf139f5fde030188b194f4ac35307dbda5 100644 (file)
--- a/cJSON.c
+++ b/cJSON.c
@@ -43,6 +43,7 @@
 #include <stdlib.h>
 #include <limits.h>
 #include <ctype.h>
+#include <float.h>
 
 #ifdef ENABLE_LOCALES
 #include <locale.h>
 #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;
@@ -483,7 +492,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. */
@@ -503,7 +513,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");
     }
diff --git a/cJSON.h b/cJSON.h
index 2c535628ae10ee5545e37d9326f2cead981d24e9..753b1ad45827aaa91c19b3cebd6bdc523b3922a1 100644 (file)
--- a/cJSON.h
+++ b/cJSON.h
@@ -137,11 +137,6 @@ typedef int cJSON_bool;
 #define CJSON_NESTING_LIMIT 1000
 #endif
 
-/* Precision of double variables comparison */
-#ifndef CJSON_DOUBLE_PRECISION
-#define CJSON_DOUBLE_PRECISION .0000000000000001
-#endif
-
 /* returns the version of cJSON as a string */
 CJSON_PUBLIC(const char*) cJSON_Version(void);
 
index 3a535b1d3f938160931dc3869b75e8eced5e4170..c750f7fcbb0e0e037d19deeb4444063115c6e8a8 100644 (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);
 }