c++ - Fast rounding function with variable precision? -
currently use following macro rounding:
my_round(inval,numdig) (((double)(int)((inval)*pow((double)10,numdig)+cnco_sign(inval)*0.5))/pow((double)10,numdig))
here inval input double rounded , numdig number of digits (=accuracy) round for.
due divisions , pow()'s function quite slow. so: there better replacement not waste time?
thanks!
this solution:
const double pow_table[] = {1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9}; const double inv_pow_table[] = {1, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7, 1e-8, 1e-9}; #define my_round(inval,numdig) (floor((inval)*pow_table[numdig]+0.5)*inv_pow_table[numdig])
Comments
Post a Comment