Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party Unity
提交
8ff74d60
T
Third Party Unity
项目概览
OpenHarmony
/
Third Party Unity
1 年多 前同步成功
通知
36
Star
144
Fork
2
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
Third Party Unity
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
8ff74d60
编写于
9月 14, 2017
作者:
J
John Lindgren
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Allow UnityPrintFloat() to print a 7th digit.
上级
774da10e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
105 addition
and
64 deletion
+105
-64
src/unity.c
src/unity.c
+36
-13
test/tests/testunity.c
test/tests/testunity.c
+69
-51
未找到文件。
src/unity.c
浏览文件 @
8ff74d60
...
...
@@ -258,11 +258,14 @@ void UnityPrintMask(const UNITY_UINT mask, const UNITY_UINT number)
/*-----------------------------------------------*/
#ifndef UNITY_EXCLUDE_FLOAT_PRINT
/* This function prints a floating-point value in a format similar to
* printf("%.6g"). It can work with either single- or double-precision,
* but for simplicity, it prints only 6 significant digits in either case.
* Printing more than 6 digits accurately is hard (at least in the single-
* precision case) and isn't attempted here. */
/*
* This function prints a floating-point value in a format similar to
* printf("%.7g"). It can work with either single- or double-precision,
* but for simplicity, it prints only 7 significant digits in either case.
* The 7th digit won't always be totally correct in single-precision
* operation (for that level of accuracy, a more complicated algorithm
* would be needed).
*/
void
UnityPrintFloat
(
const
UNITY_DOUBLE
input_number
)
{
UNITY_DOUBLE
number
=
input_number
;
...
...
@@ -285,22 +288,42 @@ void UnityPrintFloat(const UNITY_DOUBLE input_number)
UNITY_INT32
n
;
char
buf
[
16
];
/* scale up or down by powers of 10 */
while
(
number
<
100000
.
0
f
/
1e6
f
)
{
number
*=
1e6
f
;
exponent
-=
6
;
}
while
(
number
<
100000
.
0
f
)
{
number
*=
10
.
0
f
;
exponent
--
;
}
while
(
number
>
1000000
.
0
f
*
1e6
f
)
{
number
/=
1e6
f
;
exponent
+=
6
;
}
while
(
number
>
1000000
.
0
f
)
{
number
/=
10
.
0
f
;
exponent
++
;
}
/*
* Scale up or down by powers of 10. To minimize rounding error,
* start with a factor/divisor of 10^10, which is the largest
* power of 10 that can be represented exactly. Finally, compute
* (exactly) the remaining power of 10 and perform one more
* multiplication or division.
*/
if
(
number
<
1e6
f
)
{
UNITY_DOUBLE
factor
=
1
.
0
f
;
while
(
number
<
1e7
f
/
1e10
f
)
{
number
*=
1e10
f
;
exponent
-=
10
;
}
while
(
number
*
factor
<
1e6
f
)
{
factor
*=
10
.
0
f
;
exponent
--
;
}
number
*=
factor
;
}
else
if
(
number
>
1e7
f
)
{
UNITY_DOUBLE
divisor
=
1
.
0
f
;
while
(
number
>
1e6
f
*
1e10
f
)
{
number
/=
1e10
f
;
exponent
+=
10
;
}
while
(
number
/
divisor
>
1e7
f
)
{
divisor
*=
10
.
0
f
;
exponent
++
;
}
number
/=
divisor
;
}
/* round to nearest integer */
n
=
((
UNITY_INT32
)(
number
+
number
)
+
1
)
/
2
;
if
(
n
>
999999
)
if
(
n
>
999999
9
)
{
n
=
100000
;
n
=
100000
0
;
exponent
++
;
}
/* determine where to place decimal point */
decimals
=
(
exponent
<=
0
&&
exponent
>=
-
9
)
?
-
exponent
:
5
;
decimals
=
(
exponent
<=
0
&&
exponent
>=
-
10
)
?
-
exponent
:
6
;
exponent
+=
decimals
;
/* truncate trailing zeroes after decimal point */
...
...
test/tests/testunity.c
浏览文件 @
8ff74d60
...
...
@@ -4466,46 +4466,48 @@ void testFloatPrinting(void)
#if defined(UNITY_EXCLUDE_FLOAT_PRINT) || !defined(USING_OUTPUT_SPY)
TEST_IGNORE
();
#else
/* Some failures are expected due to differences in the last digit
* if UnityPrintFloat uses single-precision calculations. */
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"0"
,
0
.
0
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"4.99e-07"
,
0
.
0000004
99
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"5e-07"
,
0
.
0000005
0000005
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"0.100469
"
,
0
.
100469499
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"
1"
,
0
.
9999995
f
);
/*Rounding to int place*/
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"5e-07"
,
0
.
0000005
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"0.100469
5"
,
0
.
1004695
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"
2"
,
1
.
9999995
f
);
/*Rounding to int place*/
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"1"
,
1
.
0
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"1.25"
,
1
.
25
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"7.99999
"
,
7
.
99999
f
);
/*Not rounding*/
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"16.000
2"
,
16
.
0002
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"16.000
4"
,
16
.
0004
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"16.000
6"
,
16
.
0006
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"999999
"
,
999999
.
0
f
);
/*Last full print integer*/
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"7.99999
9"
,
7
.
9
99999
f
);
/*Not rounding*/
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"16.000
02"
,
16
.
0
0002
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"16.000
04"
,
16
.
0
0004
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"16.000
06"
,
16
.
0
0006
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"999999
9"
,
9
999999
.
0
f
);
/*Last full print integer*/
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-0"
,
-
0
.
0
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-4.99e-07"
,
-
0
.
0000004
99
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-5e-07"
,
-
0
.
0000005
0000005
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-0.100469
"
,
-
0
.
100469499
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-
1"
,
-
0
.
9999995
f
);
/*Rounding to int place*/
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-5e-07"
,
-
0
.
0000005
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-0.100469
5"
,
-
0
.
1004695
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-
2"
,
-
1
.
9999995
f
);
/*Rounding to int place*/
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-1"
,
-
1
.
0
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-1.25"
,
-
1
.
25
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-7.99999
"
,
-
7
.
99999
f
);
/*Not rounding*/
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-16.000
2"
,
-
16
.
0002
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-16.000
4"
,
-
16
.
0004
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-16.000
6"
,
-
16
.
0006
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-999999
"
,
-
999999
.
0
f
);
/*Last full print integer*/
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"4.29497e+09"
,
4294967296
.
0
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"5e+09"
,
5000000000
.
0
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"8e+09"
,
8.0e+09
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"8.3
1e+09"
,
8309999104
.
0
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"1e+10"
,
1.0e+10
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"1e+10"
,
10000000000
.
0
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-7.99999
9"
,
-
7
.
9
99999
f
);
/*Not rounding*/
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-16.000
02"
,
-
16
.
0
0002
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-16.000
04"
,
-
16
.
0
0004
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-16.000
06"
,
-
16
.
0
0006
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-999999
9"
,
-
9
999999
.
0
f
);
/*Last full print integer*/
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"4.2949
6
7e+09"
,
4294967296
.
0
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"5e+09"
,
5000000000
.
0
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"8e+09"
,
8.0e+09
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"8.3
09999e+09"
,
8309999104
.
0
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"1e+10"
,
1.0e+10
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"1e+10"
,
10000000000
.
0
f
);
/* Some compilers have trouble with inexact float constants, a float cast works generally */
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"1.00005
e+10"
,
(
float
)
1.000054
e+10
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"1.1e+38"
,
(
float
)
1.10000005e+38
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"1.635
3e+10"
,
1.63529943e+10
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"3.40282e+38"
,
3.40282346638e38
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"1.00005
5e+10"
,
(
float
)
1.000055
e+10
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"1.1e+38"
,
(
float
)
1.10000005e+38
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"1.635
299e+10"
,
1.63529943e+10
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"3.40282
3
e+38"
,
3.40282346638e38
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-1e+10"
,
-
1.0e+10
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-3.40282e+38"
,
-
3.40282346638e38
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-1e+10"
,
-
1.0e+10
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-3.40282
3
e+38"
,
-
3.40282346638e38
f
);
#endif
}
...
...
@@ -4526,27 +4528,43 @@ static void printFloatValue(float f)
{
char
expected
[
18
];
char
expected_lower
[
18
];
char
expected_lower2
[
18
];
char
expected_lower3
[
18
];
char
expected_higher
[
18
];
char
expected_higher2
[
18
];
char
expected_higher3
[
18
];
startPutcharSpy
();
UnityPrintFloat
(
f
);
sprintf
(
expected
,
"%.
6
g"
,
f
);
sprintf
(
expected
,
"%.
7
g"
,
f
);
/* We print all NaN's as "nan", not "-nan" */
if
(
strcmp
(
expected
,
"-nan"
)
==
0
)
strcpy
(
expected
,
"nan"
);
/* Allow for rounding differences in last digit */
double
lower
=
(
double
)
f
*
0
.
9999995
;
double
higher
=
(
double
)
f
*
1
.
0000005
;
if
(
isfinite
(
lower
))
sprintf
(
expected_lower
,
"%.6g"
,
lower
);
else
strcpy
(
expected_lower
,
expected
);
if
(
isfinite
(
higher
))
sprintf
(
expected_higher
,
"%.6g"
,
higher
);
else
strcpy
(
expected_higher
,
expected
);
/* Allow for relative error of +/-2.5e-7 */
double
lower
=
(
double
)
f
*
0
.
99999995
;
double
lower2
=
(
double
)
f
*
0
.
99999985
;
double
lower3
=
(
double
)
f
*
0
.
99999975
;
double
higher
=
(
double
)
f
*
1
.
00000005
;
double
higher2
=
(
double
)
f
*
1
.
00000015
;
double
higher3
=
(
double
)
f
*
1
.
00000025
;
if
(
isfinite
(
lower
))
sprintf
(
expected_lower
,
"%.7g"
,
lower
);
else
strcpy
(
expected_lower
,
expected
);
if
(
isfinite
(
lower2
))
sprintf
(
expected_lower2
,
"%.7g"
,
lower2
);
else
strcpy
(
expected_lower2
,
expected
);
if
(
isfinite
(
lower3
))
sprintf
(
expected_lower3
,
"%.7g"
,
lower3
);
else
strcpy
(
expected_lower3
,
expected
);
if
(
isfinite
(
higher
))
sprintf
(
expected_higher
,
"%.7g"
,
higher
);
else
strcpy
(
expected_higher
,
expected
);
if
(
isfinite
(
higher2
))
sprintf
(
expected_higher2
,
"%.7g"
,
higher2
);
else
strcpy
(
expected_higher2
,
expected
);
if
(
isfinite
(
higher3
))
sprintf
(
expected_higher3
,
"%.7g"
,
higher3
);
else
strcpy
(
expected_higher3
,
expected
);
if
(
strcmp
(
expected
,
getBufferPutcharSpy
())
!=
0
&&
strcmp
(
expected_lower
,
getBufferPutcharSpy
())
!=
0
&&
strcmp
(
expected_higher
,
getBufferPutcharSpy
())
!=
0
)
strcmp
(
expected_lower2
,
getBufferPutcharSpy
())
!=
0
&&
strcmp
(
expected_lower3
,
getBufferPutcharSpy
())
!=
0
&&
strcmp
(
expected_higher
,
getBufferPutcharSpy
())
!=
0
&&
strcmp
(
expected_higher2
,
getBufferPutcharSpy
())
!=
0
&&
strcmp
(
expected_higher3
,
getBufferPutcharSpy
())
!=
0
)
{
/* Fail with diagnostic printing */
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
expected
,
f
);
...
...
@@ -5252,20 +5270,20 @@ void testDoublePrinting(void)
#if defined(UNITY_EXCLUDE_FLOAT_PRINT) || defined(UNITY_EXCLUDE_DOUBLE) || !defined(USING_OUTPUT_SPY)
TEST_IGNORE
();
#else
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"0.100469"
,
0
.
10046949999999999
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"4.29497e+09"
,
4294967295
.
999999
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"4.29497e+09"
,
4294967295
.
9999995
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"4.29497e+09"
,
4294967296
.
0
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"1e+10"
,
9999999995
.
0
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"9.007
2e+15"
,
9007199254740990
.
0
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"7e+100"
,
7.0e+100
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"3e+200"
,
3.0e+200
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"9.2345
7
e+300"
,
9.23456789e+300
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-0.100469"
,
-
0
.
10046949999999999
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-4.29497e+09"
,
-
4294967295
.
999999
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-4.29497e+09"
,
-
4294967295
.
9999995
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-7e+100"
,
-
7.0e+100
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"0.100469
5
"
,
0
.
10046949999999999
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"4.2949
6
7e+09"
,
4294967295
.
999999
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"4.2949
6
7e+09"
,
4294967295
.
9999995
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"4.2949
6
7e+09"
,
4294967296
.
0
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"1e+10"
,
9999999995
.
0
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"9.007
199e+15"
,
9007199254740990
.
0
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"7e+100"
,
7.0e+100
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"3e+200"
,
3.0e+200
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"9.2345
68
e+300"
,
9.23456789e+300
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-0.100469
5
"
,
-
0
.
10046949999999999
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-4.2949
6
7e+09"
,
-
4294967295
.
999999
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-4.2949
6
7e+09"
,
-
4294967295
.
9999995
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-7e+100"
,
-
7.0e+100
);
#endif
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录