如果运算符<适用于浮点类型,为什么我们不能使用它进行相等测试? [英] If operator< works properly for floating-point types, why can't we use it for equality testing?

查看:251
本文介绍了如果运算符<适用于浮点类型,为什么我们不能使用它进行相等测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正确测试两个浮点数是否相等是很多人,包括我,不完全理解。然而,今天,我想到了一些标准容器如何定义 operator 方面的等式。我总是看到有关平等问题的人,但从来没有与其他关系比较。甚至还有无声版本可供使用,其中包括除了平等和不平等之外的所有其他内容。 / p>

假设运算符< 正确运行,与 operator == ,为什么我们不能这样做:

  bool floateq(float a,float b){
/ / check NaN
return!(a< b)&&& !(b }

事实上,我做了一个额外的重载测试,看到这里,并且似乎有与将它们与 operator ==

  std :: cout< float-> double vs double:
<< floateq(static_cast< double>(0.7f),0.7)<
<< (static_cast< double>(0.7f)== 0.7)< \\\
;

输出:


float-> double vs double:0 0


我担心使用

解决方案

== < > < = > = !=



您似乎有一个 合理实现< 应该比较(double)0.7f等于0.7。不是这种情况。如果你把 0.7f 转换为 double ,你会得到 0x1.666666p- code>。但是, 0.7 等于 0x1.6666666666666p-1 。这些在数值上不相等;事实上,(double)0.7f 明显小于 0.7 ---这是可笑的等于。



使用浮点数时,重要的是要记住它们是浮点数,而不是实数或有理数或任何其他这样的东西。你必须考虑到他们的属性,而不是每个人都希望他们拥有的属性。这样做,你会自动避免使用浮点数的大多数常见的陷阱。


Properly testing two floating-point numbers for equality is something that a lot of people, including me, don't fully understand. Today, however, I thought about how some standard containers define equality in terms of operator<. I always see people with problems surrounding equality, but never with the other relational comparisons. There are even silent versions of them to use, which include everything except for equality and inequality.

Assuming operator< works "properly", unlike operator==, why couldn't we do this:

bool floateq(float a, float b) {
    //check NaN
    return !(a < b) && !(b < a);
}

In fact, I did run a test with an additional overload for doubles, as seen here, and it seems to have the same pitfalls as comparing them with operator==:

std::cout << "float->double vs double: " 
          << floateq(static_cast<double>(0.7f), 0.7) << " " 
          << (static_cast<double>(0.7f) == 0.7) << "\n";

Output:

float->double vs double: 0 0

Am I to worry about using all comparison operators, or is there some other aspect of comparing floating-point numbers that I'm not understanding correctly?

解决方案

The ==, <, >, <=, >=, and != operators work just fine with floating-point numbers.

You seem to have the premise that some reasonable implementation of < ought to compare (double)0.7f equal to 0.7. This is not the case. If you cast 0.7f to a double, you get 0x1.666666p-1. However, 0.7 is equal to 0x1.6666666666666p-1. These are not numerically equal; in fact, (double)0.7f is considerably smaller than 0.7 --- it would be ridiculous for them to compare equal.

When working with floating-point numbers, it is important to remember that they are floating-point numbers, rather than real numbers or rational numbers or any other such thing. You have to take into account their properties and not the properties everyone wants them to have. Do this and you automatically avoid most of the commonly-cited "pitfalls" of working with floating-point numbers.

这篇关于如果运算符&lt;适用于浮点类型,为什么我们不能使用它进行相等测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆