在某些不需要那么高精度的log2函数,单要求高速的地方,可以使用以下方法来减少log2的运算量,但精度全靠内存堆,若要提高精度需要把flog2map的长度增加,该表由注释部分代码生成。
当表修改后,对应的“temp = (temp>>15)&0xFF;”也需要修改。
比如,当表长为32时候,就是temp = (temp>>18)&0x1F;
因为32需要5bit来表达
18 = 23-5
0x1F = 0b 0001 1111
#include <stdio.h> #include "math.h" /* 表生成算法flog2map for i = 0:256 temp = i/256; templog = log2(1+temp); fprintf('%f,\n',templog) end */ #define uint8_t unsigned char float flog2map[256]={
0.000000,0.005625,0.011227,0.016808,0.022368,0.027906,0.033423,0.038919,0.044394,0.049849,0.055282,0.060696,0.066089,0.071462,0.076816,0.082149,0.087463,0.092757,0.098032,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,1.000000,}; float flog2(float x) {
unsigned int temp,i=0; int outint; float tempf,templog; //整数部分 temp = *(unsigned int*)&x; outint = (temp>>23&255)-127; //小数部分 temp = (temp>>15)&0xFF; return outint + flog2map[temp]; } int main() {
float input; float output1; float output2; float i; output2 = flog2(0.04); for(i=-10;i<10;i+=0.1){
input = pow(2,i); output1 = log2f(input); output2 = flog2(input); printf("%f %f\n",output1,output2); } return 0; }
讯享网

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/80030.html