C++ 双精度或浮点数总和的 EXPECT_EQ 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15128510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error with EXPECT_EQ for sum of double or float
提问by suma
I am unable to understand why the test case failed in case of summing double numbers or floats. It works very finely for the integer data type.
我无法理解为什么测试用例在对双数或浮点数求和的情况下失败。它非常适用于整数数据类型。
//the method in simple_method.h
//simple_method.h中的方法
double sum ( double a, double b)
{
double res = a+b;
return res;
}
// the test case for this method
// 此方法的测试用例
TEST(simpleSum, sumOfFloat)
{
EXPECT_EQ(4.56, sum(0.56, 4.0));
}
// the output is
// 输出是
Running main() from gtest_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from simpleSum
[ RUN ] simpleSum.sumOfFloat
/home/pcadmin/Desktop/so/so3/simple_method_test.cpp:7: Failure
Value of: sum(0.56, 4.0)
Actual: 4.56
Expected: 4.56
[ FAILED ] simpleSum.sumOfFloat (0 ms)
[----------] 1 test from simpleSum (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] simpleSum.sumOfFloat
1 FAILED TEST
回答by congusbongus
Use EXPECT_NEAR
or the DoubleEq
matcher instead. Floating point operations can lead to rounding errorswhich makes the results ever so slightly different.
改用EXPECT_NEAR
或DoubleEq
匹配器。浮点运算会导致舍入错误,从而使结果略有不同。
回答by Jinuk Kim
See documentation for Floating Point Comparison
请参阅浮点比较的文档
EXPECT_EQ
uses exact match.
But you cannot match two floating numbers exactly. (at least with ease.)
EXPECT_EQ
使用精确匹配。但是你不能完全匹配两个浮点数。(至少很轻松。)
You can use EXPECT_FLOAT_EQ
or EXPECT_DOUBLE_EQ
. (with heuristic bounds)
Also, you may use EXPECT_NEAR
with specific bounds.
您可以使用EXPECT_FLOAT_EQ
或EXPECT_DOUBLE_EQ
。(使用启发式边界)此外,您可以使用EXPECT_NEAR
特定边界。
回答by Syaiful Nizam Yahya
From https://testing.googleblog.com/2008/10/tott-floating-point-comparison.html
来自https://testing.googleblog.com/2008/10/tott-floating-point-comparison.html
When comparing floating-point values, checking for equality might lead to unexpected results. Rounding errors can lead to a result that is close to the expected one, but not equal. As a consequence, an assertion might fail when checking for equality of two floating-point quantities even if the program is implemented correctly.
The Google C++ Testing Framework provides functions for comparing two floating-point quantities up to a given precision.
比较浮点值时,检查相等性可能会导致意外结果。舍入误差可能导致结果接近预期,但不相等。因此,即使程序正确实施,在检查两个浮点量是否相等时,断言也可能会失败。
Google C++ 测试框架提供了在给定精度下比较两个浮点量的函数。
ASSERT_FLOAT_EQ(expected, actual);
ASSERT_DOUBLE_EQ(expected, actual);
EXPECT_FLOAT_EQ(expected, actual);
EXPECT_DOUBLE_EQ(expected, actual);
In your case,
在你的情况下,
TEST(simpleSum, sumOfFloat)
{
EXPECT_DOUBLE_EQ(4.56, sum(0.56, 4.0));
}
回答by stingcoder
This is just a bug in Googletest. Text output should prove the failure, but format of it is not specified precisely.
这只是 Googletest 中的一个错误。文本输出应该证明失败,但没有准确指定它的格式。