快速出去一趟谷歌带来了http://sourceforge.net/projects/fixedptc/我注意
它是一个头文件中的ac库,用于管理32或64位整数的定点数学。
实验用下面的代码一点点:
#include <stdio.h>
#include <stdint.h>
#define FIXEDPT_BITS 64
#include "fixedptc.h"
int main(int argc, char ** argv)
{
unsigned int input = 536838144; // min 0, max 1073676289
unsigned int range = 1155625; // min 0, max 1155625
// Conversion
unsigned int tmp = (input >> 16) * ((range) >> 3u);
unsigned int output = (tmp/((1073676289) >> 16u)) << 3u;
double output2 = (double)input * ((double)range/1073676289.0);
uint32_t output3 = fixedpt_toint(fixedpt_xmul(fixedpt_fromint(input), fixedpt_xdiv(fixedpt_fromint(range), fixedpt_fromint(1073676289))));
printf("baseline = %g, better = %d, library = %d\n", output2, output, output3);
return 0;
}
给我弄了以下结果:
baseline = 577812, better = 577776, library = 577812
显示更高的精确度(相匹配的浮点),比你用你的代码获得。引擎盖下它没有做任何事情非常复杂(没有在32位工作在所有)
/* Multiplies two fixedpt numbers, returns the result. */
static inline fixedpt
fixedpt_mul(fixedpt A, fixedpt B)
{
return (((fixedptd)A * (fixedptd)B) >> FIXEDPT_FBITS);
}
/* Divides two fixedpt numbers, returns the result. */
static inline fixedpt
fixedpt_div(fixedpt A, fixedpt B)
{
return (((fixedptd)A << FIXEDPT_FBITS)/(fixedptd)B);
}
但它确实表明,你可以得到你想要的精确度。你只需要64位就可以做到这一点
你想要的输出范围是什么? (是(a * b)<= 1.0?如果a * b <= 1.102(* 2^n)的最大值被允许,事情会更快)(并且很明显float/double是不成问题的) –