Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party CJSON
提交
23e4fbc6
T
Third Party CJSON
项目概览
OpenHarmony
/
Third Party CJSON
大约 1 年 前同步成功
通知
6
Star
22
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
Third Party CJSON
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
23e4fbc6
编写于
4月 02, 2020
作者:
A
Alan Wang
提交者:
GitHub
4月 02, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #454 from Alanscut/float-compare
comparing double value with DBL_EPSILON
上级
65578af8
4e114c1f
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
20 addition
and
9 deletion
+20
-9
Makefile
Makefile
+1
-1
cJSON.c
cJSON.c
+12
-2
cJSON.h
cJSON.h
+0
-5
cJSON_Utils.c
cJSON_Utils.c
+4
-1
tests/compare_tests.c
tests/compare_tests.c
+3
-0
未找到文件。
Makefile
浏览文件 @
23e4fbc6
...
...
@@ -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'
)
...
...
cJSON.c
浏览文件 @
23e4fbc6
...
...
@@ -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"
);
}
...
...
cJSON.h
浏览文件 @
23e4fbc6
...
...
@@ -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
);
...
...
cJSON_Utils.c
浏览文件 @
23e4fbc6
...
...
@@ -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
);
}
...
...
tests/compare_tests.c
浏览文件 @
23e4fbc6
...
...
@@ -64,6 +64,9 @@ static void cjson_compare_should_compare_numbers(void)
TEST_ASSERT_TRUE
(
compare_from_string
(
"1"
,
"1"
,
false
));
TEST_ASSERT_TRUE
(
compare_from_string
(
"0.0001"
,
"0.0001"
,
true
));
TEST_ASSERT_TRUE
(
compare_from_string
(
"0.0001"
,
"0.0001"
,
false
));
TEST_ASSERT_TRUE
(
compare_from_string
(
"1E100"
,
"10E99"
,
false
));
TEST_ASSERT_FALSE
(
compare_from_string
(
"0.5E-100"
,
"0.5E-101"
,
false
));
TEST_ASSERT_FALSE
(
compare_from_string
(
"1"
,
"2"
,
true
));
TEST_ASSERT_FALSE
(
compare_from_string
(
"1"
,
"2"
,
false
));
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录