Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Safest way to send floats over sockets?  (Read 6410 times)

0 Members and 1 Guest are viewing this topic.

osadijfj43

  • Newbie
  • *
  • Posts: 1
    • View Profile
Safest way to send floats over sockets?
« 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?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Safest way to send floats over sockets?
« Reply #1 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 ?).
SFML / OS X developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Safest way to send floats over sockets?
« Reply #2 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.
Laurent Gomila - SFML developer

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Safest way to send floats over sockets?
« Reply #3 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?
Developer and Maker of rbSFML and Programmer at Paradox Development Studio