Darwinbots Forum
Bots and Simulations => DNA - General => Topic started by: Welwordion on May 05, 2006, 02:22:54 PM
-
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.
'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
-
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
-
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
-
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.
-
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.
-
Where is this program available?
-
It's an attachment to This (http://www.darwinbots.com/Forum/index.php?s=&showtopic=1214&view=findpost&p=1365989) post
It runs in Microsoft Excel sp obviously you need to have this installed on your PC.
Oh and one more thing. It isn't quite complete and doesn't include specific code for all the commands yet. Chances are it won't be able to handle Welworion's bot yet as he uses some of the more recent math functions.
-
*.refxpos *.refypos angle -1 mult *.aim add
'make sure the angle is within the right range
1256 add 1256 mod
'forgot which one was sin and which cos, the ceil and floor are used for the purpose of improving precision, as its easier to do values in a certain area
' and since sin and cos is a symetric function we used this to our advantage I also used only sine and just changed the input value by 314 '90 degree
dup dup dup dup dup 314 ceil swapint
314 floor 942 ceil 314 sub sub swapint
942 floor 942 sub add 2 div
dup 0 swapint 3 pow 600 div 100 div sub add
2 div *.veldx sub .dx store
628 ceil -1 mult 314 add swapint
628 floor 628 sub add 2 div
dup 0 swapint 3 pow 600 div 100 div sub add
2 div *.velup sub .up store
Know its an really old post of mine but I realized I never put the newest version of the code here.
-
Looks much leaner :)
-
yep made some tricky moves as I had explained back in this post,
http://forum.darwinbots.com/index.php/topic,3001.msg1378841.html
actually I think its the last version I posted in the following post back in 2007
http://forum.darwinbots.com/index.php/topic,2408.msg1374432.html
but when I saw the post listed below, which also tackle the sin, cos problem
http://forum.darwinbots.com/index.php/topic,3078.msg1379970.html
http://forum.darwinbots.com/index.php/topic,3367.msg1382216.html
I thought either my solution was forgotten or people did not like it for some reason
so I thought might be helpful to bring it back to memory for those that face the same problem
or give people the chance to tell me of a more elegant way.
-
I am looking at adding Sin and Cos internally...
sin and cos generally return a number between -1 and 1.
I would like to know the value range we going to use???
(It is part of my work for a chloroplast version over the summer, since we are modifying the DNA anyway, I have considered adding a bunch of other functions I have discussed in my old posts, one in particular will be the good old multstore)
-
I just had an idea (I might have had this one before):
How about
A B Sin
will mean:
Push to stack = Sin(B) * Abs(A)