gpt4 book ai didi

c++ - 带有 float 的奇怪的谷歌测试失败

转载 作者:太空狗 更新时间:2023-10-29 21:41:58 26 4
gpt4 key购买 nike

我在使用 Google Test 1.7.0 测试浮点值是否相等时遇到了一些问题。

我的断言是这样的:

ASSERT_NEAR(124691356.375f, actual, DELTA);

测试失败,出现以下错误:

The difference between 124691356.375f and actual is 3.625, which exceeds DELTA, where 124691356.375f evaluates to 124691360, actual evaluates to 124691356.375, and DELTA evaluates to 0.0625.

这是怎么回事? 实际和预期结果明显在0.0625的允许误差范围内。为什么 gtest 以这种方式评估浮点文字 124691356.375f

更新:DELTAactual 是double 类型,期望值是float 字面量。如果我将文字更改为 double ,或将其他参数更改为 float (以便所有内容都属于同一类型),则测试通过。问题仍然存在 - 当类型不匹配时,是什么导致了这种行为?

最佳答案

124691356.375f 是一个单精度 float ,有 7-8 位有效小数位。于是“四舍五入”为124691360,不在允许误差范围内。您可能应该使用 124691356.375lf。另见 wikipediathis question .

关于c++ - 带有 float 的奇怪的谷歌测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27492740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com