Bots and Simulations > DNA - General
sin and cos approximation
Welwordion:
I tried to write a gene to aproximate sin and cos , did not test it yet but thought I would post it here so you can test it yourself and see if the code is correct or improve it.
--- Code: ---'cos x = 1 - x^2/2! + x^4/4!- ...(I kept the error small by using cos x = - cos (pi-x) and cos (x+pi) =- cos x such limiting x to <= pi/4)
1
*.aim 628 mod 314 floor 314 sub sgn -2 mult 1 add *.aim 314 mod 2 div mult *.aim 628 mod 314 floor 314 sub sgn 314 mult add
2 pow 200 div sub
*.aim 628 mod 314 floor 314 sub sgn -2 mult 1 add *.aim 314 mod 2 div mult *.aim 628 mod 314 floor 314 sub sgn 314 mult add
'splitted the numbers as we can only store numbers smaller than 10^9 and we need to minize rounding errors
3 pow 24000 div
*.aim 628 mod 314 floor 314 sub sgn -2 mult 1 add *.aim 314 mod 2 div mult *.aim 628 mod 314 floor 314 sub sgn 314 mult add
mult 1000 div add
'the next line corrects alegbraic sign
628 *.aim sub abs 313 floor 313 sub sgn 2 mult 1 sub mult
'sin x = x - x^3/3! + x^5/5! - ...(again kept the error small using sin x = sin (pi-x) and sin (x+pi)= - sin x )
*.aim 628 mod 314 floor 314 sub sgn -2 mult 1 add *.aim 314 mod 2 div mult *.aim 628 mod 314 floor 314 sub sgn 314 mult add
*.aim 628 mod 314 floor 314 sub sgn -2 mult 1 add *.aim 314 mod 2 div mult *.aim 628 mod 314 floor 314 sub sgn 314 mult add
3 pow 600 div 100 div sub
*.aim 628 mod 314 floor 314 sub sgn -2 mult 1 add *.aim 314 mod 2 div mult *.aim 628 mod 314 floor 314 sub sgn 314 mult add
'splitted the numbers as we can only store numbers smaller than 10^9 and we need to minize rounding errors
3 pow 24000 div
*.aim 628 mod 314 floor 314 sub sgn -2 mult 1 add *.aim 314 mod 2 div mult *.aim 628 mod 314 floor 314 sub sgn 314 mult add
2 pow mult 5000 div 100 div add
'the next line corrects alegbraic sign, note that when signum is zero x=pi and sin x =0 so there will happen no error
628 *.aim sub sgn mult
--- End code ---
Numsgil:
The theoretical basis is sound. The code is a little thick, so I don't necessarly see how you're dealing with the fact that angles are in weird units, and sin and cos generally return a number between -1 and 1. I'll just assume you are
PurpleYouko:
The angles are currently in pi radians except that we add a multiple for some strange reason.
1/4 turn is 314 in DB that is pretty close to pi * 100
Welwordion:
Well pi = 3,1415.. mutiplicate it with 200 and remove everything behind the comma and you have 618 which is
a half circle as well as pi is half a circle, so basically our angles are radian measure *200.
or better say our angles measurements are better to calculate sin and cos then angles in degree.
P.S.:^^ seems like yoko was faster then me
Note: I corrected a mistake as I was adding 1 instead of pi(314).
Note2: Just tried to test the concept but it seems somewhere is division trough 0 as It says the bot is invalid.
Welwordion:
ok I could make the bot run without error( he does not like if you divide with large numbers, so I just divided twice with samller numbers).
Ok however I still not know if the result is correct, could someone use youko programm to follow the operations on the stack?(it does not work on my pc , or I do not know how to make it work)
Note:I tested the stuff usinfgmovement of a rotating bot , its move a little whacky and not in the direction I exüected but it moves in one direction so there is a chnde it might work correctly.
Navigation
[0] Message Index
[#] Next page
Go to full version