# SparkFun Forums

### Trig functions on Atmega168 - arduino

Your source for all things Atmel.

### Trig functions on Atmega168 - arduino#98056

By thefatmoop
#98056
looked around a little. Is there anyway to calculate trig funtions without a lookup table? =/

### Re: Trig functions on Atmega168 - arduino#98131

By OrlandoArias
#98131
Quick note I'd like to add to this. Most likely the provided functions in the arduino core library are just directly inherited from avr-libc. 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/avr-libc/user-man ... 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.

### Re: Trig functions on Atmega168 - arduino#98179

By thefatmoop
#98179
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
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;
}
``````
to verify code is working: ### Re: Trig functions on Atmega168 - arduino#98207

By stevech
#98207
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.

### Re: Trig functions on Atmega168 - arduino#98510

By ruurdjan
#98510
thefatmoop wrote:ohh duhh... i didn't look to see if one was included
to verify code is working: Reminds me of: (xkcd.com)