Tuts 4 You

# Compressing floats in a tiny format

## Recommended Posts

Hi.

I want to store floats in a tiny format. As small as possible eg to 8bit or 16bit.

Anyone an idea how that could be done?

I need to have a precicion of 3 digits behind the comma.

##### Link to comment

I would say it depends on the range in front of the comma

Is it a known (small) range? If yes, you could try crippling down

the precision from top and bottom. Knowing the range and making

use of it would be one possible way.

##### Link to comment

I once compressed the data of a 3D object that way.

Example for this fast, easy but lazy compression:

You check your values and get MIN and MAX.

Let's say the range is from -800.000 to 800.000,

which is a full range of 1600000 different numbers.

Now how to get 1600000 different numbers into words,

which only got a range of 65535 numbers?

Divide 1600000 by 65535, which is ~25.

Now divide all values by 25 in order to 'compress' them:

1) (-800.000) -800000 : 25 = 0x7D00

2) ( 0.555) 555 : 25 = 0x0016

3) ( 777.777) 777777 : 25 = 0x7987

4) ...

In order to uncompress them multiply by 25:

1) 0x7D00 * 25 = 800000 ( 800.000)

2) 0x0016 * 25 = 550 ( 0.550)

3) 0x7987 * 25 = -800000 (-777.775)

4) ...

You see that you also loose a small amount of precision in the end..

PS: Please somebody come up and tell how stupid this was.

A more intelligent compression is wanted ^^

##### Link to comment

I guess the easiest way is to just store the main integer in as few bits as possible and then store all 3 post-comma digits in 4bits each

if you know the main integer is between 0 and 16, you can store that in 4 bits too and you've got your 16bit

generally it will be 2^log2(max_value) bits (round up )

other than that, try writing them all to a file and zip it :?

##### Link to comment

It's hard to answer the question without additional information. Why do you need it? What's the smaller/largest number you need to store? Do you need to store exact number or some loss of precision is acceptable? Do you need extremely fast code for reading/storing these floats or it can be some complex algo?

Look at IEEE-754 standard, they have specified also 16-bit half-precision binary format. Human-understandable description is here: http://en.wikipedia.org/wiki/Half_precision_floating-point_format

##### Link to comment

I currently have an idea to store in one byte (Bit 8=Sign, bits 0-7 bits for the floats)

Currently im codeing on that idea. After trying out ill report.

@KAO:

Its for storing datas of 3D Objects in 4/64k Intros.

It's hard to answer the question without additional information. Why do you need it? What's the smaller/largest number you need to store? Do you need to store exact number or some loss of precision is acceptable? Do you need extremely fast code for reading/storing these floats or it can be some complex algo?

Look at IEEE-754 standard, they have specified also 16-bit half-precision binary format. Human-understandable description is here: http://en.wikipedia.org/wiki/Half_precision_floating-point_format

##### Link to comment
Its for storing datas of 3D Objects in 4/64k Intros.
Yay, got it. But storing it in a byte would mean a resolution of 256x256x256 for your objects then. Isn't it too tight?

Anyway I think it's the right way to go for some division then, since it's imho not really noticeable in 3D models, when shrinking the precision.

Reminds of using 'Posterize' in Photoshop. The pic doesn't really get worse, but the size goes down amazingly.

Edited by Ufo-Pu55y

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account