Go to Post But here's the beautiful thing about FIRST: you are supposed to make mistakes. - RunawayEngineer [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 11-02-2006, 09:57
Dan894 Dan894 is offline
Registered User
FLL #0894
Team Role: Programmer
 
Join Date: Jan 2006
Rookie Year: 2006
Location: Flint, Michigan
Posts: 6
Dan894 is an unknown quantity at this point
Programming Trigonometric Functions

What's the best way to do this. I've heard make a look up table (which I'm not entirely sure how to do anyway, I've never done arrays or array-like things in C), and Taylor polynomials. I've been trying the Taylor approach, but encounter this crude reality that most computers do NOT have infinite memory.

Is defining an integral exponential function like this (recursively) generally a bad idea?

float exp(float base, int exponent) {
if(exponent < 0)
return exp(base, -1*exponent)
else if(exponent == 0)
return 1;
else
return exp(base, exponent-1)
}

I did a similar thing with factorial...

unsigned long factorial (unsigned character n) {
if(n==0)
return 1;
else
return n*factorial(n-1);
}

I doubt I'd ever be using that for something bigger than 12! or so.

Of course, since I had some debugging to do, I wound up defining the trig functions using a much more brute force mentality:

float sin(float x) {
return x-(x*x*x)/6+(x*x*x*x*x)/120-(x*x*x*x*x*x*x)/5040+(x*x*x*x*x*x*x*x*x)/362880;
}
float cos(float x) {
return 1-(x*x)/2+(x*x*x*x)/24-(x*x*x*x*x*x)/720+(x*x*x*x*x*x*x*x)/40320;
}
Then I defined the other four using ratios... and the arctrig...

float arcsin(float x) {
return x + x*x*x/6 + 3*x*x*x*x*x/8 + 135*x*x*x*x*x*x*x/48 + 1215*x*x*x*x*x*x*x*x*x/384;
}

float arctan(float x) {
if(-1 < x && x < 1)
return x-x*x*x/3+x*x*x*x*x/5-x*x*x*x*x*x*x/7+x*x*x*x*x*x*x*x*x/9;
else if(x >= 1)
return pi/2-1/x+1/(3*x*x*x)-1/(5*x*x*x*x*x)+1/(7*x*x*x*x*x*x*x)-1/(9*x*x*x*x*x*x*x*x*x);
else
return -pi/2-1/x+1/(3*x*x*x)-1/(5*x*x*x*x*x)+1/(7*x*x*x*x*x*x*x)-1/(9*x*x*x*x*x*x*x*x*x);
}

And the remaining ones I defined using triangle identities.

Around the term x*x*x*x*x*x*x*x*x/362880, I start getting an error message like

Hence I just give up the accuracy and only get accurate to the 1.6^8/40320 or whichever term I gave up at. And I still don't trust that this code will, you know, actually work. (I also wound up defining
float pi = 3.141592653589793238462548838279; )
  #2   Spotlight this post!  
Unread 11-02-2006, 11:14
Keith Watson Keith Watson is offline
Registered User
FRC #0957 (WATSON)
Team Role: Mentor
 
Join Date: Feb 2006
Rookie Year: 2006
Location: Wilsonville, OR
Posts: 112
Keith Watson is just really niceKeith Watson is just really niceKeith Watson is just really niceKeith Watson is just really nice
Re: Programming Trigonometric Functions

What accuracy (how many decimal places) do you really need? For example, take an angles of 1.0, 1.1, 1.01, 1.001, etc and use those to calculate distances in the size of the playing field. What accuracy is good enough?

I forget which college math class it is but they teach how to stop the series based on how many decimal places of accuracy you want. Also consider how many bits is the variable you are using and what accuracy actually fits inside of that. Once these things are considered 20 decimals places for pi does not add to the accuracy.

If you choose to use the math library instead, those almost always have the accuracy and overflow issues sorted out.
__________________
Keith Watson - Professional Software Engineer
No relation to "Kevin" Watson, who created the camera tracking code.
  #3   Spotlight this post!  
Unread 11-02-2006, 11:39
Dan894 Dan894 is offline
Registered User
FLL #0894
Team Role: Programmer
 
Join Date: Jan 2006
Rookie Year: 2006
Location: Flint, Michigan
Posts: 6
Dan894 is an unknown quantity at this point
Re: Programming Trigonometric Functions

Am I going to have any problem with slow calculations using the Taylors?

There was an error too... Math section can not fit the section.

And in Calc II they cover Taylor's inequality, which for these trig functions, it gives my accuracy down to (pi/2)^n/n! where n is one bigger than the exponent in the last term.
  #4   Spotlight this post!  
Unread 11-02-2006, 12:27
Kevin Watson's Avatar
Kevin Watson Kevin Watson is offline
La Caņada High School
FRC #2429
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: La Caņada, California
Posts: 1,335
Kevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond repute
Re: Programming Trigonometric Functions

Quote:
Originally Posted by Dan894
...I've heard make a look up table (which I'm not entirely sure how to do anyway, I've never done arrays or array-like things in C)...
From a couple of days ago: http://www.chiefdelphi.com/forums/sh...78&postcount=4. If you try the search function, you'll find many posts on this topic.

-Kevin
__________________
Kevin Watson
Engineer at stealth-mode startup
http://kevin.org
  #5   Spotlight this post!  
Unread 14-02-2006, 20:27
esquared's Avatar
esquared esquared is offline
Keeps saying 3-2-1-Rush...
AKA: Angry Eric
no team (Volunteer!)
Team Role: Mascot
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Boston, MA
Posts: 192
esquared has a reputation beyond reputeesquared has a reputation beyond reputeesquared has a reputation beyond reputeesquared has a reputation beyond reputeesquared has a reputation beyond reputeesquared has a reputation beyond reputeesquared has a reputation beyond reputeesquared has a reputation beyond reputeesquared has a reputation beyond reputeesquared has a reputation beyond reputeesquared has a reputation beyond repute
Re: Programming Trigonometric Functions

Quote:
Originally Posted by Dan894
What's the best way to do this. I've heard make a look up table (which I'm not entirely sure how to do anyway, I've never done arrays or array-like things in C)...
We started with the command line version, and one of students developed a gui frontend, of the program in the following thread:

http://www.chiefdelphi.com/forums/sh...ad.php?t=44012

Does the lookup table generation for you.


-Eric
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming Vex w/ MPLab dababyjebus FIRST Tech Challenge 27 25-04-2008 09:11
Programming - Getting Started Mark McLeod Programming 80 16-04-2008 23:37
Functions of '?', ':', and '&' in the C programming language DanDon Programming 8 05-05-2005 09:25
Updated: Serial Port Driver Code Kevin Watson Programming 4 05-02-2005 18:39
Robot Programming Education phrontist Programming 11 03-05-2004 07:32


All times are GMT -5. The time now is 01:33.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi