“ - 各人”产生“比较具有==或!=的浮点是不安全的” [英] "-Weverything" yielding "Comparing floating point with == or != is unsafe"

查看:1516
本文介绍了“ - 各人”产生“比较具有==或!=的浮点是不安全的”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,我转换为双重这样:

  double d = [string doubleValue]; 

doubleValue 的文档告诉我们, overflow,此方法返回 HUGE_VAL -HUGE_VAL 。这是我之前检查过的:

  if(d == HUGE_VAL || d == -HUGE_VAL)
// overflow

现在,由于添加了新的-Weverything警告标志,编译器现在抱怨

 比较浮点与==或!=不安全

如何解决此问题?






我也有同样的问题,比较两个正常浮点数(即不是HUGE_VAL)。例如,

  double a,b; 
// ...
if(a!= b)//这将产生相同的警告
// ...

如何解决这个问题?

解决方案

不需要担心这个警告。



doubleValue 的文档不会说它返回在溢出时足够接近 HUGE_VAL -HUGE_VAL 。它说它在溢出的情况下准确返回这些值。



换句话说,方法在溢出时返回的值将 == 比较为 HUGE_VAL -HUGE_VAL





考虑 0.3 + 0.4 == 0.7 的例子。此示例计算为false。人们,包括你遇到的警告的作者,认为浮点 == 不准确,并且意外的结果来自这个不准确。



他们都错了。



浮点添加是不准确不准确:它返回您请求的操作的最近的可表示浮点数。在上面的例子中,转换(从十进制到浮点)和浮点加法是奇怪行为的原因。



浮点 / em>,另一方面,工作方式与其他离散类型完全一样。浮点相等是精确的:除了小的异常(NaN值和+0和-0的情况),当且仅当所考虑的两个浮点数具有相同的表示时,等于计算为真。 / p>

您不需要epsilon来测试两个浮点值是否相等。此外,由于 Dewar实质上说,示例 0.3 + 0.4 == 0.7 应该在 + ,而不是 ==



最后,与epsilon内部相比,意味着不相等的值将看起来相等,这不适合所有算法。 / p>

I have a string that I convert to a double like this:

double d = [string doubleValue];

The documentation for doubleValue tells us that upon overflow, this method returns either HUGE_VAL or -HUGE_VAL. This is how I checked for this earlier:

if (d == HUGE_VAL || d == -HUGE_VAL)
   //overflow

Now, since adding the new "-Weverything" warning flag, the compiler now complains that

Comparing floating point with == or != is unsafe

How can I resolve this issue? How should I be doing these comparisons?


I also have the same question about comparing two "normal" floating point numbers (i.e. not "HUGE_VAL" ones). For instance,

double a, b;
//...
if (a != b) //this will now yield the same warning
  //...

How should this be resolved?

解决方案

You do not need to worry about this warning. It is nonsense in a lot of cases, including yours.

The documentation of doubleValue does not say that it returns something close enough to HUGE_VAL or -HUGE_VAL on overflow. It says that it returns exactly these values in case of overflow.

In other words, the value returned by the method in case of overflow compares == to HUGE_VAL or -HUGE_VAL.

Why does the warning exist in the first place?

Consider the example 0.3 + 0.4 == 0.7. This example evaluates to false. People, including the authors of the warning you have met, think that floating-point == is inaccurate, and that the unexpected result comes from this inaccuracy.

They are all wrong.

Floating-point addition is "inaccurate", for some sense of inaccurate: it returns the nearest representable floating-point number for the operation you have requested. In the example above, conversions (from decimal to floating-point) and floating-point addition are the causes of the strange behavior.

Floating-point equality, on the other hand, works pretty much exactly as it does for other discrete types. Floating-point equality is exact: except for minor exceptions (the NaN value and the case of +0. and -0.), equality evaluates to true if and only if the two floating-point numbers under consideration have the same representation.

You don't need an epsilon to test if two floating-point values are equal. And, as Dewar says in substance, the warning in the example 0.3 + 0.4 == 0.7 should be on +, not on ==, for the warning to make sense.

Lastly, comparing to within an epsilon means that values that aren't equal will look equal, which is not appropriate for all algorithms.

这篇关于“ - 各人”产生“比较具有==或!=的浮点是不安全的”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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