/* * (c) Ian Stephenson * * ian@dctsystems.freeserve.co.uk * * Fast pow() reference implementation */ #include float log2(float i) { return(log(i)/log(2)); } float shift23=(1<<23); float OOshift23=1.0/(1<<23); float myLog2(float i) { float LogBodge=0.346607f; float x; float y; x=*(int *)&i; x*= OOshift23; //1/pow(2,23); x=x-127; y=x-floorf(x); y=(y-y*y)*LogBodge; return x+y; } float myPow2(float i) { float PowBodge=0.33971f; float x; float y=i-floorf(i); y=(y-y*y)*PowBodge; x=i+127-y; x*= shift23; //pow(2,23); *(int*)&x=(int)x; return x; } float myPow(float a, float b) { return myPow2(b*myLog2(a)); } main() { float i; float toterr=0; float maxerr=0; for(i=0.02;i<256;i+=0.0001) { //float val=pow(2,log2(i)); //float myVal=myPow2(myLog2(i)); float val=powf(0.1,1/i); float myVal=myPow(0.1,1/i); float err=fabs(val-myVal)/val; printf("%f\t%f,%f\t%f%%\n",i,val,myVal,err*100); toterr+=err; //if(err>maxerr) // maxerr=err; } printf("%f:%f\n",toterr,maxerr); }