标签:
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;
}
Pack and unpack a range of a float into a limited number of bits.
标签:
原文地址:http://www.cnblogs.com/mynamepfd/p/4297962.html