SFML community forums

Help => Network => Topic started by: osadijfj43 on April 12, 2009, 05:35:52 pm

Title: Safest way to send floats over sockets?
Post by: osadijfj43 on April 12, 2009, 05:35:52 pm
The SFML packet tutorial mentions problems that can arise from exchanging primitive types over sockets to different platforms.  As a solution, it mentions

Quote
... to use SFML's fixed size types : sf::Int8, sf::Uint16, sf::Int32, etc. Those types are guaranteed to have the intended size on any platform.


What is the safest way to avoid problems while sending floats (and doubles) over a socket?  Does the IEEE floating point standard define floats to be 32-bits and doubles to be 64-bits?  If so, are there any platforms that deviate from the standard that I (well, I suppose everyone) should be aware of?
Title: Safest way to send floats over sockets?
Post by: Hiura on April 12, 2009, 07:14:53 pm
The only rule is sizeof(float) <= sizeof(double) <= sizeof(long double), I think.
But you can build your own type to "patch" this lack with two sf::Int*. Or maybe with another lib (boost ?).
Title: Safest way to send floats over sockets?
Post by: Laurent on April 12, 2009, 07:19:57 pm
There is no standard specification about the size of floating numbers. However, as far as I know, every "common" platform implements them the same way, it's pretty safe to use them directly.
Title: Safest way to send floats over sockets?
Post by: Groogy on April 12, 2009, 11:58:00 pm
I would do like Hiura said if you have to be 100% it's right. We actually did a similar thing in class. We had a struct with two integer to represent a floating point number. How does that sound?