## [Algorithms] Approximating the pow() function

 [Algorithms] Approximating the pow() function From: Guillaume Provost - 2004-04-26 19:20:42 ``` Sorry for asking a really dumb question, but have any of you worked around this issue? I want a cheap and fast approximation to the pow( ) function without going through the standard math libs (this needs to be platform independant / IEEE single precision floating point format). Unfortunately, the (relatively small) error introduced by the log base 2 gets compounded when I compute the exponential portion. This results in a +-50% (or one exponential bit) in relative accuracy when dealing with higher exponent values. Is there any other way to cheaply approximate pow( ) in a more precise way? (Code appended). I thought about using a polynomial to interpolate the mantissa portion across exponent values, but that only decreases the average error - not the worst case error. #define log2_e (0.69314718055994530941723212145818) #define invlog2_e (1 / 0.69314718055994530941723212145818) #define mantissa (float(0x07fffff)) #define inverseMantissa (1.0f / mantissa) // 50% relative accuracy on worse case! (high exponent values) float inline approxpow(float a_base, float a_exp) { long& nat_base = ((*(long *)&a_base)); float log2_base = ((nat_base & 0x7f800000) >> 23) - 127; // Linearly interpolate the mantissa across exponents log2_base += float(nat_base & 0x007fffff) * inverseMantissa; float exp = a_exp * log2_base; float reminder = (exp - __floor(exp)) * mantissa; int retval = (long(exp + 127) << 23 | long(reminder)); return *(float *)&retval; }; Guillaume. ```

 [Algorithms] Approximating the pow() function From: Guillaume Provost - 2004-04-26 19:20:42 ``` Sorry for asking a really dumb question, but have any of you worked around this issue? I want a cheap and fast approximation to the pow( ) function without going through the standard math libs (this needs to be platform independant / IEEE single precision floating point format). Unfortunately, the (relatively small) error introduced by the log base 2 gets compounded when I compute the exponential portion. This results in a +-50% (or one exponential bit) in relative accuracy when dealing with higher exponent values. Is there any other way to cheaply approximate pow( ) in a more precise way? (Code appended). I thought about using a polynomial to interpolate the mantissa portion across exponent values, but that only decreases the average error - not the worst case error. #define log2_e (0.69314718055994530941723212145818) #define invlog2_e (1 / 0.69314718055994530941723212145818) #define mantissa (float(0x07fffff)) #define inverseMantissa (1.0f / mantissa) // 50% relative accuracy on worse case! (high exponent values) float inline approxpow(float a_base, float a_exp) { long& nat_base = ((*(long *)&a_base)); float log2_base = ((nat_base & 0x7f800000) >> 23) - 127; // Linearly interpolate the mantissa across exponents log2_base += float(nat_base & 0x007fffff) * inverseMantissa; float exp = a_exp * log2_base; float reminder = (exp - __floor(exp)) * mantissa; int retval = (long(exp + 127) << 23 | long(reminder)); return *(float *)&retval; }; Guillaume. ```