简体   繁体   English

简单递归帮助

[英]Simple Recursion Help

I have a simple recursive function that calculates a simple pendulum swing decay, using the ratio of height1:height2 of 0.98. 我有一个简单的递归函数,它使用0.98的height1:height2之比来计算简单的摆摆衰减。

The function has a base case of 0.0, but for some reason it turns into infinite self-calls! 该函数的基本情况为0.0,但是由于某种原因,它变成了无限的自调用!

Can anyone spot what I'm missing? 谁能发现我所缺少的东西?

Code: 码:

float swingDecay (float value) {


     if ( value == 0.00 ) {
          return value;
     }

     else { 
          return swingDecay (value * 0.98);  }     
}

mIL3S www.milkdrinkingcow.com mIL3S www.milkdrinkingcow.com

You should always use 'approximate' comparisons in floating point calculations. 在浮点计算中,应始终使用“近似”比较。 Eg, if (abs(value) < EPS) instead of if ( value == 0.00 ) . 例如, if (abs(value) < EPS)而不是if ( value == 0.00 ) EPS here is a small constant (depends on your requirements and datatype). 这里的EPS是一个小常数(取决于您的要求和数据类型)。

I suspect this is what's actually happening. 我怀疑这是实际情况。 You get to the smallest possible positive value in your datatype, like 1 * 2^(-10000) (10000 comes from the top of my head) and now value * 0.98 = value . 您将获得数据类型中可能的最小正值,例如1 * 2^(-10000) (10000来自我的头顶),现在value * 0.98 = value Eg, it has to be rounded either to 0 or to total and 0.98*total is obviously closer to total . 例如,它必须被倒圆要么0total0.98*total显然是更接近total
But that's only speculations, though. 但这只是猜测。 With floating point computations, you can never be sure :) 使用浮点计算,您将永远无法确定:)

Due to floating point calculations never being exact in floating point math you never get to value == 0.00. 由于浮点计算在浮点数学中从来都不是精确的,因此您永远不会获得值== 0.00。 You might want to try something like value < 0.0000001 or something like that and tweak it where it works. 您可能想尝试类似<<0.0000001的值或类似的东西,并在可行的地方对其进行调整。

Do not directly compare floating point numbers; 不要直接比较浮点数; your "value" will probably never really be 0.0 (zero). 您的“值”可能永远不会真正为0.0(零)。

do something like : 做类似的事情:

float smallNumber = 0.00001;
if ( value < smallNumber )
{
...
}

( value == 0.00 ) (值== 0.00)

Never gets true. 永远不会成真。 Or, it takes so many runs of the function that the it runs into, well, stack overflow :P You should take another look at how you made your function. 或者,它需要运行大量函数,然后它就会运行到堆栈溢出中:P您应该再看一下如何编写函数。 Right now it is not even useful, it can only ever return 0. 现在,它甚至没有用,只能返回0。

You could check if (value * 0.98 == value) instead of if (value == 0) . 您可以检查if (value * 0.98 == value)而不是if (value == 0) This condition will be met exactly when value becomes so small (subnormal) that it has too few bits of precision for multiplication by 0.98 to yield a different result. value变得太小(低于正常值)以至于精度乘以0.98位数太少而不能产生不同的结果时,将完全满足此条件。

use this (as seems you are going for 2 digit accuracy. 使用此功能(似乎您希望获得2位数的精度。

if (value < 0.001 )

You should not use equality for floating point values. 您不应该对浮点值使用相等性。

don't compare float values with constants, always check if they fall under a lower-bound. 不要将浮点值与常量进行比较,请始终检查它们是否在下限以下。 change your value == 0.00 to value <= 0.0001 for example 例如,将您的值== 0.00更改为值<= 0.0001

Wow, thanks for the quick answer everyone! 哇,谢谢大家的快速回答!

Apparently that little floating point detail was skipped in my class... So since everyone is pretty much saying the same thing (don't compare floating points with equality since they're never really exact), does the same hold true if I used ints or doubles? 显然,我的课堂上忽略了很少的浮点细节...因此,由于每个人都在说同一件事(不要将浮点数与相等值进行比较,因为它们从未真正精确地比较过),所以如果我使用整数还是双倍?

Originally I had the test as if ( value <= 0.0 ), but that gave me the same thing. 最初,我进行的测试就好像(value <= 0.0),但这给了我同样的事情。

Just ran it with the test as <= 0.005 and that seemed to be just fine! 只需在<= 0.005的条件下运行它就可以了!

Thanks all! 谢谢大家!

mIL3S mIL3S

www.milkdrinkingcow.com www.milkdrinkingcow.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM