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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.83167636,"math_prob":0.9892791,"size":1186,"snap":"2022-05-2022-21","text_gpt3_token_len":320,"char_repetition_ratio":0.08967851,"word_repetition_ratio":0.1632653,"special_character_ratio":0.2698145,"punctuation_ratio":0.16460906,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.997427,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-01-21T17:04:44Z\",\"WARC-Record-ID\":\"<urn:uuid:f10c0f7f-b004-4d43-9e52-f2bcd801aad8>\",\"Content-Length\":\"103361\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:90293dda-fed8-4097-935e-ac1bfcb6f7fe>\",\"WARC-Concurrent-To\":\"<urn:uuid:b64480ad-908d-4900-878d-55a794c0e6fc>\",\"WARC-IP-Address\":\"172.66.42.251\",\"WARC-Target-URI\":\"https://www.daniweb.com/programming/software-development/threads/253479/sin-cos-tan\",\"WARC-Payload-Digest\":\"sha1:LIOU6DWPLAEV7MKS275LHRD6IFVVOMSQ\",\"WARC-Block-Digest\":\"sha1:D3G62KH5Z5QUN5HNAXW5PM22UVWP6IWA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-05/CC-MAIN-2022-05_segments_1642320303512.46_warc_CC-MAIN-20220121162107-20220121192107-00128.warc.gz\"}"}