Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party Unity
提交
a6d3f3a5
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看板
提交
a6d3f3a5
编写于
11月 07, 2017
作者:
J
John Lindgren
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Restore round-to-even behavior.
上级
2d4e32cd
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
56 addition
and
33 deletion
+56
-33
src/unity.c
src/unity.c
+14
-5
test/tests/testunity.c
test/tests/testunity.c
+42
-28
未找到文件。
src/unity.c
浏览文件 @
a6d3f3a5
...
@@ -292,7 +292,7 @@ void UnityPrintFloat(const UNITY_DOUBLE input_number)
...
@@ -292,7 +292,7 @@ void UnityPrintFloat(const UNITY_DOUBLE input_number)
else
if
(
isinf
(
number
))
UnityPrint
(
"inf"
);
else
if
(
isinf
(
number
))
UnityPrint
(
"inf"
);
else
else
{
{
UNITY_INT32
n
=
0
;
UNITY_INT32
n
_int
=
0
,
n
;
int
exponent
=
0
;
int
exponent
=
0
;
int
decimals
,
digits
;
int
decimals
,
digits
;
char
buf
[
16
];
char
buf
[
16
];
...
@@ -330,16 +330,25 @@ void UnityPrintFloat(const UNITY_DOUBLE input_number)
...
@@ -330,16 +330,25 @@ void UnityPrintFloat(const UNITY_DOUBLE input_number)
* freeing up significant bits in the fractional part.
* freeing up significant bits in the fractional part.
*/
*/
UNITY_DOUBLE
factor
=
1
.
0
f
;
UNITY_DOUBLE
factor
=
1
.
0
f
;
n
=
(
UNITY_INT32
)
number
;
n
_int
=
(
UNITY_INT32
)
number
;
number
-=
(
UNITY_DOUBLE
)
n
;
number
-=
(
UNITY_DOUBLE
)
n
_int
;
while
(
n
<
min_scaled
)
{
n
*=
10
;
factor
*=
10
.
0
f
;
exponent
--
;
}
while
(
n
_int
<
min_scaled
)
{
n_int
*=
10
;
factor
*=
10
.
0
f
;
exponent
--
;
}
number
*=
factor
;
number
*=
factor
;
}
}
/* round to nearest integer */
/* round to nearest integer */
n
+=
((
UNITY_INT32
)(
number
+
number
)
+
1
)
/
2
;
n
=
((
UNITY_INT32
)(
number
+
number
)
+
1
)
/
2
;
#ifndef UNITY_ROUND_TIES_AWAY_FROM_ZERO
/* round to even if exactly between two integers */
if
((
n
&
1
)
&&
((
UNITY_DOUBLE
)
n
-
number
==
0
.
5
f
))
n
--
;
#endif
n
+=
n_int
;
if
(
n
>=
max_scaled
)
if
(
n
>=
max_scaled
)
{
{
n
=
min_scaled
;
n
=
min_scaled
;
...
...
test/tests/testunity.c
浏览文件 @
a6d3f3a5
...
@@ -4468,7 +4468,6 @@ void testFloatPrinting(void)
...
@@ -4468,7 +4468,6 @@ void testFloatPrinting(void)
#else
#else
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"0"
,
0
.
0
f
);
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
(
"4.99e-07"
,
0
.
0000004
99
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"5.000001e-07"
,
0
.
00000050000005
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"0.1004695"
,
0
.
100469499
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"0.1004695"
,
0
.
100469499
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"2"
,
1
.
9999995
f
);
/*Rounding to int place*/
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"
,
1
.
0
f
);
...
@@ -4481,7 +4480,6 @@ void testFloatPrinting(void)
...
@@ -4481,7 +4480,6 @@ void testFloatPrinting(void)
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-0"
,
-
0
.
0
f
);
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
(
"-4.99e-07"
,
-
0
.
0000004
99
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-5.000001e-07"
,
-
0
.
00000050000005
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-0.1004695"
,
-
0
.
100469499
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-0.1004695"
,
-
0
.
100469499
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-2"
,
-
1
.
9999995
f
);
/*Rounding to int place*/
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"
,
-
1
.
0
f
);
...
@@ -4512,6 +4510,25 @@ void testFloatPrinting(void)
...
@@ -4512,6 +4510,25 @@ void testFloatPrinting(void)
#endif
#endif
}
}
void
testFloatPrintingRoundTiesToEven
(
void
)
{
#if defined(UNITY_EXCLUDE_FLOAT_PRINT) || defined(UNITY_INCLUDE_DOUBLE) || !defined(USING_OUTPUT_SPY)
TEST_IGNORE
();
#else
#ifdef UNITY_ROUND_TIES_AWAY_FROM_ZERO
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"0.0004882813"
,
0
.
0004
8828125
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"488281.3"
,
488281
.
25
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"5.000001e-07"
,
0
.
00000050000005
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-5.000001e-07"
,
-
0
.
00000050000005
f
);
#else
/* Default to Round ties to even */
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"0.0004882812"
,
0
.
0004
8828125
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"488281.2"
,
488281
.
25
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"5e-07"
,
0
.
00000050000005
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"-5e-07"
,
-
0
.
00000050000005
f
);
#endif
#endif
}
void
testFloatPrintingInfinityAndNaN
(
void
)
void
testFloatPrintingInfinityAndNaN
(
void
)
{
{
#if defined(UNITY_EXCLUDE_FLOAT_PRINT) || !defined(USING_OUTPUT_SPY)
#if defined(UNITY_EXCLUDE_FLOAT_PRINT) || !defined(USING_OUTPUT_SPY)
...
@@ -4529,31 +4546,15 @@ void testFloatPrintingInfinityAndNaN(void)
...
@@ -4529,31 +4546,15 @@ void testFloatPrintingInfinityAndNaN(void)
static
void
printFloatValue
(
float
f
)
static
void
printFloatValue
(
float
f
)
{
{
char
expected
[
18
];
char
expected
[
18
];
char
expected_lower
[
18
];
char
expected_higher
[
18
];
startPutcharSpy
();
startPutcharSpy
();
UnityPrintFloat
(
f
);
UnityPrintFloat
(
f
);
sprintf
(
expected
,
"%.9g"
,
f
);
sprintf
(
expected
,
"%.9g"
,
f
);
/* We print all NaN's as "nan", not "-nan" */
/* We print all NaN's as "nan", not "-nan" */
if
(
strcmp
(
expected
,
"-nan"
)
==
0
)
strcpy
(
expected
,
"nan"
);
if
(
strcmp
(
expected
,
"-nan"
)
==
0
)
strcpy
(
expected
,
"nan"
);
strcpy
(
expected_lower
,
expected
);
strcpy
(
expected_higher
,
expected
);
/* Allow for rounding differences in the last digit */
double
lower
=
(
double
)
f
*
0
.
9999999995
;
double
higher
=
(
double
)
f
*
1
.
0000000005
;
if
(
isfinite
(
lower
))
sprintf
(
expected_lower
,
"%.9g"
,
lower
);
if
(
isfinite
(
higher
))
sprintf
(
expected_higher
,
"%.9g"
,
higher
);
if
(
strcmp
(
expected
,
getBufferPutcharSpy
())
!=
0
&&
if
(
strcmp
(
expected
,
getBufferPutcharSpy
()))
strcmp
(
expected_lower
,
getBufferPutcharSpy
())
!=
0
&&
strcmp
(
expected_higher
,
getBufferPutcharSpy
())
!=
0
)
{
{
/* Fail with diagnostic printing */
/* Fail with diagnostic printing */
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
expected
,
f
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
expected
,
f
);
...
@@ -4571,13 +4572,11 @@ static void printFloatValue(float f)
...
@@ -4571,13 +4572,11 @@ static void printFloatValue(float f)
char
expected_higher3
[
18
];
char
expected_higher3
[
18
];
startPutcharSpy
();
startPutcharSpy
();
UnityPrintFloat
(
f
);
UnityPrintFloat
(
f
);
sprintf
(
expected
,
"%.7g"
,
f
);
sprintf
(
expected
,
"%.7g"
,
f
);
/* We print all NaN's as "nan", not "-nan" */
/* We print all NaN's as "nan", not "-nan" */
if
(
strcmp
(
expected
,
"-nan"
)
==
0
)
strcpy
(
expected
,
"nan"
);
if
(
strcmp
(
expected
,
"-nan"
)
==
0
)
strcpy
(
expected
,
"nan"
);
strcpy
(
expected_lower
,
expected
);
strcpy
(
expected_lower
,
expected
);
strcpy
(
expected_lower2
,
expected
);
strcpy
(
expected_lower2
,
expected
);
...
@@ -4594,17 +4593,17 @@ static void printFloatValue(float f)
...
@@ -4594,17 +4593,17 @@ static void printFloatValue(float f)
if
(
isfinite
(
higher
))
sprintf
(
expected_higher
,
"%.7g"
,
higher
);
if
(
isfinite
(
higher
))
sprintf
(
expected_higher
,
"%.7g"
,
higher
);
/* Outside [1,10000000] allow for relative error of +/-2.5e-7 */
/* Outside [1,10000000] allow for relative error of +/-2.5e-7 */
if
(
f
<
1
.
0
||
f
>
10000000
)
if
(
f
<
1
.
0
||
f
>
10000000
)
{
{
double
lower2
=
(
double
)
f
*
0
.
99999985
;
double
lower2
=
(
double
)
f
*
0
.
99999985
;
double
lower3
=
(
double
)
f
*
0
.
99999975
;
double
lower3
=
(
double
)
f
*
0
.
99999975
;
double
higher2
=
(
double
)
f
*
1
.
00000015
;
double
higher2
=
(
double
)
f
*
1
.
00000015
;
double
higher3
=
(
double
)
f
*
1
.
00000025
;
double
higher3
=
(
double
)
f
*
1
.
00000025
;
if
(
isfinite
(
lower2
))
sprintf
(
expected_lower2
,
"%.7g"
,
lower2
);
if
(
isfinite
(
lower2
))
sprintf
(
expected_lower2
,
"%.7g"
,
lower2
);
if
(
isfinite
(
lower3
))
sprintf
(
expected_lower3
,
"%.7g"
,
lower3
);
if
(
isfinite
(
lower3
))
sprintf
(
expected_lower3
,
"%.7g"
,
lower3
);
if
(
isfinite
(
higher2
))
sprintf
(
expected_higher2
,
"%.7g"
,
higher2
);
if
(
isfinite
(
higher2
))
sprintf
(
expected_higher2
,
"%.7g"
,
higher2
);
if
(
isfinite
(
higher3
))
sprintf
(
expected_higher3
,
"%.7g"
,
higher3
);
if
(
isfinite
(
higher3
))
sprintf
(
expected_higher3
,
"%.7g"
,
higher3
);
}
}
if
(
strcmp
(
expected
,
getBufferPutcharSpy
())
!=
0
&&
if
(
strcmp
(
expected
,
getBufferPutcharSpy
())
!=
0
&&
...
@@ -5362,6 +5361,21 @@ void testDoublePrinting(void)
...
@@ -5362,6 +5361,21 @@ void testDoublePrinting(void)
#endif
#endif
}
}
void
testDoublePrintingRoundTiesToEven
(
void
)
{
#if defined(UNITY_EXCLUDE_FLOAT_PRINT) || defined(UNITY_EXCLUDE_DOUBLE) || !defined(USING_OUTPUT_SPY)
TEST_IGNORE
();
#else
#ifdef UNITY_ROUND_TIES_AWAY_FROM_ZERO
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"1.00000001e+10"
,
10000000050
.
0
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"9.00719925e+15"
,
9007199245000000
.
0
);
#else
/* Default to Round ties to even */
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"1e+10"
,
10000000050
.
0
);
TEST_ASSERT_EQUAL_PRINT_FLOATING
(
"9.00719924e+15"
,
9007199245000000
.
0
);
#endif
#endif
}
void
testDoublePrintingInfinityAndNaN
(
void
)
void
testDoublePrintingInfinityAndNaN
(
void
)
{
{
#if defined(UNITY_EXCLUDE_FLOAT_PRINT) || defined(UNITY_EXCLUDE_DOUBLE) || !defined(USING_OUTPUT_SPY)
#if defined(UNITY_EXCLUDE_FLOAT_PRINT) || defined(UNITY_EXCLUDE_DOUBLE) || !defined(USING_OUTPUT_SPY)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录