Code functions that pack and unpack a range of a float into a limited number of bits.
unsigned int pack_float(float val, float minVal, float maxVal, int bits);
float unpack_float(unsigned int val, float minVal, float maxVal, int bits);
unsigned
int
pack_float(
float
val,
float
minVal,
float
maxVal,
int
bits)
{
assert
(bits >= 0 && bits < 32);
assert
(val >= minVal && val < maxVal);
return
(1U<<bits)*(val-minVal)/(maxVal-minVal);
}
float
unpack_float(unsigned
int
val,
float
minVal,
float
maxVal,
int
bits)
{
assert
(bits >= 0 && bits < 32);
assert
(val < (1U<<bits));
return
val*(maxVal-minVal)/(1U<<bits)+minVal;
}
时间: 2024-10-25 14:29:07