Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party CJSON
提交
87fad25c
T
Third Party CJSON
项目概览
OpenHarmony
/
Third Party CJSON
11 个月 前同步成功
通知
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,发现更多精彩内容 >>
提交
87fad25c
编写于
4月 29, 2017
作者:
M
Max Bruckner
提交者:
GitHub
4月 29, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #153 from DeboraG/print_number_prec
cJSON: Fix print_number to print significant digits of doubles
上级
28570ad0
a69ad226
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
18 addition
and
61 deletion
+18
-61
cJSON.c
cJSON.c
+11
-44
tests/print_number.c
tests/print_number.c
+7
-17
未找到文件。
cJSON.c
浏览文件 @
87fad25c
...
...
@@ -415,27 +415,6 @@ static void update_offset(printbuffer * const buffer)
buffer
->
offset
+=
strlen
((
const
char
*
)
buffer_pointer
);
}
/* Removes trailing zeroes from the end of a printed number */
static
int
trim_trailing_zeroes
(
const
unsigned
char
*
const
number
,
int
length
,
const
unsigned
char
decimal_point
)
{
if
((
number
==
NULL
)
||
(
length
<=
0
))
{
return
-
1
;
}
while
((
length
>
0
)
&&
(
number
[
length
-
1
]
==
'0'
))
{
length
--
;
}
if
((
length
>
0
)
&&
(
number
[
length
-
1
]
==
decimal_point
))
{
/* remove trailing decimal_point */
length
--
;
}
return
length
;
}
/* Render the number nicely from the given item into a string. */
static
cJSON_bool
print_number
(
const
cJSON
*
const
item
,
printbuffer
*
const
output_buffer
)
{
...
...
@@ -443,9 +422,9 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
double
d
=
item
->
valuedouble
;
int
length
=
0
;
size_t
i
=
0
;
cJSON_bool
trim_zeroes
=
true
;
/* should zeroes at the end be removed? */
unsigned
char
number_buffer
[
64
];
/* temporary buffer to print the number into */
unsigned
char
number_buffer
[
26
];
/* temporary buffer to print the number into */
unsigned
char
decimal_point
=
get_decimal_point
();
double
test
;
if
(
output_buffer
==
NULL
)
{
...
...
@@ -457,20 +436,17 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
{
length
=
sprintf
((
char
*
)
number_buffer
,
"null"
);
}
else
if
((
fabs
(
floor
(
d
)
-
d
)
<=
DBL_EPSILON
)
&&
(
fabs
(
d
)
<
1.0e60
))
{
/* integer */
length
=
sprintf
((
char
*
)
number_buffer
,
"%.0f"
,
d
);
trim_zeroes
=
false
;
/* don't remove zeroes for "big integers" */
}
else
if
((
fabs
(
d
)
<
1.0e-6
)
||
(
fabs
(
d
)
>
1.0e9
))
{
length
=
sprintf
((
char
*
)
number_buffer
,
"%e"
,
d
);
trim_zeroes
=
false
;
/* don't remove zeroes in engineering notation */
}
else
{
length
=
sprintf
((
char
*
)
number_buffer
,
"%f"
,
d
);
/* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */
length
=
sprintf
((
char
*
)
number_buffer
,
"%1.15g"
,
d
);
/* Check whether the original double can be recovered */
if
((
sscanf
((
char
*
)
number_buffer
,
"%lg"
,
&
test
)
!=
1
)
||
((
double
)
test
!=
d
))
{
/* If not, print with 17 decimal places of precision */
length
=
sprintf
((
char
*
)
number_buffer
,
"%1.17g"
,
d
);
}
}
/* sprintf failed or buffer overrun occured */
...
...
@@ -479,15 +455,6 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
return
false
;
}
if
(
trim_zeroes
)
{
length
=
trim_trailing_zeroes
(
number_buffer
,
length
,
decimal_point
);
if
(
length
<=
0
)
{
return
false
;
}
}
/* reserve appropriate space in the output */
output_pointer
=
ensure
(
output_buffer
,
(
size_t
)
length
);
if
(
output_pointer
==
NULL
)
...
...
tests/print_number.c
浏览文件 @
87fad25c
...
...
@@ -64,19 +64,19 @@ static void print_number_should_print_positive_integers(void)
static
void
print_number_should_print_positive_reals
(
void
)
{
assert_print_number
(
"0.123"
,
0
.
123
);
assert_print_number
(
"1
.000000
e-09"
,
10e-10
);
assert_print_number
(
"1e-09"
,
10e-10
);
assert_print_number
(
"1000000000000"
,
10e11
);
assert_print_number
(
"1.23
0000
e+129"
,
123e+127
);
assert_print_number
(
"
0"
,
123e-128
);
/* TODO: Maybe this shouldn't be 0 */
assert_print_number
(
"1.23e+129"
,
123e+127
);
assert_print_number
(
"
1.23e-126"
,
123e-128
);
}
static
void
print_number_should_print_negative_reals
(
void
)
{
assert_print_number
(
"-0.0123"
,
-
0
.
0123
);
assert_print_number
(
"-1
.000000
e-09"
,
-
10e-10
);
assert_print_number
(
"-1
000000000000000000000
"
,
-
10e20
);
assert_print_number
(
"-1.23
0000
e+129"
,
-
123e+127
);
assert_print_number
(
"-1.23
0000
e-126"
,
-
123e-128
);
assert_print_number
(
"-1e-09"
,
-
10e-10
);
assert_print_number
(
"-1
e+21
"
,
-
10e20
);
assert_print_number
(
"-1.23e+129"
,
-
123e+127
);
assert_print_number
(
"-1.23e-126"
,
-
123e-128
);
}
static
void
print_number_should_print_non_number
(
void
)
...
...
@@ -88,15 +88,6 @@ static void print_number_should_print_non_number(void)
/* assert_print_number("null", -INFTY); */
}
static
void
trim_trailing_zeroes_should_trim_trailing_zeroes
(
void
)
{
TEST_ASSERT_EQUAL_INT
(
2
,
trim_trailing_zeroes
((
const
unsigned
char
*
)
"10.00"
,
(
int
)(
sizeof
(
"10.00"
)
-
1
),
'.'
));
TEST_ASSERT_EQUAL_INT
(
0
,
trim_trailing_zeroes
((
const
unsigned
char
*
)
".00"
,
(
int
)(
sizeof
(
".00"
)
-
1
),
'.'
));
TEST_ASSERT_EQUAL_INT
(
0
,
trim_trailing_zeroes
((
const
unsigned
char
*
)
"00"
,
(
int
)(
sizeof
(
"00"
)
-
1
),
'.'
));
TEST_ASSERT_EQUAL_INT
(
-
1
,
trim_trailing_zeroes
(
NULL
,
10
,
'.'
));
TEST_ASSERT_EQUAL_INT
(
-
1
,
trim_trailing_zeroes
((
const
unsigned
char
*
)
""
,
0
,
'.'
));
}
int
main
(
void
)
{
/* initialize cJSON item */
...
...
@@ -108,7 +99,6 @@ int main(void)
RUN_TEST
(
print_number_should_print_positive_reals
);
RUN_TEST
(
print_number_should_print_negative_reals
);
RUN_TEST
(
print_number_should_print_non_number
);
RUN_TEST
(
trim_trailing_zeroes_should_trim_trailing_zeroes
);
return
UNITY_END
();
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录