码迷,mamicode.com
首页 > 其他好文 > 详细

Pack and unpack a range of a float into a limited number of bits.

时间:2015-02-23 13:07:28      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!