浮点字节序?
我正在为实时海上模拟器编写客户端和服务器,并且由于我必须通过套接字发送大量数据,因此我使用二进制数据来最大化可以发送的数据量。我已经了解整数字节顺序,以及如何使用 htonl
和 ntohl
来规避字节顺序问题,但我的应用程序与几乎所有模拟软件一样,处理大量浮点数。
我的问题是:处理浮点数的二进制格式时是否存在字节序问题?我知道运行我的代码的所有机器都使用浮点的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 维基百科,
According to Wikipedia,
是的,浮点可以依赖于字节顺序。请参阅将浮点值从大端转换为小端 有关信息,请务必阅读评论。
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.
编辑:以下是错误的答案(离开以便人们知道这种“有点流行”的观点是错误的,请阅读已接受的答案和对此答案的评论)
--错误答案开始--
不存在浮点字节序之类的东西或整数字节序等。它只是二进制表示字节序。
机器要么是小端,要么是大端。这意味着它将以任何数据类型的二进制表示形式表示 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---