Copy Link
Add to Bookmark
Report

Fast sin and cos calculations using Euler's formula

DrWatson's profile picture
Published in 
atari
 · 11 months ago

  • File : Sincos.tut
  • Date : 7.2.1997
  • Author : [CrY0] / Quasar
  • Email : uzs3e7@ibm.rhrz.uni-bonn.de
  • Description : Fast sin and cos calculations using Euler's formula.
  • Source code : Borland / Turbo C++ 3.1.

[Intr0]

Hi folks, [CrY0] here. Actually i don't know how many people will ever need to do real time sin and cos calculations, since almost everything can be done with tables. At least, that's what i thought, but yesterday i met Kory on IRC channel #coders, and he really needed to do that for a prog. he was coding. So i called up on my math knowledge and came up with a quite quick method to do the job, at least, as long as you don't mind having accuracy losses. :)

I know many others have done this before, it is nothing new, but you can always use another tutor, right?

[MatH]

Ok, if you have had higher trig, and you know what a sum ( represented by the greek letter Sigma ) and the constant e is, then skip this part and go straight for the source code later on the text.

The method i will explain here is based on Euler's formula.

First, we need to define some stuff and notation:

a)

n! = \prod_{k=1}^{n} k = 1 \cdot 2 \cdot 3 \cdot \ldots \cdot n

example: 4! = 1 \cdot 2 \cdot 3 \cdot 4 = 24

b) (Where E means 'the sum of' , i couldn't find Sigma on my keyboard :) )

e = \sum_{n=0}^{\infty} \frac{1}{n!}

e = Euler's number. This is a constant. The value is not of interest for us now, so just read on :)


c) Euler's Formula:

e^{ix} = \cos(x) + i \sin(x)

where i is the square root of -1 (complex).


Now we can use b) and write Euler's formula like this:

e^{ix} = \sum_{n=0}^{\infty} \frac{{(i \cdot x)}}{{n!}} = \sum_{n=0}^{\infty} {i^n} \cdot {\frac{x^n}{n!}} \cdot \sum_{k=0}^{\infty} (-1)^k \frac{{x^{2k}}}{{(2k)!}} + i \sum_{k=0}^{\infty} (-1)^k \cdot \frac{{x^{2k+1}}}{{(2k+1)!}}

We see that we splitted the first sum into two sums. We notice too that the in the first sum we have the even numbers and in the second the odd numbers in the power of x, 2*k and 2*k+1. Further, we see that the first sub-sum (i like that word :) ) is real, and the second is complex.

Now if we look at c), we see that cos x is real part and i*sin x the complex one. Ok, made 'click' already??

Allright, i don't want to bore you with the math stuff anymore, so ill go straight to the interesting part.

cox(x)

\cos(x) = \sum_{k=0}^{\infty} \frac{(-1)^k \cdot x^{2k}}{(2k)!} = 1 - \frac{x^2}{2!} + \frac{x^4}{4!} - \frac{x^6}{6!} + \ldots

sin(x)

\sin(x) = \sum_{k=0}^{\infty} \frac{(-1)^k \cdot x^{2k+1}}{(2k+1)!} = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!} + \ldots

Ok? That's fine, i hear you say, but how does that improve my sexual life???

It doesn't improve your sexual life, but your code can be made pretty fast. Isn't that the real important thing?? :)

[C0De]

Let's try to set up a little piece of code that will do the job. I will begin with lame floating-point-using C code, and move down to fast fixed point arithmetic in pure ASM. ( Note: that will be on a separated file ).

#include <stdio.h> 
#include <math.h> // We need this to compare our results with the real sin()
// and cos() functions.
// Besides, we need the pow() function.
void main()
{
float result;
float x;

printf("\nEnter x: " ); // x is the number whose sinus you want to
// calculate.
scanf( "%f", &x ); // Enter x.

result = x - pow( x, 3 ) / 6; // Do it!

// Check our result with math.h's sin() function.

printf( "\nOur result : %f", result );
printf( "\nmath.h's result : %f", sin( x ) );
}

Try it with many values, and you will see that it is not very accurate, but it is fast. If you need more accuracy, then you must do the following:

\text{result} = x - \frac{x^3}{6} + \frac{x^5}{120} - \frac{x^7}{5040} + \ldots

Where 6 = 3!, 120 = 5!, 5040 = 7! ...


Ok? Now go ahead and try the cosine for yourself!

Well, thats all for now, next week ill finish the assembler part of this tutor, so stay tuned!

[GrReEtZ]

A tutor can't be complete without the greetings part, so here i go:

  • Mega Greetz to my demo group, QUASAR.
  • Special Greetz to Kory for making me write this in the first place :)
  • Cool greetz to: tig2, zylon, debonair, barog, RuneStar, CRShadow, iitm, X, RyanT, Scorpio, Frenzy, luvscrs, Kaneda, Killgates, jungle, and everyone else in Undernet #coders i forget, you guys rule!!!

For bugs, flames, marriage proposals or just for fun, email me at the above address.

[E0f]

← previous
next →
loading
sending ...
New to Neperos ? Sign Up for free
download Neperos App from Google Play
install Neperos as PWA

Let's discover also

Recent Articles

Recent Comments

Neperos cookies
This website uses cookies to store your preferences and improve the service. Cookies authorization will allow me and / or my partners to process personal data such as browsing behaviour.

By pressing OK you agree to the Terms of Service and acknowledge the Privacy Policy

By pressing REJECT you will be able to continue to use Neperos (like read articles or write comments) but some important cookies will not be set. This may affect certain features and functions of the platform.
OK
REJECT