如何检查花车的依赖 [英] How to check dependencies of floats

查看:111
本文介绍了如何检查花车的依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要确定(在C + +)如果一个浮点数是另一个浮点数的乘法逆。的问题是,我必须使用一个第三可变做到这一点。例如,这code:

I want to determine (in c++) if one float number is the multiplicative inverse of another float number. The problem is that i have to use a third variable to do it. For instance this code:

float x=5,y=0.2;
if(x==(1/y)) cout<<"They are the multiplicative inverse of eachother"<<endl;
else cout<<"They are NOT the multiplicative inverse of eachother"<<endl;

将输出:他们是不是......,这是错误的,这个code:

will output: "they are not..." which is wrong and this code:

float x=5,y=0.2,z;
z=1/y;
if(x==z) cout<<"They are the multiplicative inverse of eachother"<<endl;
else cout<<"They are NOT the multiplicative inverse of eachother"<<endl;

将输出:?他们是......,这是正确的
为什么会这样

will output: "they are..." which is right.
why is this happening?

推荐答案

    您这里有两个问题,但都来自同根

    The Float Precision Problem

      You have two problems here, but both come from the same root

      您无法比拟的花车precisely。你不能减,或将它们precisely。你不能指望什么作为他们precisely。与他们的任何操作可以(而且几乎总是如此)带来一些错误到结果。即使 A = 0.2F 不是precise操作。那的更深层次的原因是很好这里的其他答案的作者解释说。 <子>(我的感谢,并投票给他们的。)

      You can't compare floats precisely. You can't subtract or divide them precisely. You can't count anything for them precisely. Any operation with them could (and almost always does) bring some error into the result. Even a=0.2f is not a precise operation. The deeper reasons of that are very well explained by the authors of the other answers here. (My thanks and votes to them for that.)

      下面来你的第一个,更简单的错误。你永远不应该,从来没有永远 永远 从来没有在他们身上使用= =或等值的任何一种语言。

      Here comes your first and more simple error. You should never, never, never, never, NEVER use on them == or its equivalent in any language.

      而不是 A == b ,使用 ABS(AB)&LT; HighestPossibleError 而不是

        但是,这是不是在你的任务的唯一问题。

          But this is not the sole problem in your task.

          ABS(1 / YX)&LT; HighestPossibleError 将无法正常工作,无论是。至少,它不会工作往往不够。为什么?

          Abs(1/y-x)<HighestPossibleError won't work, either. At least, it won't work often enough. Why?

          让我们对x = 1000和y = 0.001。让我们以y的开始相对误差为10 -6

          Let's take pair x=1000 and y=0.001. Let's take the "starting" relative error of y for 10-6.

          <子>(相对误差=误差/值)。

          值的相对误差增加的乘法和除法。

          Relative errors of values are adding to at multiplication and division.

          1 / y为1000左右。相对误差是相同的10 -6 。 (1还没有错误)

          1/y is about 1000. Its relative error is the same 10-6. ("1" hasn't errors)

          这使得绝对误差= 1000 * 10 -6 = 0.001。当你以后减去X,该错误将所有剩下的。 (绝对错误添加到加减,x的误差可以忽略不计。)当然,你不能指望这么大的误差,HighestPossibleError将无疑为低,你的程序将甩开了良好的对x,是

          That makes absolute error =1000*10-6=0.001. When you subtract x later, that error will be all that remains. (Absolute errors are adding to at adding and subtracting, and the error of x is negligibly small.) Surely, you are not counting on so large errors, HighestPossibleError would be surely set lower and your program would throw off a good pair of x,y

          因此​​,接下来的两个规则浮点运算:尽量不要用较小的分歧之一更大的估价师和上帝救你脱离后减去收盘值。

          So, the next two rule for float operations: try not to divide greater valuer by lesser one and God save you from subtracting the close values after that.

          有两种简单的方法来逃避这个问题。

          There are two simple ways to escape this problem.

          • 通过建立x的是什么,y具有更大的ABS值,除以1的较大者,后来才减去较小之一。

          • By founding what of x,y has the greater abs value and divide 1 by the greater one and only later to subtract the lesser one.

          如果你想比较 1 /对X Y ,你的工作尚未有字母,没有价值观的同时,和你的业务不作任何错误的,由y乘以比较的两侧 你有 1对X * Y 。 <子>(通常你应该检查该操作的迹象,但在这里我们采用ABS值,因此,它是干净的。)的结果比较有没有分裂的。

          If you want to compare 1/y against x, while you are working yet with letters, not values, and your operations make no errors, multiply the both sides of comparison by y and you have 1 against x*y. (Usually you should check signs in that operation, but here we use abs values, so, it is clean.) The result comparison has no division at all.

          在一个较短的方式:

          1/y V x   <=>   y*(1/y) V x*y   <=>   1 V x*y 
          

          我们已经知道,这样的比较,因为 1对X * Y 应该这样做:

          We already know that such comparison as 1 against x*y should be done so:

          const float HighestPossibleError=1e-10;
          if(Abs(x*y-1.0)<HighestPossibleError){...
          

          这就是一切。

          That is all.

          P.S。如果你真的需要它的所有的一行,使用:

          P.S. If you really need it all on one line, use:

          if(Abs(x*y-1.0)<1e-10){...
          

          但它是坏的风格。我不会建议吧。

          But it is bad style. I wouldn't advise it.

          P.P.S。在第二个例子中,编译器优化了code的话,那它设置Z到5运行任何code之前。因此,检查5对5的作品即使是浮动。

          P.P.S. In your second example the compiler optimizes the code so, that it sets z to 5 before running any code. So, checking 5 against 5 works even for floats.

          这篇关于如何检查花车的依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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