I've recently mapped my 1990s vintage HC11 sin/cos approximation over to Picaxe 20M2 and figured it may be of interest. I used a combination of series expansions, Chebyshev polynomials and a bit of fine tuning.
The code is about 115 bytes and gives 3+ significant figures over most of the range 0.0 - 90.0 degrees.
The program below is a small demo, producing comma delimited results, to check accuracy against Excel.
I hope it demonstrates the you can do some pretty serious number crunching with Picaxe's 16bits.
Here are the Simulator test results compared to Excel:
The code is about 115 bytes and gives 3+ significant figures over most of the range 0.0 - 90.0 degrees.
The program below is a small demo, producing comma delimited results, to check accuracy against Excel.
I hope it demonstrates the you can do some pretty serious number crunching with Picaxe's 16bits.
Code:
Symbol LoopCnt = w11 ' Used in Demo Code.
Symbol X = w12
Symbol Temp = w13
Symbol StringBuf = $60 ' 32 byte buffer starts here
Symbol StackX = $40 ' Save X reg
SinCosTest:
For LoopCnt= 0 to 900 step 50[INDENT]X=loopcnt : Gosub PrintX
Gosub SinX : Gosub PrintX
X=loopcnt
Gosub CosX : Gosub PrintX
serout b.1,N2400,(13) ' b.1 for Simulator
Next Loopcnt
[/INDENT]
Stop
PrintX: 'Comma delimited for pasting into Excel
Bptr = StringBuf
bintoascii X,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc
Bptr = StringBuf
serout b.1,N2400,(@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,", ")
Return
'End of Test Prog
' ***************************************************
' Subroutine Uses Sin(x) = 1.743*X - 0.00000085*X^3
' Calculation scaled for X in 1/10 Degrees. .
' ***************************************************
SinX:
If X>450 Then : X=900 - X: Goto CosX : Endif ' 0 <= X <= 450
Temp = X*100**57120
X = X/4*X/25*17+200/400*X+5/10
X = Temp - X /5 ' X = 10000*Sin(X)
Return
' ***************************************************
' Subroutine Uses Cos(x) = 1 - 0.3092X - 0.13899*X^2
' And Cos(x) = 1 - 0.5*X^2 for small values of X
' Calculation scaled for X in 1/10 Degrees.
' ***************************************************
CosX:
If X>450 Then : X=900 - X : Goto SinX : Endif
If X < 65 Then : X = X*X + 33 /66: X = 10000 - X: Else[INDENT]Temp = x/2 ' 0 <= X <= 450
Temp= Temp * Temp ** 21705
X = 13*X/7
X = 60000 - X - Temp/6 ' X = 10000*Cos(X/10)
[/INDENT]
EndIf
Return
Degrees | SinX | Sin(X) | CosX | Cos(X) | ||
0 | 0 | 0 | 10000 | 10000 | ||
50 | 870 | 872 | 9962 | 9962 | ||
100 | 1735 | 1736 | 9831 | 9848 | ||
150 | 2587 | 2588 | 9643 | 9659 | ||
200 | 3418 | 3420 | 9386 | 9397 | ||
250 | 4227 | 4226 | 9060 | 9063 | ||
300 | 5001 | 5000 | 8665 | 8660 | ||
350 | 5737 | 5736 | 8201 | 8192 | ||
400 | 6428 | 6428 | 7668 | 7660 | ||
450 | 7070 | 7071 | 7066 | 7071 | ||
500 | 7668 | 7660 | 6428 | 6428 | ||
550 | 8201 | 8192 | 5737 | 5736 | ||
600 | 8665 | 8660 | 5001 | 5000 | ||
650 | 9060 | 9063 | 4227 | 4226 | ||
700 | 9386 | 9397 | 3418 | 3420 | ||
750 | 9643 | 9659 | 2587 | 2588 | ||
800 | 9831 | 9848 | 1735 | 1736 | ||
850 | 9962 | 9962 | 870 | 872 | ||
900 | 10000 | 10000 | 0 | 0 |
Last edited: