我必须读取 4 个字节或 8 个字节的定点数,并且每个项目的小数点位置移动(已知位置)(刻度数)
有没有自动化的图书馆?C/C++ 是首选语言
例如:
这是假设规模为 20
double toDoublePrecisionFixedPoint(short first,short second,short third,short forth)
{
double d = 0;
int top = (first << 0x10) | (second & 0x0000FFFF);
top/=8;
top >>= 8;
d+=top;
long long a = 0x0;
a = ((long long)second&0x07FF)<< 0x20;
long long t = 0x0;
t = (((long long)third) << 0x10) & 0xFFFF0000;
long long f = 0x0;
f = (((long long) forth)) & 0xFFFF;
long long bottom = a | t | f;
long long maxflag= 0x80000000000;
double dlong = (double)bottom/(double)maxflag;
d += dlong;
return d;
}
假设比例为 15:
float toSinglePrecisionFixedPoint (short first, short second)
{
float f;
float dec = ((float)second) / ((float)0x10000);
f = (float)first;
if(f> 0 && dec >0)
f += dec;
else if(f >0 && dec <0)
f += (1 + dec);
else if(f < 0 && dec < 0)
f += dec;
else if(f < 0 && dec >0)
f -= (1 - dec);
else if(f == 0)
f += dec;
return f;
}
void floatToShorts(float f,short*ret)
{
ret[0] = 0x00;
ret[1] = 0x00;
ret[0] = (short)f;
double decimal = 0;
//THIS IS REMOVING THE WHOLE NUMBER
modf(f , &decimal);
ret[1] = (short)(decimal * 0x10000);
}
void doubleToShorts(double d,short*ret)
{
ret[0] = 0x00;
ret[1] = 0x00;
ret[2] = 0x00;
ret[3] = 0x00;
d*=0x80000000000;
long long l = (long long)d;
ret[0] = ((short)((l & 0xFFFF000000000000) >> 48));
ret[1] = ((short)((l & 0x0000FFFF00000000) >> 32));
ret[2] = ((short)((l & 0x00000000FFFF0000) >> 16));
ret[3] = ((short)((l & 0x000000000000FFFF)));
}
这对我来说没问题,直到我的项目现在需要一个可变比例的位置。这很好 - 但我只是好奇是否有更好的方法来做到这一点?必须有一个完整的图书馆
这很快就复杂了,我很容易让我的代码无法工作——我也确信我没有做我应该做的那么多的错误检查——所以很好奇是否有一个库。