Parallax-Stamp Code

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
'_______________________________________________________
 

hippy

Ex-Staff (retired)
It is usually possible to rewrite programs or algorithms written for other micros for a PICAXE but maths, very large numbers and floating point can prove difficult.

In those cases it's usually easier to approach it from a 'what do you actually want to achieve?' direction. Then find the best way a PICAXE could do that rather than simply try to convert a line at a time.

In this case it may be that it may be possible as it seems to use the same constraints a PICAXE has. The only line I'm not too sure about and cannot figure out what it means is "lgx0(k)=x2f".
 

hippy

Ex-Staff (retired)
Close. At least when simulated it prints out numbers which closely match those in the comments of the original code. Note sure where the 'off by 1' errors marked with * are, in this code or in the original comments, or a subtle difference in the way the PICAXE / simulator does its maths ...

Code:
12041       prints 12041 (decimal conversion)
8781        as fraction e.g. 8781/32768
2679 *      log2 x = 2680/10000=0.2680 (2^0.2680 = 1.2041)
8066 *      log10 x = 8067/100000 =0.08067 (10^0.08067=1.2041)
1857        ln x = 1857/10000=0.1857
Code:
#Picaxe 08M2

Symbol x    = w0    ; b1:b0
Symbol x2   = w1    ; b3:b2
Symbol lgx  = w2    ; b5:b4
Symbol bitW = w3    ; b7:b6
Symbol tmpW = w4    ; b9:b8

TestProgram:
  x = 39457
  tmpW = x ** 20000   : SerTxd( #tmpW, CR, LF )
  Gosub CalculateLog
  tmpW = lgx          : SerTxd( #tmpW, CR, LF )
  tmpW = lgx ** 20000 : SerTxd( #tmpW, CR, LF )
  tmpW = lgx ** 60206 : SerTxd( #tmpW, CR, LF )
  tmpW = lgx ** 13863 : SertXd( #tmpW, CR, LF )
  End

CalcLog:
  lgx = 0
  bitW = $4000
  Do
    x2 = x ** x
    If x2 >= $8000 Then
      lgx = lgx | bitW
      x = x2
    Else
      x = x * x / $8000 + x2 + x2
    End If
    bitW = bitW / 2
  Loop Until bitW = 0
  Return
 
Last edited:

hippy

Ex-Staff (retired)
Note sure where the 'off by 1' errors marked with * are, in this code or in the original comments, or a subtle difference in the way the PICAXE / simulator does its maths
The values returned by the PICAXE are correct when calculated by hand ...

8781 ** 20000 => ( 8781 * 20000 ) / 65536 => 2679.7485 => 2679

8781 ** 60206 => ( 8781 * 60206 ) / 65536 => 8066.8470 => 8066

And for the record, "lgx0(k)=x2f" meant set bit 'k' in 'lgx' to the bit value of 'x2f'.

So, barring any implementation bugs, that seems to be done and dusted.
 
Top