浮点字节序?

发布于 2024-09-03 06:55:11 字数 315 浏览 6 评论 0原文

我正在为实时海上模拟器编写客户端和服务器,并且由于我必须通过套接字发送大量数据,因此我使用二进制数据来最大化可以发送的数据量。我已经了解整数字节顺序,以及如何使用 htonlntohl 来规避字节顺序问题,但我的应用程序与几乎所有模拟软件一样,处理大量浮点数。

我的问题是:处理浮点数的二进制格式时是否存在字节序问题?我知道运行我的代码的所有机器都使用浮点的 IEEE 实现,但是在处理浮点数时是否存在一些字节序问题?

由于我只能访问具有相同字节序的机器,因此我无法自己测试这一点。所以,如果有人能帮助我解决这个问题,我会很高兴。

I'm writing a client and a server for a realtime offshore simulator, and, as I have to send a lot of data through a socket, I'm using binary data to maximize the amount of data I can send. I already know about integers endianness, and how to use htonl and ntohl to circumvent endianness issues, but my application, as almost all simulation software, deals with a lot of floats.

My question is: Is there some issue of endianness when dealing with binary formats of floating point numbers? I know that all the machines where my code will run use IEEE implementation of floating points, but is there some endianness issue when dealing with floats?

Since I only have access to machines with the same endian, I cannot test this by myself. So, I'll be glad if someone can help me with this.

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

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

发布评论

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

评论(3

伊面 2024-09-10 06:55:11

根据 维基百科

浮点和字节顺序

在某些机器上,虽然整数是
以小端形式表示,
浮点数是
以大端形式表示。
因为浮点数有很多
格式和缺乏标准
“网络”表示,无标准
用于传输浮点值
已经做了。这意味着
写在一个浮点数据上
机器可能无法读取
另一个,情况也是如此,即使
两者都使用 IEEE 754 浮点
算术,因为字节序
内存表示不属于
IEEE 规范。

According to Wikipedia,

Floating-point and endianness

On some machines, while integers were
represented in little-endian form,
floating point numbers were
represented in big-endian form.
Because there are many floating point
formats, and a lack of a standard
"network" representation, no standard
for transferring floating point values
has been made. This means that
floating point data written on one
machine may not be readable on
another, and this is the case even if
both use IEEE 754 floating point
arithmetic since the endianness of the
memory representation is not part of
the IEEE specification.

冰火雁神 2024-09-10 06:55:11

是的,浮点可以依赖于字节顺序。请参阅将浮点值从大端转换为小端 有关信息,请务必阅读评论。

Yes, floating point can be endianess dependent. See Converting float values from big endian to little endian for info, be sure to read the comments.

过期以后 2024-09-10 06:55:11

编辑:以下是错误的答案(离开以便人们知道这种“有点流行”的观点是错误的,请阅读已接受的答案和对此答案的评论)

--错误答案开始--

不存在浮点字节序之类的东西或整数字节序等。它只是二进制表示字节序。
机器要么是小端,要么是大端。这意味着它将以任何数据类型的二进制表示形式表示 MSb/MSB 作为其第一位/字节或最后一位/字节。
就是这样。

--错误答案结束---

EDIT: THE FOLLOWING IS WRONG ANSWER (leaving so that people know that this 'somewhat popular' view is wrong, please read the accepted answer and comments on this answer)

--WRONG ANSWER BEGIN--

There is no such thing as floating point endianness or integer endianness etc. Its just binary representation endianness.
Either a machine is little-endian, or its big-endian. Which means that it will either represent the MSb/MSB in the binary representation of any datatype as its first bit/byte or last bit/byte.
Thats it.

--WRONG ANSWER END---

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