Trig functions on Atmega168  arduino
Moderator: phalanx

 Posts: 31
 Joined: Tue Dec 30, 2008 1:19 pm
Trig functions on Atmega168  arduino
looked around a little. Is there anyway to calculate trig funtions without a lookup table? =/

 Posts: 23
 Joined: Sun Jan 10, 2010 1:22 pm
 Location: The Wired 2.0
Re: Trig functions on Atmega168  arduino
Quick note I'd like to add to this. Most likely the provided functions in the arduino core library are just directly inherited from avrlibc. I have to look at the sources, but I doubt they would reinvent the wheel. If that is the case, please look at the following link, specifically at the time it takes to execute these functions in your AVR chip.
http://www.nongnu.org/avrlibc/userman ... marks.html
If your application is timing critical, a lookup table may be the best method. As an example, sin(1.2345) will take 1653 / (16 MHz) = 103.3125 microseconds to execute. Using a lookup table not only may reduce the time it takes for the function to execute, it may even make your memory (both flash and RAM) footprint smaller.
http://www.nongnu.org/avrlibc/userman ... marks.html
If your application is timing critical, a lookup table may be the best method. As an example, sin(1.2345) will take 1653 / (16 MHz) = 103.3125 microseconds to execute. Using a lookup table not only may reduce the time it takes for the function to execute, it may even make your memory (both flash and RAM) footprint smaller.
C code. C code run. Run, code run... Please?

 Posts: 31
 Joined: Tue Dec 30, 2008 1:19 pm
Re: Trig functions on Atmega168  arduino
ohh duhh... i didn't look to see if one was included
haha 103 microseconds per calculation wow! i guess it's going to take some major code tweaking to get about 4 sig figs on an integration
for what it's worth here's the code for c++ using the rectangle method to approx an integration
to verify code is working:
haha 103 microseconds per calculation wow! i guess it's going to take some major code tweaking to get about 4 sig figs on an integration
for what it's worth here's the code for c++ using the rectangle method to approx an integration
Code: Select all
double f(double x){
return sin(x)/x;
}
double integrate(double a,double b,int N){
double dx = (b  a) / N;//delta x, not instantaneous
double sum = 0;
for(int iter = 1; iter < N; iter++){
double x = a + iter*dx;
sum += f(x);
}
return sum*dx;
}
double integrateFull(double a, double b){
double area1 = integrate(a,b,1);
double area2 = integrate(a,b,2);
int N=2;
while(fabs(area1  area2)> .00001){
area1 = area2;
N+=5;
area2 = integrate(a,b,N);
}
cout << "\nN is:"<<N<<endl;
return area2;
}
Re: Trig functions on Atmega168  arduino
Floating point on a $3 microprocessor a'int fast!
Best to find a way to get the algorithm to work with you using fixed point arithmetic. If you need speed.
Best to find a way to get the algorithm to work with you using fixed point arithmetic. If you need speed.
Re: Trig functions on Atmega168  arduino
Reminds me of:thefatmoop wrote:ohh duhh... i didn't look to see if one was included
to verify code is working:
(xkcd.com)