Hey guys,
so I want to write a program that calculates the sinus, cosinus and tangent without using math library...
Any ideas?

Hey guys,
so I want to write a program that calculates the sinus, cosinus and tangent without using math library...
Any ideas?

SOHCAHTOA

What about a taylor series approximation... The theory is rooted in calculus, but you can probably manage by simply applying the formulae and ignoring the theory (FYI series and taylor approximations are typically covered in a Calc 2 course).

Here are the approximations (scroll down to the "series definition" …

Sin(x) = x - x^3/3! + x^5/5! + x^7/7! ...
Cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! ...
Cos(x) = Sin( x - 90 ) ; //assuming x is in degrees
Tan(x) = Sin(X)/cos(X)
"!" means factorial.

for example to calculate Sinus you can do this :

All 10 Replies

Hey guys,
so I want to write a program that calculates the sinus, cosinus and tangent without using math library...
Any ideas?

SOHCAHTOA

What are some of the ideas that you have had? How would you try to do it if you had to?

EDIT: Shawn nice idea in principle but unless OP is doing right triangles...

@OP: How could you get the tangent knowing the other two?

What are some of the ideas that you have had? How would you try to do it if you had to?

for sinus I have this:

#include<iostream>
#include <stdio.h>
#define sin 0.017452406437283512819418978516316
using namespace std;

int main()
{
int valor;
long double seno;
cout<<"Calculate sinus of: "<<endl;
cin>>value;

seno=value*sin;

cout<<"Sinus of "<<value<<" is: "<<seno<<endl;

system("PAUSE");
return 0;
}

but I want to know if anyone has another way to write this program... and I don't know how to do cosinus and tangent

Where did you get that number from?

btw, it doesn't really matter per se but the functions are called sine and cosine, a sinus is for example the empty space behind your nose and upper cheeks.

Have you done some calculus? Have you heard of Taylor Seres?

Where did you get that number from?

btw, it doesn't really matter per se but the functions are called sine and cosine, a sinus is for example the empty space behind your nose and upper cheeks.

Have you done some calculus? Have you heard of Taylor Seres?

The number is the value of sin(1) so he's essentially getting sin(x) by doing sin(1)*x which is quite wrong :P

Gotcha, yeah that's um creative. And thanks for keeping SOHCAHTOA alive!

What about a taylor series approximation... The theory is rooted in calculus, but you can probably manage by simply applying the formulae and ignoring the theory (FYI series and taylor approximations are typically covered in a Calc 2 course).

Here are the approximations (scroll down to the "series definition" section): http://en.wikipedia.org/wiki/Trigonometric_functions

Edit: You need to know radians as well!

Sin(x) = x - x^3/3! + x^5/5! + x^7/7! ...
Cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! ...
Cos(x) = Sin( x - 90 ) ; //assuming x is in degrees
Tan(x) = Sin(X)/cos(X)
"!" means factorial.

for example to calculate Sinus you can do this :

//Sin(x) = x - x^3/3! + x^5/5! + x^7/7! ...
float Sinus( float val ){
float result = val - std::pow(val, 3.0) / factorial(3);
result  += std::pow(val,5.0) / factorial(5);
result  += std::pow(val,7.0) / factorial(7);
result += std::pow(val,9.0) / factorial(7);
return result;
}

Sin(x) = x - x^3/3! + x^5/5! + x^7/7! ...

Just wondering: are you intentionally trying to mislead OP?

Just wondering: are you intentionally trying to mislead OP?

Oops the sign must alternate,

Sin(x) = x - x^3/3! + x^5/5! - x^7/7! + x^9/9! ...

//Sin(x) = x - x^3/3! + x^5/5! - x^7/7! + x^9/9! ...
float Sinus( float val ){
float result = val - std::pow(val, 3.0) / factorial(3);
result  += std::pow(val,5.0) / factorial(5);
result  -= std::pow(val,7.0) / factorial(7);
result += std::pow(val,9.0) / factorial(9);
return result;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.