Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2005-09-03 11:48:11

mikau
Member
Registered: 2005-08-22
Posts: 1,504

the formulas for sine, cosine and tangent

I would like to right some simple C++ programs to do some vector calculations and stuff but I need a way to calculate sine, cosine and tangent. I take it most computers have the formula's built in somewhere but I think it might be educational (mathematicly) and good programming practice to write my own algorithm. I've seen a few differant formulas for more or less accurate calculations on the internet but the explanations are often quite confusing. Using words to express yourself is not what mathematicians do best.

Basicly I wan't to define the following functions: float sin( float x); float cos(float x); float tan(float x);

So I can just call sin(x) cos (x) or tan(x) anytime I need them.


A logarithm is just a misspelled algorithm.

Offline

#2 2005-09-04 18:14:13

ryos
Member
Registered: 2005-08-04
Posts: 394

Re: the formulas for sine, cosine and tangent

This may help.

If it doesn't, I'll have a go at translating it for you.

Of course, if your program is such that you know the lengths of at least two of the sides of your triangle, you can compute the trig functions using SOH CAH TOA.


El que pega primero pega dos veces.

Offline

#3 2005-09-05 00:03:06

ahgua
Member
Registered: 2005-08-24
Posts: 25

Re: the formulas for sine, cosine and tangent

It is better to remember "TOA CAH SOH". It means "fat old woman" in hokkien (If I remember correctly ).


Life is a passing dream, but the death that follows is eternal...

Offline

#4 2005-09-05 05:30:08

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: the formulas for sine, cosine and tangent

Hmm...what does "^" mean? Is that something used in calculus or something? Never enountered it in my math book.


A logarithm is just a misspelled algorithm.

Offline

#5 2005-09-05 07:35:47

ryos
Member
Registered: 2005-08-04
Posts: 394

Re: the formulas for sine, cosine and tangent

"^" means to raise to a power. It's necessary on plain text documents that don't support superscripts. It's also an operator in many programming languages, including, I think, C/++, so you can use it in your programs to raise things to a power.


El que pega primero pega dos veces.

Offline

#6 2005-09-05 09:52:15

John E. Franklin
Member
Registered: 2005-08-29
Posts: 3,588

Re: the formulas for sine, cosine and tangent

I wrote this in Just BASIC v1.0.
It computes the cosine of angles 0, 5, 10, 15, 20, 25, 30, ... to 90 degrees.
The computation of cosine of 90 degrees comes out about 10^-15, not exactly zero though.

'Note: dangle is angle in degrees and rangle is angle in radians
for dangle = 0 to 90 step 5
pi = 3.141592653589793238462643383279502884197169399375105820974944
rangle=(pi*dangle)/180
'Note:  cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + ...
rangleSqrd=rangle * rangle
diver = 0
nextTerm = 1
cosineNow = 1

for iter=1 to 10
for d1 = 1 to 2
  diver = diver + 1
  nextTerm = nextTerm / diver
next d1
nextTerm = 0 - nextTerm
nextTerm = nextTerm * rangleSqrd
cosineNow = cosineNow + nextTerm
next iter
print "Cosine( ";dangle;" ) = ";cosineNow
next dangle

igloo myrtilles fourmis

Offline

#7 2005-09-05 10:39:47

MathsIsFun
Administrator
Registered: 2005-01-21
Posts: 7,711

Re: the formulas for sine, cosine and tangent

Code looks right to me. Does it work in all quadrants (90° to 360°) ?


"The physicists defer only to mathematicians, and the mathematicians defer only to God ..."  - Leon M. Lederman

Offline

#8 2005-09-05 11:07:21

John E. Franklin
Member
Registered: 2005-08-29
Posts: 3,588

Re: the formulas for sine, cosine and tangent

It works in all four quadrants, but the floating point math isn't that precise in this language I used
so when you try angles like 3600 degrees (ten times around a circle), you get really inaccurate answers.
You could with perhaps 20 times more programming, teach the program how to multiply, divide, add, and
subtract out to a large number of decimal places stored in an array.  I might try that some day...  smile

Last edited by John E. Franklin (2005-09-05 11:08:39)


igloo myrtilles fourmis

Offline

#9 2005-09-06 00:37:08

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: the formulas for sine, cosine and tangent

Might be a good idea to first automaticly reduce the arguement. I don't know this languagel but, as the first step of the function, in C++ you could just say

float sine(float x)
{
  while ( x >= 360)
{
  x = (x - 360);
}

/* insert formula for sine here*/

}


since adding 360 degree's to an angle has no effect on its sine, cosine or tangent, the value returned will be correct, and more accurate.


A logarithm is just a misspelled algorithm.

Offline

#10 2005-09-06 09:17:57

John E. Franklin
Member
Registered: 2005-08-29
Posts: 3,588

Re: the formulas for sine, cosine and tangent

The following web page says you can use any value of x in the cosine summation and the sine too.
Your recommendation is a good one to get the angle within the four quadrants.  You might even
be more accurate if you stay with the first quadrant and alter the signs as needed.

http://euclideanspace.com/maths/algebra … /index.htm

Last edited by John E. Franklin (2005-09-07 02:53:55)


igloo myrtilles fourmis

Offline

#11 2005-09-06 15:15:53

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: the formulas for sine, cosine and tangent

Well I wrote a perfectly working function for sine but my compiler is screwing up. :-( Its a pretty cool function though.


A logarithm is just a misspelled algorithm.

Offline

#12 2005-09-07 00:34:42

MathsIsFun
Administrator
Registered: 2005-01-21
Posts: 7,711

Re: the formulas for sine, cosine and tangent

John E. Franklin wrote:

You could with perhaps 20 times more programming, teach the program how to multiply, divide, add, and
subtract out to a large number of decimal places stored in an array.  I might try that some day...  smile

Or ...

... you might like to improve *this* !


"The physicists defer only to mathematicians, and the mathematicians defer only to God ..."  - Leon M. Lederman

Offline

#13 2005-09-07 02:49:38

John E. Franklin
Member
Registered: 2005-08-29
Posts: 3,588

Re: the formulas for sine, cosine and tangent

Those are two very nice calculators!  I'm impressed.  I started writing my large digit calculation program yesterday, so I'll continue with that.  Plus, I'm not very good at examining programs written by others, and I like writing things from scratch.


igloo myrtilles fourmis

Offline

#14 2005-09-07 02:49:55

Polly
Member
Registered: 2005-06-24
Posts: 221

Re: the formulas for sine, cosine and tangent

hi peeps not bin on 4 ages and stsrted bak at school today

Offline

Board footer

Powered by FluxBB