49

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
cake
  • 1,336
  • 1
  • 13
  • 20
  • 1
    possible duplicate of [Converting float values from big endian to little endian](http://stackoverflow.com/questions/2782725/converting-float-values-from-big-endian-to-little-endian) – philant May 31 '10 at 17:54

3 Answers3

23

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.

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
  • @Craig M. Brandenburg: I appreciate your effort to keep this answer up to date, but your edit substantially changed the meaning of my answer, so I've rolled it back. I'd recommend you just post your own answer -- if you did I would upvote it. – Daniel Pryden Sep 11 '14 at 06:16
  • 12
    FYI, the content on Wikipedia has been updated (and is better): "Although the ubiquitous x86 of today use little-endian storage for all types of data (integer, floating point, BCD), there have been a few historical machines where floating point numbers were represented in big-endian form while integers were represented in little-endian form." ... "However, on modern standard computers (i.e., implementing IEEE 754), one may in practice safely assume that the endianness is the same for floating point numbers as for integers, making the conversion straightforward regardless of data type." – jrupe Jun 05 '15 at 19:52
12

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.

Community
  • 1
  • 1
Gregor Brandt
  • 7,659
  • 38
  • 59
-5

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---

0fnt
  • 8,211
  • 9
  • 45
  • 62
  • 1
    Thanks for the clarification on Endianness meaning. I wasn't sure how MSB order was defined in the case of floating point numbers. – cake May 31 '10 at 18:06
  • 1
    Would the downvoter care to explain themself? I though this post nicely clarified the topic of endianess, and was in line with my own understanding of it. – csj May 31 '10 at 19:31
  • 9
    While I didn't downvote, it might have been because the statement isn't actually true. There have been systems where the FPU expected a different byte order than the rest of the system did, in which case there would be "floating point endianness" and "integer endianness." I don't *think* its much of an issue on modern architectures, though, so as a general rule you're likely okay to ignore this distinction. – Dennis Zickefoose May 31 '10 at 19:58
  • 1
    @Dennis Thank you. I did not know that systems existed where the FPU used a different endianness. – csj May 31 '10 at 20:45
  • Dennis thanks for the clarification. However, I fail to understand the relationship between FPU and network transactions. Network transactions will be based on the system endianness, which would be independent of the FPU endianness, AFAIunderstand. Can you please correct me if I'm wrong. Thanks. – 0fnt Jun 01 '10 at 05:26
  • 1
    The relevance of the FPU is that floats stored in memory will use the native byte ordering, i.e. the ordering expected and produced by the FPU. There have been machines that store floats using big endian and ints in little endian. – bames53 Sep 10 '14 at 19:30