Efficient implementation of natural logarithm (ln) and exponentiation

后端 未结 8 2368
借酒劲吻你
借酒劲吻你 2020-12-15 09:18

I\'m looking for implementation of log() and exp() functions provided in C library . I\'m working with 8 bit microcontro

8条回答
  •  天涯浪人
    2020-12-15 09:57

    If you don't need floating-point math for anything else, you may compute an approximate fractional base-2 log pretty easily. Start by shifting your value left until it's 32768 or higher and store the number of times you did that in count. Then, repeat some number of times (depending upon your desired scale factor):

    n = (mult(n,n) + 32768u) >> 16; // If a function is available for 16x16->32 multiply
    count<<=1;
    if (n < 32768) n*=2; else count+=1;
    

    If the above loop is repeated 8 times, then the log base 2 of the number will be count/256. If ten times, count/1024. If eleven, count/2048. Effectively, this function works by computing the integer power-of-two logarithm of n**(2^reps), but with intermediate values scaled to avoid overflow.

提交回复
热议问题