C++ 浮点字节序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2945174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Floating point Endianness?
提问by cake
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 ammount 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.
我正在为实时离岸模拟器编写客户端和服务器,并且由于我必须通过套接字发送大量数据,因此我使用二进制数据来最大化我可以发送的数据量。我已经知道整数字节序,以及如何使用 htonl 和 ntohl 来规避字节序问题,但是我的应用程序,几乎所有的模拟软件,都处理很多浮点数。
My question is: Is there some issue of endianness whean 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?
我的问题是:处理浮点数的二进制格式时是否存在字节序问题?我知道我的代码将运行的所有机器都使用浮点的 IEEE 实现,但是在处理浮点数时是否存在一些字节序问题?
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.
由于我只能访问具有相同字节序的机器,因此我无法自己测试。所以,如果有人能帮助我,我会很高兴。
Thanks in advance.
提前致谢。
采纳答案by Gregor Brandt
Yes, floating point can be endianess dependent. See Converting float values from big endian to little endianfor info, be sure to read the comments.
是的,浮点数可以依赖于字节序。有关信息,请参阅将浮点值从大端转换为小端,请务必阅读评论。
回答by Daniel Pryden
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.
浮点和字节序
在某些机器上,整数以小端形式表示,而浮点数以大端形式表示。因为有许多浮点格式,并且缺乏标准的“网络”表示,所以没有制定传输浮点值的标准。这意味着写入在一台机器上的浮点数据可能无法在另一台机器上读取,即使两者都使用 IEEE 754 浮点算法也是如此,因为内存表示的字节序不是 IEEE 规范的一部分。
回答by 0fnt
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.
没有浮点字节序或整数字节序等之类的东西。它只是二进制表示字节序。机器要么是小端的,要么是大端的。这意味着它将以任何数据类型的二进制表示形式表示 MSb/MSB 作为其第一位/字节或最后一位/字节。就是这样。
--WRONG ANSWER END---
--错误答案结束--