【发布时间】:2017-08-21 10:58:52
【问题描述】:
在我的代码中,
float f = -0.0; // Negative
并与负零
进行比较f == -0.0f
结果将是true。
但是
float f = 0.0; // Positive
并与负零
进行比较f == -0.0f
另外,结果将是true 而不是false
为什么这两种情况的结果都是正确的?
这里是a MCVE to test it (live on coliru):
#include <iostream>
int main()
{
float f = -0.0;
std::cout<<"==== > " << f <<std::endl<<std::endl;
if(f == -0.0f)
{
std::cout<<"true"<<std::endl;
}
else
{
std::cout<<"false"<<std::endl;
}
}
输出:
==== > -0 // Here print negative zero
true
【问题讨论】:
-
-0.0 == 0.0是真的,但其他功能在这两者上有所不同。 What operations and functions on +0.0 and -0.0 give different arithmetic results?
标签: c++ floating-point ieee-754