# Thread: any good ideas

1. ## any good ideas

Hi,

Just a quick question. Is it possible (in any way) to compress a double into a smaller varaible. Say unsigned short etc. This to decrease the size of a struct with a few doubles in it. What i want to do is compress a struct and then be able to decompress it. I have given it a though and are playing with the idea of ecrypting (maybe?) and then decrypting to get the true value back. But i need some ideas (if this is possible) on what i should do?

G'n'R

2. > Is it possible (in any way) to compress a double into a smaller varaible. Say unsigned short etc
If the double values are actually integers in the range of unsigned short, then yes (and why aren't you storing them as such if this is so).

Otherwise you will lose information when you truncate the value.

> What i want to do is compress a struct and then be able to decompress it
Unless this is a large structure, and you need to transmit it over a slow link, I see no point. Small amounts of binary data do not compress very well.

If you encrypt it, you won't be able to compress it.

3. Well the thing is... It is a large struct and i need doubles to store large values. However, i got limited storage area. I works now, but it would be a great advantage to make it smaller. Hence, the idea about maybe encrypting the values in the double with a special code to "compress" it and then using that code to "decompress" it when reading it back. e.g. Say i got 274877906943 in the double and then (here is where i need some good ideas) somehow (by division or something else) make that number smaller for storage for so turn it back into its true value later.

Cheers
G'n'R

4. doubles are typically stored like this
&nbsp;1 sign bit
10 exponent bits
53 mantissa bits
64 bits in total.

Now, if you're saying that these doubles are effectively large integers, which contain less than 15 decimal digits ( 274877906943 is 12 digits) then you can pack these into 54 bits instead of 64 bits.

A 12 digit number only needs 40 bits to accurately represent it.

5. Well, there are 2 types of data compression, lossy and lossless.

Lossy compression would be what you'd do if you converted each double to some smaller data type. A double has 64 bits, an unsigned short would have 16 bits, so many doubles would have to map to the same unsigned short -- meaning you couldn't prefectly reconstruct the original. Salem's suggestion is lossy, as well -- it would shorten the value by discarding the least significant bits. Most video and audio compression works like this.

Lossless would let you perfectly reconstruct the original, but not all data could be compressed, and sometimes you actually make files larger by compressing. ZIP files are an example of lossless compression, they work by finding patterns in the data and saving the patterns instead of the raw data. Sparse matrices are very good candidates for lossless compression, as they contain many 0 values.

Popular pages Recent additions