C# 数学与 XNA MathHelper

发布于 2024-08-06 16:54:27 字数 439 浏览 1 评论 0原文

自从我需要在 C# 中使用 PI (3.1415...) 以来,我就使用 Math.PI 来获取该值。通常我只会使用 Math.PI/2.02.0*Math.PI 之类的值,但现在我刚刚注意到 XNA 提供了一个 MathHelper 类。这样做的好处是我可以调用 MathHelper.PiOver2MathHelper.TwoPi,从而使极其琐碎的步骤变得更加琐碎。 ;-)

我认为这两个类是可以互换的,但我注意到 Math.PI/2.0 != MathHelper.PiOver2。我试图研究为什么会这样,但我一无所获。所以,我想我应该在这里碰碰运气。关于PI的使用,Math类和MathHelper类有什么区别吗?其中一个比另一个更受青睐吗?或者我应该不去管它,只确保在整个程序中始终使用其中之一?

Ever since I needed to work with PI (3.1415...) in C# I have used Math.PI to get the value. Usually I would just use values like Math.PI/2.0 or 2.0*Math.PI, but now I have just noticed that XNA provides a MathHelper class. The nice thing about this is I can call MathHelper.PiOver2 and MathHelper.TwoPi, thus making an extremely trivial step even more trivial. ;-)

I assumed these two classes were interchangable, but I noticed that Math.PI/2.0 != MathHelper.PiOver2. I tried to research why this would be, but I found nothing. So, I thought I would try my luck here. With regards to using PI, are there any differences between the Math class and the MathHelper class? Is one preferred over the other? Or should I just leave well enough alone and just make sure to consistently use one or the other throughout my program?

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

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

发布评论

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

评论(3

薆情海 2024-08-13 16:54:27

他们怎么不平等呢?如果它们足够接近,这可能只是传统问题,用浮点测试相等性几乎是不可能的。

另外,它们是同一类型吗?我的观点是大多数游戏计算都是用浮点数完成的,而 Math.PI 将是双精度数。

编辑: MathHelper 确实使用浮点数

How not equal are they? If they are sufficiently close, this might just be the traditional problem that testing equality with floating points is near impossible.

Also, are they of the same type? My opinion was most gaming calculations were done with floats, where as Math.PI would be a double.

EDIT: MathHelper does indeed use floats

风流物 2024-08-13 16:54:27

查看数字:

3,1415926535897900000000000000000 // Math.PI

3,1415930000000000000000000000000 // MathHelper.PI

3,1415926535897932384626433832795 // 来自 Windows Cal 的 PI c


1,5707963267949000000000000000000 // Math.PI / 2

1,5707963705062900000000000000000 // MathHelper.PiOver2

1,5707960000000000000000000000000 // MathHelper.Pi / 2

1,5707963267948966192313216916398 // Windows Calc 中的 PI / 2


问题说明:精度损失

Best PI / 2 = Math.PI / 2

Look to digit:

3,1415926535897900000000000000000 // Math.PI

3,1415930000000000000000000000000 // MathHelper.PI

3,1415926535897932384626433832795 // PI from Windows Calc


1,5707963267949000000000000000000 // Math.PI / 2

1,5707963705062900000000000000000 // MathHelper.PiOver2

1,5707960000000000000000000000000 // MathHelper.Pi / 2

1,5707963267948966192313216916398 // PI / 2 from Windows Calc


Explanation of problem: The loss in accuracy

Best PI / 2 = Math.PI / 2

治碍 2024-08-13 16:54:27

最好的 PI/2 不是 Math.PI/2!

在游戏开发中不要使用Math.PI常量,准确性的损失可以忽略不计,你不会看到游戏对象运动的差异......性能更重要。使用 MathHelper.PiOver2 将为您节省双除法和双精度到浮点转换。这似乎没什么帮助,但在计算密集型问题(粒子系统)中,差异是显着的。

Best PI/2 is not Math.PI/2!

In game development don't use the Math.PI constant, the loss of accuracy is negligible, you won't see the difference in the movement of your game objects... The performance is more important. Using the MathHelper.PiOver2 will save you a double division and a double to float conversion. This might seems to be very little help, but there are computationally intensive problems (particle systems) where the difference is significant.

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