#define SCALE (1 << 31)
#define fix_Q31_80(x) ( (int) ( (float)(x)*(float)0x80000000 ) )
#define fix_Q31_SC(x) ( (int) ( (float)(x)*(float)SCALE ) )
int main()
{
int fix_80 = fix_Q31_80(0.5f);
int fix_sc = fix_Q31_SC(0.5f);
}为什么fix_80和fix_sc的值不同?
fix_80 == Hex:0x40000000
fix_sc == Hex:0xc0000000发布于 2017-01-18 19:26:15
0x80000000是一个大数字,需要32位来表示该数字。这意味着在32位系统上(或在32位兼容应用程序中),int太小。所以改用unsigned long:
#define SCALE (1u << 31)
#define fix_Q31_80(x) ( (unsigned long) ( (float)(x)*(float)0x80000000u ) )
#define fix_Q31_SC(x) ( (unsigned long) ( (float)(x)*(float)SCALE ) )
int main()
{
unsigned long fix_80 = fix_Q31_80(0.5f);
unsigned long fix_sc = fix_Q31_SC(0.5f);
}https://stackoverflow.com/questions/41717188
复制相似问题