浮点,是相等比较足以prevent除以零? [英] Floating Point, is an equality comparison enough to prevent division by zero?

查看:100
本文介绍了浮点,是相等比较足以prevent除以零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// value will always be in the range of [0.0 - maximum]

float obtainRatio(float value, float maximum){
    if(maximum != 0.f){
        return value / maximum;  
    }else{
        return 0.f;
    }
}

最高的范围可以是任何东西,包括负数。 值的范围也可以是任何东西,虽然功能只需要进行检测时输入的是在 [0.0的范围 - 最大] 。输出应始终处于范围 [0.0 - 1.0]

The range of maximum can be anything, including negative numbers. The range of value can also be anything, though the function is only required to make "sense" when the input is in the range of [0.0 - maximum]. The output should always be in the range of [0.0 - 1.0]

我有我想了解一下两个问题,这一点:

I have two questions that I'm wondering about, with this:


  • 这是相等比较足以保证功能不会被划分为零?

  • 如果最大值是退化值(非常小或非常大),是否有机会函数将返回之外的结果?[0.0 - 1.0](假设值是在正确的范围内)

推荐答案

下面是一个迟到的回答澄清一些概念有关的问题:

Here is a late answer clarifying some concepts in relation to the question:

在浮点被零除不喜欢整数除法一个致命错误零。
既然你知道之间0.0 最高,零只划分可能出现的 0.0 / 0.0 ,它被定义为生产 NaN的。浮点值 NaN的是函数 obtainRatio 一个完全可以接受的价值回归,而实际上是在一个更好的非凡的价值回归比 0.0 ,因为你提出的版本回来了。

In floating-point, division by zero is not a fatal error like integer division by zero is. Since you know that value is between 0.0 and maximum, the only division by zero that can occur is 0.0 / 0.0, which is defined as producing NaN. The floating-point value NaN is a perfectly acceptable value for function obtainRatio to return, and is in fact a much better exceptional value to return than 0.0, as your proposed version is returning.

没有什么近似约&LT定义; = 花车之间。 A< = B 做的不可以有时评估为真时, A 是一点点上述 b 。如果 A B 是两个有限浮动变量, A< = b 评估为真什么时候通过理性的再$ p $ A 小于或等于理性的再$ p $由 psented b 。唯一的小毛刺人们可以感觉到,其实并不是一个小故障,但规则的严格间pretation以上: +0.0< = -0.0 的值为true,因为理性的再$ p $由 +0.0 psented和理性的再$ p $由 -0.0 psented 都为0。

There is nothing approximate about the definition of <= between floats. a <= b does not sometimes evaluate to true when a is just a little above b. If a and b are two finite float variables, a <= b evaluate to true exactly when the rational represented by a is less than or equal to the rational represented by b. The only little glitch one may perceive is actually not a glitch but a strict interpretation of the rule above: +0.0 <= -0.0 evaluates to true, because "the rational represented by +0.0" and "the rational represented by -0.0" are both 0.

同样,有什么近似约 == 花车之间:两个有限浮动变量 A b A == b 真当且仅当理性重新$ p $由psented A psented b 理性的再$ p $是相同。

Similarly, there is nothing approximate about == between floats: two finite float variables a and b make a == b true if and only if the rational represented by a and the rational represented by b are the same.

在一个如果(F!= 0.0)条件, F的的值不能再$ P零$ psentation,因而除以˚F不能除以零。该部门仍然可以溢出。在值/最大的具体情况,因为你的函数需要 0≤值≤最大不可能有溢出。而我们并不需要在precondition怀疑是否表示有理数或浮子之间的关系之间的关系,因为两者是基本相同的。

Within a if (f != 0.0) condition, the value of f cannot be a representation of zero, and thus a division by f cannot be a division by zero. The division can still overflow. In the particular case of value / maximum, there cannot be an overflow because your function requires 0 ≤ value ≤ maximum. And we don't need to wonder whether in the precondition means the relation between rationals or the relation between floats, since the two are essentially the same.

C99允许额外的precision浮点前pressions,这已经在过去的错误地间$ P $由编译器制造商PTED为许可进行浮点行为古怪(到如此地步,程序如果(M = 0){如果(M == 0,)的printf(哦);} <!/ code>可以预期打印哦,在某些情况下)。

C99 allows extra precision for floating-point expressions, which has been in the past wrongly interpreted by compiler makers as a license to make floating-point behavior erratic (to the point that the program if (m != 0.) { if (m == 0.) printf("oh"); } could be expected to print "oh" in some circumstances).

在现实中,C99编译器,提供了IEEE 754浮点和定义 FLT_EVAL_METHOD 来一个非负的值不能改变 M 它已经过测试之后。变量 M 设置为一个价值重估presentable浮法当它被最后分配的,该值要么是0重新presentation或者它不是'T。只有操作和常量可以有多余的precision(见C99标准,5.2.4.2.2:8)。

In reality, a C99 compiler that offers IEEE 754 floating-point and defines FLT_EVAL_METHOD to a nonnegative value cannot change the value of m after it has been tested. The variable m was set to a value representable as float when it was last assigned, and that value either is a representation of 0 or it isn't. Only operations and constants can have excess precision (See the C99 standard, 5.2.4.2.2:8).

在GCC的情况下,最近的版本中做什么是正确与 -fexcess- precision =标准,由 -std =暗示C99

In the case of GCC, recent versions do what is proper with -fexcess-precision=standard, implied by -std=c99.


  • 浮点C中的悲惨状况的大卫Monniaux的描述在几年前(第一个版本2007年出版)。大卫的报告并没有尝试跨preT C99的标准,但描述了浮点计算的现实C作为就在那时,用实际的例子。这种情况已大为在关心编译器和感谢呈现整个问题毫无意义SSE2指令集,因为提高,这要归功于改进的标准符合性。

  • David Monniaux's description of the sad state of floating-point in C a few years ago (first version published in 2007). David's report does not try to interpret the C99 standard but describes the reality of floating-point computation in C as it was then, with real examples. The situation has much improved since, thanks to improved standard-compliance in compilers that care and thanks to the SSE2 instruction set that renders the entire issue moot.

邮件列表后通过约瑟夫·迈尔斯描述花车当时的海湾局势GCC(坏),他是如何跨preTED标准(良好)以及他是如何实现在海湾合作委员会(GOOD)他除pretation。

The 2008 mailing list post by Joseph S. Myers describing the then current GCC situation with floats in GCC (bad), how he interpreted the standard (good) and how he was implementing his interpretation in GCC (GOOD).

这篇关于浮点,是相等比较足以prevent除以零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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