This particular code may be useful when you want to show analog values with a color-code on a led or similar.
Full details here: http://irnas.eu/2015/03/25/logarithm-integer-system/
-- ESP8266 running nodemcu lua - integer
-- 492us for x=1000 and 221us for x=1
function log_approx (x)
E=272
ln10=230
ans=0
res=0
while x > 0 do
res =res*100/E
a1 = x*10000/E
x = a1/100;
res = res+a1-x*100;
ans=ans+1;
if res>100 then
x=x+1;
res=res-100;
end
end
res = res-100 - (100-res)*(100-res)/200
ans = (ans*100+res)/100
return ans*1000/ln10
end
-- ESP8266 running nodemcu lua - integer
-- 433us for x=1000 and 195us for x=1
input_val={2,3,4,5,6,7,9,12,16,20,25,32,40,50,63,79,100,126,158,200,251,316,398,501,631,794,1000,1025}
output_val={0,3,4,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30}
function log_approx2 (x)
for i,v in ipairs(input_val) do
if(x<v) then
do return output_val[i] end
end
end
return 0
end
