Jump to content
  • Advertisement
Sign in to follow this  

Negative floats

This topic is 2778 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[font="Arial Black"]solved[/font]

Hello yet again.

I have a function to write a float to a buffer and a function to read a float from a buffer. It should work for positive and negative floats but It only works properly with positive floats.

This code sample should explain what I mean:

//append a numeric value to the out buffer
void addValOut(char valToAdd);
void addValOut(unsigned short valToAdd);
void addValOut(unsigned long valToAdd);
void addValOut(long valToAdd);
void addValOut(float valToAdd);

//get numeric values from the in buffer
char getChar();
unsigned short getUshort();
unsigned long getUlong();
long getLong();
float getFloat();

//adds bytes for a value to the out buffer - overloaded for long
void ribClient::addValOut(long valToAdd){
//convert byte order to network byte order
long toAdd = htonl(valToAdd);
char strToAdd[4];
//add to the out buffer

//adds bytes for a value to the out buffer - overloaded for float, htonl is handled by addValOut(long valToAdd)
void ribClient::addValOut(float valToAdd){
long convertedFloat = *reinterpret_cast<long *>(&valToAdd);

//returns an unsigned long determined from the next location in the inBuff
float ribClient::getFloat(){
long floatAsLong = getLong();
float floatForm = *reinterpret_cast<float *>(&floatAsLong);
return floatForm;

If I send 2 floats: 33.212 and -135.23 the outputs on the other end are: 33.2129 and -512.0000. (I presume the extra .0009 on the first one is meant to happen?)

I can send everything except floats fine. I get the same result using an unsigned long or signed long to hold the float.

Any ideas? I presume I am misunderstanding how floats are stored, the results of my googling/searching lead to various different threads and sites - none of which seemed consistent.

I tried sending 86212 and it was read as 86527.9922 small positive values seem to work though.

Share this post

Link to post
Share on other sites
How floats are stored really isn't an issue here, essentially you have 4 bytes that you want to store and retrieve, and at some point between when you submit it to be stored and when you retrieve it the value of those bytes has changed.

Have you tried stepping with a debugger and seeing that the value you are passing through your functions remains constant? You must be rearranging some bytes somewhere or something if your values don't remain the same.

Also maybe post your getLong function, so that we can follow your code from start to finish.

Share this post

Link to post
Share on other sites
First, know that your code makes many non-portable assumptions.

You are assuming equivalence between float and long types, which is not correct. You are assuming that a buffer of char[4] is the correct size, and that the buffer is properly aligned for both long and float variables. You are possibly crossing platform boundaries (at least I assume this is the case since you are using network byte order) and assuming the floating point format is identical.

You need to learn about basic serialization. There are many FAQs on serialization, like this one, some instructions are better than others. If you are using exactly the same clients on both ends then you can simply dump the variables directly across the wire. There is no need to use network byte ordering for data that doesn't need it, that applies to your integer values and floating point values.

See also FAQ entry 15.

Share this post

Link to post
Share on other sites
I am only running this on x86, I should have mentioned that. I used ntohl etc. initially because I thought I had to (not understanding that most of the articles I read were old and acted like home machines would be using different endianness), I havn't since removed them from my code.

I will try sending the float as it is without doing anything to it.

It works now, I am just sending the float as is. Thanks.

I probably won't need to be working with networking accross platform boundaries for some time.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!