简单递归帮助

发布于 2024-10-10 13:42:19 字数 360 浏览 8 评论 0原文

我有一个简单的递归函数,使用 height1:height2 的比率 0.98 来计算简单的摆锤摆动衰减。

该函数的基本情况为 0.0,但由于某种原因它变成了无限的自调用!

谁能发现我缺少什么吗?

代码:

float swingDecay (float value) {


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

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

mIL3S www.milkdrinkingcow.com

I have a simple recursive function that calculates a simple pendulum swing decay, using the ratio of height1:height2 of 0.98.

The function has a base case of 0.0, but for some reason it turns into infinite self-calls!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

入怼 2024-10-17 13:42:19

您应该始终在浮点计算中使用“近似”比较。例如,if (abs(value) < EPS) 而不是 if ( value == 0.00 )EPS 这里是一个小常量(取决于您的要求和数据类型)。

我怀疑这就是实际发生的事情。您得到数据类型中可能的最小正值,例如 1 * 2^(-10000) (10000 来自我的脑海),现在 value * 0.98 = value代码>.例如,它必须四舍五入到 0total 并且 0.98*total 显然更接近 total .
但这只是猜测。对于浮点计算,你永远无法确定:)

You should always use 'approximate' comparisons in floating point calculations. E.g., if (abs(value) < EPS) instead of if ( value == 0.00 ). EPS here is a small constant (depends on your requirements and datatype).

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. E.g., it has to be rounded either to 0 or to total and 0.98*total is obviously closer to total.
But that's only speculations, though. With floating point computations, you can never be sure :)

指尖凝香 2024-10-17 13:42:19

由于浮点计算在浮点数学中永远不会精确,因此您永远不会得到 value == 0.00。您可能想尝试类似 value < 0.0000001 或类似的东西并在它起作用的地方进行调整。

Due to floating point calculations never being exact in floating point math you never get to value == 0.00. You might want to try something like value < 0.0000001 or something like that and tweak it where it works.

终止放荡 2024-10-17 13:42:19

不要直接比较浮点数;你的“价值”可能永远不会真正是 0.0(零)。

做类似的事情:

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

Do not directly compare floating point numbers; your "value" will probably never really be 0.0 (zero).

do something like :

float smallNumber = 0.00001;
if ( value < smallNumber )
{
...
}
空袭的梦i 2024-10-17 13:42:19

( value == 0.00 )

永远不会成立。
或者,该函数运行了太多次,以至于它遇到了堆栈溢出:P
你应该再看看你是如何创建你的函数的。现在它甚至没有用,它只能返回 0。

( value == 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. Right now it is not even useful, it can only ever return 0.

我也只是我 2024-10-17 13:42:19

您可以检查 if (value * 0.98 == value) 而不是 if (value == 0)。当 value 变得如此小(次正规)以至于它的精度位数太少而无法乘以 0.98 来产生不同的结果时,就会恰好满足此条件。

You could check if (value * 0.98 == value) instead of 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.

谁与争疯 2024-10-17 13:42:19

使用这个(看起来你想要 2 位精度。

if (value < 0.001 )

你不应该对浮点值使用相等。

use this (as seems you are going for 2 digit accuracy.

if (value < 0.001 )

You should not use equality for floating point values.

却一份温柔 2024-10-17 13:42:19

不要将浮点值与常量进行比较,始终检查它们是否处于下限范围内。
例如,将值 == 0.00 更改为值 <= 0.0001

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

夏尔 2024-10-17 13:42:19

哇,谢谢大家的快速回答!

显然,我的课上跳过了那个小浮点细节......
因此,既然每个人都在说同样的事情(不要将浮点数与相等进行比较,因为它们从来都不是真正精确的),那么如果我使用整数或双精度数,同样的情况也成立吗?

最初我的测试就像( value <= 0.0 )一样,但这给了我同样的结果。

只是用 <= 0.005 的测试来运行它,这似乎很好!

谢谢大家!

mIL3S

www.milkdrinkingcow.com

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.

Just ran it with the test as <= 0.005 and that seemed to be just fine!

Thanks all!

mIL3S

www.milkdrinkingcow.com

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文