Would a 2 byte float ever be worth it?

Started by
17 comments, last by Basiror 19 years, 3 months ago
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?
Advertisement
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
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]
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
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.

enum Bool { True, False, FileNotFound };
@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.
enum Bool { True, False, FileNotFound };
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
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
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.

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.
FTA, my 2D futuristic action MMORPG
Quote:Original post by pTymN
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.


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.
---Yesterday is history, tomorrow is a mystery, today is a gift and that's why it's called the present.

This topic is closed to new replies.

Advertisement