# Would a 2 byte float ever be worth it?

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

## Recommended Posts

I am told that the minimum packet size for udp is 64 bytes of data. It is my reasoning that if you decide to send 2 byte floats instead of the regular 4 byte floats you will have more room to send other data later on as you add it. Using data types like short and making special classes for 2 byte floats for sending data over a network seems pretty worth it to me. Even thought the packet may be padded if you decided to send a single float what if you send 100? 64/4 = 16 4 byte floats can fit in the minum packet 64/2 = 32 2 byte floats can fit in the minum packet What do you guys think?

##### Share on other sites
Modern graphics cards do have a 'half' type which is 16 bits (1 bit sign, 5 bits exponent, 10 bits mantissa) for use in shaders.

Granted, it has nothing to do with networking (aside from the whole data transfer issue), but it just shows that is has been done. [smile]

I guess it essentially depends on whether you can afford the loss in precision or not.

##### Share on other sites
Hehe, i dunno if itd be worth it for that, unless you ARE sending 100 floats, i doubt youd really NEED to use a float16, especially not for pac man :-D, and theres always the loss of precision to consider too

edit: rawr fruny beat me

anywho, hope that helps
-Dan

##### Share on other sites
It'd be possible, reasonably easy too.

Float Flint* flifli = &flBool Sign = fli & 0x80000000int Exp = fli &  0x7F800000int Mant = fli & 0x007FFFFF

That turns the float, into three variables, the sign, the Exponent and the mantissa.

Now, to turn this into a two byte float, you would to the following things.

float16 Newfloatint* flifli = &newfloatfli |= sign << 15 //Put the sign bit on the msb.fli |= (Exp & 0x78)fli |= (mant & 0x000003FF)

Note - If the float is bigger then the lower byte of the float allows, bad things happen. You also loose accurisy, as the exponent gets cut.

Edited - I think this is up to specs. Can anybody conferm/deny that for me?
From,
Nice coder

[Edited by - Nice Coder on January 8, 2005 12:14:46 AM]

##### Share on other sites
There is no minimum packet size for UDP.

However, it's perfectly valid to bit-pack your data to the precision you need. However, you're likely better off using fixed point, than reduced-precision floating point. Floating point is annoying, in that it's much less precise towards the ends of precision, than towards the center, in absolute terms.

For example, if your world is 1000 meters across, you could spend 20 bits on each of X and Z components, and if the height range is 200 meters, and spend 18 bits on Y. You would make each step of that 20-bit value be 1000/(1<<20), which would be the basic precision of your position packets. Use one of the many bit-stream classes available on the web for marshalling partial-byte data.

##### Share on other sites
@Nice coder: that won't work at all. You're dropping the most important bits of the mantissa. Also, the 16-bit float format is defined as 1 sign, 5 exponent, and 10 mantissa. NVIDIA has classes to converte between half and float on their web site (and it's part of the OpenEXR standard, too).

Also, the code you posted won't deal with denormal numbers correctly, either.

##### Share on other sites
Sorry, i didn't look up the specs for 16 bit floating point numbers. I basically didn't know they exist, i use only 32 and 64 bit numbers.

I was taking the only bytes which i could take, if i was just to take the msb the thing wouldn't work well. at least now, it works for numbers under 2^256.

Also, what are denormal numbers? Denormalised numbers?
I was assuming the floats it would be taking were normalised.

Thanks for the info hplus0603.
From,
Nice coder

##### Share on other sites
wtf?!

I remember

float 16 bits
double 32 bits
long double 64 bits (warning slow, not supported in hardware)

and i just found out that .net considers char a 2 byte size. What is the world coming to! Just cuz the space is there doesnt mean that we should use it so judiciously if the easy optimizations are so accessable, such as using only the memory that you need.

##### Share on other sites
by any chance does anyone have a 16 bit float class laying around? or, better yet, a 16 bit fixed point type? ive tried googling but cannot find anything like this.

##### Share on other sites
Quote:
 Original post by pTymNwtf?!I remember float 16 bitsdouble 32 bitslong double 64 bits (warning slow, not supported in hardware)and i just found out that .net considers char a 2 byte size. What is the world coming to! Just cuz the space is there doesnt mean that we should use it so judiciously if the easy optimizations are so accessable, such as using only the memory that you need.

It's been a long long while now that you have had to use specific typedefs to force a certain size. It goes beyond this even you see, those variables aren't even guaranteed to be the <same> size on various compilers or even over the same compiler.

• 23
• 10
• 19
• 16
• 14