Calculating cosine and sine by hand

BlackBeltPanda

New member
Joined
May 28, 2014
Messages
3
Hello,

So I'm writing a program that plots 16 points of a circle, with center (0,0) and radius 0.5. I have no access to any higher math functions, like sin, cos, or even factorials, so I'm attempting to write my own functions.
Basically, up until about 90 degrees, sin and cos are accurate. Here are my formulas:

Factorials:
Where n is the given number
Code:
        i = n-1
        While i>0:
            n = n*i
            i = i-1
n should now be equal to n!

Sin:
Where n is the given number
Code:
        r = (n/180)*3.1415926535 (convert degrees to radians)
        sin = r - ((r^3)/3!) + ((r^5)/5!) - ((r^7)/7!)

Cos:
Where n is the given number
Code:
        r = (n/180)*3.1415926535 (convert degrees to radians)
        cos = 1 - ((r^2)/2!) + ((r^4)/4!) - ((r^6)/6!)

Am I missing something or not understanding something? Cos(270) should be 0, but my function returns -4.7655522868. =/
 
Last edited:
Where n is the given number
Code:
        i = n-1
        While i>0:
            n = n*i
            i = i-1
n should now be equal to n!
Have you checked this, to confirm that you're getting the correct value? (Maybe a different variable should be used, like "f" for "factorial".)

By the way, what do you mean by "n is the given number"? What value does "r" represent? Such as in:

Sin:
Where n is the given number
Code:
        r = (n/180)*3.1415926535 (convert degrees to radians)
        sin = r - ((r^3)/3!) + ((r^5)/5!) - ((r^7)/7!)
I'm assuming, by the way, that you're using the Maclaurin series representations of sine and cosine, and that you're using the first four terms of said series as a "good enough" approximation.
 
Cos(270°) should be 0, but my function returns -4.7655522868

That result is correct, using a 6th-degree polynomial approximation.

To get a better approximation for cos(r), you need more terms in your Taylor polynomial.

(The following decimal numbers are rounded.)

1 - r^2/2! + r^4/4! - r^6/6! + r^8/8! - r^10/10! + r^12/12! - r^14/14! = -0.002633

1 - r^2/2! + r^4/4! - r^6/6! + r^8/8! - r^10/10! + r^12/12! - r^14/14! + r^16/16! - r^18/18! = -0.00001144

1 - r^2/2! + r^4/4! - r^6/6! + r^8/8! - r^10/10! + r^12/12! - r^14/14! + r^16/16! - r^18/18! + r^20/20! - r^22/22! = -0.00000003050

:)
 
That result is correct, using a 6th-degree polynomial approximation.

To get a better approximation for cos(r), you need more terms in your Taylor polynomial.

(The following decimal numbers are rounded.)

1 - r^2/2! + r^4/4! - r^6/6! + r^8/8! - r^10/10! + r^12/12! - r^14/14! = -0.002633

1 - r^2/2! + r^4/4! - r^6/6! + r^8/8! - r^10/10! + r^12/12! - r^14/14! + r^16/16! - r^18/18! = -0.00001144

1 - r^2/2! + r^4/4! - r^6/6! + r^8/8! - r^10/10! + r^12/12! - r^14/14! + r^16/16! - r^18/18! + r^20/20! - r^22/22! = -0.00000003050

:)

Ah, thanks, the website I found explaining the Taylor series and how to use it to find the sine and cosine didn't go into much detail about accuracy and margin of error. I'll write a loop to iterate through to 21/22 for sin/cos. =)
 
I'd thought maybe "n!" was being used in your coding where "just n" had been intended. But Wolfram Alpha returns a much nicer result, once more terms are included. (I used "4.712" as a quick approximation of "(270/180)*(pi)".) This confirms that your coding is good; you just need more of it. ;)
 
I managed to work the formula into a loop and now my results are accurate to 8 decimal places when finding the sine or cosine of 360. =D
Now I can render out those circles. =)
 
Top