elektriker
New Member
Hello!
A question for the Picaxe specialists.
Is it possible to rewrite this parallax-stamp-code for Picaxe 08M2?
Thank you very much.
'__________________________________________________________________
' LOGRTHM.BS2
' logarithm base 2 by calculation
' y = lg n
' for 1<=n<2 0<=y<1
' represented on the Stamp as fractions
' 32768/32768<=n<=65535/32768
' 0/32768<=y<=32767/32768
x var word ' input, a number between 1(32768) and 2(65536)
xf var x.bit15 ' the most significant bit of x
x2 var word ' auxiliary variable, high word of x^2
x2f var x2.bit15 ' msb of x2
lgx var word ' result, log base 2 of x
lgx0 var lgx.bit0 ' lowest bit of lgx, for bit addressing
bitk var bit ' auxiliary bit for calculation
k var nib ' index for steps of approximation
x=39457 ' example x=39457/32768 = 1.204132
debug dec x**20000,cr ' prints 12041 (decimal conversion)
lgx=0 ' initialize logarithm
for k=14 to 0 ' 15 bit result
x2=x**x ' square of x, high word
x=x*x ' low word
lgx0(k)=x2f ' this bit is 1 if x^2>=2
bitk=~x2f ' complement it for calculation
x=x2<<bitk+(bitk&xf) ' adjusted value of x
next ' next bit
debug dec lgx,32,cr, lgx**20000
' print the result
' as fraction e.g. 8781/32768
' as decimal value e.g.
' log2 x = 2680/10000=0.2680 (2^0.2680 = 1.2041)
debug dec lgx**60206,cr,dec lgx**13863
' log10 x = 8067/100000 =0.08067 (10^0.08067=1.2041)
' ln x = 1857/10000=0.1857
'_______________________________________________________
A question for the Picaxe specialists.
Is it possible to rewrite this parallax-stamp-code for Picaxe 08M2?
Thank you very much.
'__________________________________________________________________
' LOGRTHM.BS2
' logarithm base 2 by calculation
' y = lg n
' for 1<=n<2 0<=y<1
' represented on the Stamp as fractions
' 32768/32768<=n<=65535/32768
' 0/32768<=y<=32767/32768
x var word ' input, a number between 1(32768) and 2(65536)
xf var x.bit15 ' the most significant bit of x
x2 var word ' auxiliary variable, high word of x^2
x2f var x2.bit15 ' msb of x2
lgx var word ' result, log base 2 of x
lgx0 var lgx.bit0 ' lowest bit of lgx, for bit addressing
bitk var bit ' auxiliary bit for calculation
k var nib ' index for steps of approximation
x=39457 ' example x=39457/32768 = 1.204132
debug dec x**20000,cr ' prints 12041 (decimal conversion)
lgx=0 ' initialize logarithm
for k=14 to 0 ' 15 bit result
x2=x**x ' square of x, high word
x=x*x ' low word
lgx0(k)=x2f ' this bit is 1 if x^2>=2
bitk=~x2f ' complement it for calculation
x=x2<<bitk+(bitk&xf) ' adjusted value of x
next ' next bit
debug dec lgx,32,cr, lgx**20000
' print the result
' as fraction e.g. 8781/32768
' as decimal value e.g.
' log2 x = 2680/10000=0.2680 (2^0.2680 = 1.2041)
debug dec lgx**60206,cr,dec lgx**13863
' log10 x = 8067/100000 =0.08067 (10^0.08067=1.2041)
' ln x = 1857/10000=0.1857
'_______________________________________________________