在不同机器之间使用浮点数/双精度数时我应该知道什么?

发布于 2024-11-27 02:57:29 字数 221 浏览 3 评论 0原文

我听说不同 CPU 上的浮点数/双精度数有很多问题。

如果我想制作一个所有内容都使用浮点的游戏,我如何确保每台机器上的浮点计算完全相同,以便我的模拟在每台机器上看起来都完全相同?

我还担心写入/读取文件或将浮点值发送/接收到不同的计算机。如果有的话,必须进行哪些转换?

我需要 100% 确定我的浮点值的计算结果完全相同,因为即使计算中的微小差异也会导致完全不同的未来。这可能吗?

I've heard that there are many problems with floats/doubles on different CPU's.

If i want to make a game that uses floats for everything, how can i be sure the float calculations are exactly the same on every machine so that my simulation will look exactly same on every machine?

I am also concerned about writing/reading files or sending/receiving the float values to different computers. What conversions there must be done, if any?

I need to be 100% sure that my float values are computed exactly the same, because even a slight difference in the calculations will result in a totally different future. Is this even possible ?

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

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

发布评论

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

评论(2

枫林﹌晚霞¤ 2024-12-04 02:57:29

除了范围限制之外,标准 C++ 没有规定有关浮点类型的任何细节,并且可能某些数学函数(如正弦函数和指数函数)必须正确到一定的精度级别。

除此之外,在这种普遍程度上,真的没有什么可以依靠的!

也就是说,您很可能实际上不需要在每个平台上进行相同的二进制计算,并且 floatdouble 类型的精度和准确度保证实际上将足以用于模拟目的。

请注意,当您修改子表达式的计算顺序时,您甚至无法在自己的程序中生成可靠的代数表达式结果,因此要求您想要的可再现性无论如何可能有点不切实际。如果您需要真正的浮点精度和准确度保证,那么您可能最好使用具有正确舍入的任意精度库,例如 MPFR< /a> - 但这对于游戏来说似乎不现实。

序列化浮动是一个完全不同的故事,您必须了解目标平台使用的表示形式。如果所有平台实际上都使用 32 或 64 位大小的 IEEE 754 浮点数,您可能可以直接交换二进制表示形式(模字节序)。如果您有其他平台,则必须考虑自己的序列化方案。

Standard C++ does not prescribe any details about floating point types other than range constraints, and possibly that some of the maths functions (like sine and exponential) have to be correct up to a certain level of accuracy.

Other than that, at that level of generality, there's really nothing else you can rely on!

That said, it is quite possible that you will not actually require binarily identical computations on every platform, and that the precision and accuracy guarantees of the float or double types will in fact be sufficient for simulation purposes.

Note that you cannot even produce a reliable result of an algebraic expression inside your own program when you modify the order of evaluation of subexpressions, so asking for the sort of reproducibility that you want may be a bit unrealistic anyway. If you need real floating point precision and accuracy guarantees, you might be better off with an arbitrary precision library with correct rounding, like MPFR - but that seems unrealistic for a game.

Serializing floats is an entirely different story, and you'll have to have some idea of the representations used by your target platforms. If all platforms were in fact to use IEEE 754 floats of 32 or 64 bit size, you could probably just exchange the binary representation directly (modulo endianness). If you have other platforms, you'll have to think up your own serialization scheme.

羞稚 2024-12-04 02:57:29

每个程序员都应该知道的内容: http://docs.sun.com/source/806 -3568/ncg_goldberg.html

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