限制传递给三角函数的角度有什么好处吗?

发布于 2024-12-03 12:48:22 字数 284 浏览 0 评论 0原文

我想知道将传递给三角函数的角度限制在 0Math.PI * 2 之间是否有任何优势?我有一个大量使用三角函数的函数,项目中的某人将其添加到开头:

angle %= Math.PI * 2;

这有什么好处吗?如果传递的角度在这些值之间,三角函数是否会更快?如果是的话,他们不应该自己夹住它吗?还有其他情况需要夹紧等效角度吗?

该语言是 JavaScript,最有可能在 V8 和 SpiderMonkey 上运行。

I was wondering whether there was any advantage to clamping the angle passed to trigonometric functions between 0 and Math.PI * 2? I had a function which made heavy use of trigonometric functions, and someone in the project added this to the beggining:

angle %= Math.PI * 2;

Is there any advantage to this? Are the trigonometric functions faster if the angle passed is between those values? If so, shouldn't they clamp it themselves? Is there any other case where equivalent angles should be clamped?

The language is JavaScript, most likely to be run on V8 and SpiderMonkey.

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

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

发布评论

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

评论(2

高跟鞋的旋律 2024-12-10 12:48:22

由于大多数用于计算三角函数的(片上)算法都使用 CORDIC 的某种变体,我的赌注是无论如何,这些值在 trig 函数调用的入口点都被限制在 [0, Pi/2) 内。

话虽如此,如果您有办法在整个算法中保持角度接近于零,那么这样做可能是明智的。事实上,sin(10^42) 的值几乎是未定义的,因为 10^42 范围内的粒度约为 10^25。

这意味着,例如,如果您要添加角度,并且通过这样做,它们的大小可能会变大,那么您应该考虑定期夹紧它们。但没有必要在调用三角函数之前钳位它们。

Since most (on-die) algorithms for computing trigonometric functions use some variant of CORDIC, my bet is that those values are getting clamped within [0, Pi/2) anyway at the entry point of the trig function call.

That being said, if you have a way to keep the angles close to zero throughout the algorithm, it is probably wise to do it. Indeed, the value of sin(10^42) is pretty much undefined, since the granularity in the 10^42 range is around 10^25.

This means for instance that if you are to add angles, and if by doing so, they can get large in magnitude, then you should consider periodically clamping them. But it is unneccessary to clamp them just before the trigonometric function call.

大海や 2024-12-10 12:48:22

将角度限制在 -pi/4 到 pi/4 范围内(适当使用正弦或余弦)的一个优点是,您可以确保如果使用 pi 的某种近似值计算角度,则可以使用以下方法执行范围缩小:相同的近似值。这种方法有两个好处:它可以提高 180 度的正弦或 90 度的余弦等数据的准确性,并且可以避免数学库浪费计算周期来执行超精确的范围缩减。 “更精确”的 pi 近似值与计算角度时使用的近似值不匹配。

例如,考虑 2⁴⁸ * pi 的正弦。 pi 的最佳双精度近似值(乘以 2^48)是 884279719003555,这恰好也是 2⁴⁸π 的最佳双精度近似值。 2⁴⁸π 的实际值为 884279719003555.03447074。用 pi 的最佳双近似对前一个值进行模归约将得到零,其正弦等于 2⁴⁸π 的正确正弦。将按 pi 的最佳近似值放大的值按 π 求模,将得到 -0.03447074,其正弦为 -0.03446278。

An advantage of clamping angles to the range -pi/4 to pi/4 (use sine or cosine as appropriate) is that you can ensure that if the angles are computed using some approximation of pi, range reduction is performed using that same approximation. Such an approach will have two benefits: it will improve the accuracy of things like the sine of 180 degrees or the cosine of 90 degrees, and it will avoid having math libraries waste computational cycles in an effort to perform super-accurate range reduction by a "more precise" approximation of pi which doesn't match the one used in computing the angles.

Consider, for example, the sine of 2⁴⁸ * pi. The best double approximation of pi, times 2^48, is 884279719003555, which happens to also be the best double approximation of 2⁴⁸π. The actual value of 2⁴⁸π is 884279719003555.03447074. Mod-reducing the best double approximation the former value by the best double approximation of pi would yield zero, the sine of which equals the correct sine of 2⁴⁸π. Mod-reducing by π the value scaled up by the best approximation of pi will yield -0.03447074, the sine of which is -0.03446278.

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