重新调整浮点方程

发布于 2024-07-22 20:43:24 字数 509 浏览 7 评论 0原文

我想知道是否有办法提高坡度计算的准确性。 (这是几个月前出现的此处) 。

看来通过改变:

float get_slope(float dXa, float dXb, float dYa, float dYb) {
    return (dXa - dXb)/(dYa - dYb);
}

可能

float get_slope(float dXa, float dXb, float dYa, float dYb) {
    return  dXa/(dYa - dYb) - dXb/(dYa - dYb);
}

会有所改善。 建议?

编辑:我追求的是精度,而不是效率。

I'd like to know if there is a way to improve the accuracy of calculating a slope. (This came up a few months back here).

It seems by changing:

float get_slope(float dXa, float dXb, float dYa, float dYb) {
    return (dXa - dXb)/(dYa - dYb);
}

to

float get_slope(float dXa, float dXb, float dYa, float dYb) {
    return  dXa/(dYa - dYb) - dXb/(dYa - dYb);
}

might be an improvement. Suggestions?

Edit: It's precision I'm after, not efficiency.

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

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

发布评论

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

评论(3

遇到 2024-07-29 20:43:24

在函数内将它们转换为double


显然,当分母接近零时,你就会遇到麻烦。 你的斜率将接近无穷大。 所以很大程度上取决于你想对斜坡做什么。 有时,如果您知道 delta y 将接近于零,您可以计算斜率的倒数并使用它。 您甚至可以检测哪个更小 - deltax 或 deltay 的绝对值,并返回斜率或 1/斜率。 另请参阅atan2()。


如果您知道输入是十进制的,并且您也希望输出是十进制的,则可以通过使用十进制库进行所有计算来克服将浮点数转换为二进制并返回的固有精度损失。 我记得当我使用 Atari BASIC 进行十进制计算时我是多么高兴,因为它使用了 6502 的 BCD 模式。

Cast them to double inside the function.


Where you're going to run into trouble is when the denominator is near zero, obviously. Your slope will approach infinity. So a lot depends on what you want to do with the slope. Sometimes, if you know your delta y is going to be near zero, you can calculate the reciprocal of slope and use that instead. You can even detect which is smaller--the absolute value of deltax or deltay and return the slope or 1/slope. Also look into atan2().


If you know the input is in decimal, and you want the output in decimal as well, you can overcome the loss of precision that is inherent in converting floats to binary and back by doing all calculations with a decimal library. I remember how pleased I was when I used Atari BASIC for decimal calculations, as it used the 6502's BCD mode.

辞别 2024-07-29 20:43:24

我认为你有一个错字。 您可能的意思是

return  dXa/(dYa - dYb) - dXb/(dYa - dYb);

我会说您给出的第一种形式具有更高的精度。 如果 dXa 和 dXb 接近且较大,那么在相减之前,您将失去两次除法的精度。

I think you have a typo. You probably mean

return  dXa/(dYa - dYb) - dXb/(dYa - dYb);

I would say that the first form that you gave has higher precision. If dXa and dXb are close and large, then you would lose precision in the two divisions before subtracting.

情深已缘浅 2024-07-29 20:43:24

如果您不介意消耗一些额外的周期,则可以通过循环获得更高的准确性。

计算A和B之间线段的斜率。

计算{(Xa - (Xa -Xb))、(Ya -(Ya -Yb)}和{(Xb + (Xa - Xb))之间线段的斜率, (Yb +(Ya - Yb))}... 基本上是 A - 斜率和 B + 斜率,

然后比较所得斜率,如果差异太大(选择所需的阈值),则继续,并对所有斜率进行平均。 这可以帮助消除由非常小的斜率的

浮点运算引起的异常。

If you don't mind burning some extra cycles you can get better accuracy by doing a loop.

Calculate slope of the line segment between A and B.

Calculate slope of the line segment between {(Xa - (Xa -Xb)), (Ya -(Ya -Yb)} and {(Xb + (Xa - Xb)), (Yb +(Ya - Yb))}... Basically A - slope and B + slope.

Then compare the resulting slopes. If the difference is too high (choose the threshold you want) then keep going, and average all of the slopes at the end.

This can help smooth out anomalies caused by floating point arithmetic for very small slopes.

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