Solved

# Array declaration

Posted on 1998-09-03
240 Views
I have an array of float values which range from 0.00 to 16.00. What can I do to the elements so that the memory needed to store the elements is minimized?
0
Question by:peiyoke
• 6
• 6
• 4

LVL 3

Accepted Solution

braveheart earned 10 total points
ID: 1252488
What is the precision to which you have to store them?  Do they only take nearly integral values, or can they be treated as such?

If you are only interested in two decimal places you can store them as short integers with values between 0 and 1600, or you could pack 3 such values into a 4 byte integer.

If your data are not uniformly distributed you may be able to use a more sophisticated packing method but you are unlikely to want anything like that.
0

LVL 84

Expert Comment

ID: 1252489
also, are there correlations between values in the array?
do the values always increase as the array index increases?
do consecutive entries tend to have close values?
0

Author Comment

ID: 1252490
braveheart,
could you elaborate more on your key point?

ozo,
yes, there are correlations between values in the array. but it is not necessary that the values increases as the array index increases.

0

LVL 84

Expert Comment

ID: 1252491
I think the key point is that we can take less memory to store the elements
if we know some limitations on what their values can be.
Do we?  What do we know about those limitations?
0

LVL 3

Expert Comment

ID: 1252492
peiyoke, if you can't proceed with the help we have given you, you will have to provide us with more information about the data.

With what accuracy or precision do you have to store the data?
How many decimal places or significant figures?

Are there any other interesting features about the data?
You have already told us the upper and lower bounds but is there anything important about the distribution or ordering of the values?

For instance, it may be more space efficient to store not the actual numbers but the difference between one number and the previous one, or the difference between each number and the mean value.  Maybe the numbers are all multiples of some value or at least share some common factors.
0

Author Comment

ID: 1252493

I need to have the exact values in float data type for more accuracy. I can't use any other values to represent the array elements.
0

Author Comment

ID: 1252494

The limitation on the array values is only the range from 0 to 16.
0

LVL 84

Expert Comment

ID: 1252495
If that's all the restriction you can give, then all you can save is a few bits per entry.
0

Author Comment

ID: 1252496

ozo,

how can I save the few bits per entry? what should I do to the array elements? please give me some advice. Thank you!!
0

LVL 3

Expert Comment

ID: 1252497
If you need the exact values, stored to the maximum precision that is permitted by the data type, and there is nothing remarkable about the data, what makes you think that you can compress the data?

Compression relies on the fact that the data are taking up more space than is necessary, either in each item of data, or in the distribution.
0

LVL 84

Expert Comment

ID: 1252498
If you need to save the full precision of your floating point data type,
we'll need to know exactly what that format is.
Is it IEEE format?  How many bits?
We could save the sign bit, and the few bits it takes to represent larger mantisas, but that's about it.

(But are sure you really need the full precison over the full range?)
If your floating point format can't distinguish between
15.999999999999998 and
15.999999999999999
then is there any use in distinguishing between
0.000000000000000000001 and
0.000000000000000000002 ?
If you only need to keep the same absolute error as a 64 bit IEEE  float,
then you can reduce your storage from 64 bits to 53 bits.

Or if you can know that some values are more likely to appear than others,
(either globally, or in the context of knowing the values of nearby numbers in the array)
then we can allocate fewer bits to more likely values, and more bits to less likely values.
The better you can predict which values are most likely, the fewer bits we'd need to specify which value actually occured.
0

Author Comment

ID: 1252499
Actually what I have been doing is that I have a set of values ranging from 0 to 256. Then I reduced all the values to a range of 0 to 16 using uniform quantization. To store the set of unsigned char values, I need 8 bits. Now that I have reduced the values from 256 levels to 16 levels, I hope to store the values using 4 bits.  If I am not taking the integral part of the new values, what should I do to my values?

Experts, I am a very fresh student in programming and C language. Can both of you explain in a simpler way, please?  Thanks to both of you for the help.

0

LVL 3

Expert Comment

ID: 1252500
Are these values in the range 0-256 i.e. integers, or are they 0.0-256.0, i.e. reals?

What is uniform quantization?

Why are you storing numerics as unsigned char?

You don't have to take the integral part of a value to store it as an integer.  If you are only interested in 2 decimal places, then multiply by 100 and store as an integer, for instance.

I don't know how to explain it any simpler.
0

LVL 84

Expert Comment

ID: 1252501
If each element can have one of 256 distinct values, the easiest way to store them,
while minimizing the memory needed to store the elements, is in a char.
If there are only 16 different values that each element can take,
then you can store them in 4 bits each, (but you lose 4 bits of precision converting them, which you said you didn't want to do)
0

Author Comment

ID: 1252502
ozo,

one last question. how do I store the array elements in 4 bits(what data type is that)?
0

LVL 84

Expert Comment

ID: 1252503
That would be packing two elements into a char.
or perhaps bit-field structure members.
0

## Featured Post

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to useâ€¦
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see soâ€¦
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.