Author Topic: sin and cos approximation  (Read 7316 times)

Offline Welwordion

  • Bot Destroyer
  • ***
  • Posts: 325
    • View Profile
sin and cos approximation
« 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.

Code: [Select]
'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
« Last Edit: May 06, 2006, 04:35:06 AM by Welwordion »

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
sin and cos approximation
« Reply #1 on: May 05, 2006, 02:30:58 PM »
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

Offline PurpleYouko

  • Bot God
  • *****
  • Posts: 2556
    • View Profile
sin and cos approximation
« Reply #2 on: May 05, 2006, 02:41:04 PM »
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
There are 10 kinds of people in the world
Those who understand binary.
and those who don't

:D PY :D

Offline Welwordion

  • Bot Destroyer
  • ***
  • Posts: 325
    • View Profile
sin and cos approximation
« Reply #3 on: May 05, 2006, 02:44:56 PM »
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.
« Last Edit: May 06, 2006, 03:59:05 AM by Welwordion »

Offline Welwordion

  • Bot Destroyer
  • ***
  • Posts: 325
    • View Profile
sin and cos approximation
« Reply #4 on: May 06, 2006, 04:39:07 AM »
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.
« Last Edit: May 06, 2006, 07:47:28 AM by Welwordion »

Offline abyaly

  • Bot Destroyer
  • ***
  • Posts: 363
    • View Profile
sin and cos approximation
« Reply #5 on: May 06, 2006, 08:19:17 AM »
Where is this program available?
Lancre operated on the feudal system, which was to say, everyone feuded all
the time and handed on the fight to their descendants.
        -- (Terry Pratchett, Carpe Jugulum)

Offline PurpleYouko

  • Bot God
  • *****
  • Posts: 2556
    • View Profile
sin and cos approximation
« Reply #6 on: May 06, 2006, 11:22:05 AM »
It's an attachment to This 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.
There are 10 kinds of people in the world
Those who understand binary.
and those who don't

:D PY :D

Offline Welwordion

  • Bot Destroyer
  • ***
  • Posts: 325
    • View Profile
Re: sin and cos approximation
« Reply #7 on: January 25, 2012, 06:03:07 AM »
Code: [Select]
*.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.
« Last Edit: January 25, 2012, 06:21:42 AM by Welwordion »

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
Re: sin and cos approximation
« Reply #8 on: January 25, 2012, 01:27:12 PM »
Looks much leaner :)

Offline Welwordion

  • Bot Destroyer
  • ***
  • Posts: 325
    • View Profile
Re: sin and cos approximation
« Reply #9 on: January 25, 2012, 05:28:23 PM »
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.
« Last Edit: January 25, 2012, 05:37:19 PM by Welwordion »

Offline Botsareus

  • Society makes it all backwards - there is a good reason for that
  • Bot God
  • *****
  • Posts: 4483
    • View Profile
Re: sin and cos approximation
« Reply #10 on: March 15, 2012, 01:04:54 PM »
I am looking at adding Sin and Cos internally...

Quote
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)
« Last Edit: March 16, 2012, 04:39:33 PM by Botsareus »

Offline Botsareus

  • Society makes it all backwards - there is a good reason for that
  • Bot God
  • *****
  • Posts: 4483
    • View Profile
Re: sin and cos approximation
« Reply #11 on: March 16, 2012, 07:08:29 PM »
I just had an idea (I might have had this one before):

How about

Code: [Select]
A B Sin
will mean:

Code: [Select]
Push to stack =  Sin(B) * Abs(A)