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

You are not logged in.

- Topics: Active | Unanswered

Pages: **1**

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

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

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

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

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

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

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

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

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

"^" 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

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

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

**MathsIsFun****Administrator**- Registered: 2005-01-21
- Posts: 7,552

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

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

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...

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

**igloo** **myrtilles** **fourmis**

Offline

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

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

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

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

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

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

**MathsIsFun****Administrator**- Registered: 2005-01-21
- Posts: 7,552

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...

Or ...

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

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

Offline

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

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

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

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

Offline

Pages: **1**