# Thread: tan() and sin()

1. ## tan() and sin()

I'm still develloping my calculator
But, there is another problem:

sin(number1);
tan(number1);

do not give the same output as my other calculator (The one which I take to school)!

No error, and I included math.h.

What am I doing wrong?

2. Feeding degrees where radians are expected?

Show an example of expected input and output.

3. math.h functions take parameters measured in radians, not degrees.
There are 2*PI radians in a full circle, scale your angles appropriately.

4. sin(1); in computer: 0.841471
sin(1); in school calculator: 0.017452406

5. Originally Posted by Ideswa
sin(1); in computer: 0.841471
sin(1); in school calculator: 0.017452406
Like the others have said, you're mixing radians and degrees. Both of those are correct.
sin(1 degree) = .0174524
sin(1 radian) = .8414709

Radians and degrees are sort of like feet and meters. Just different way of measuring, for the most part. 2 * pi radians = 360 deg, as Salem said, so: pi radians = 180 degrees, pi / 2 radians = 90 deg, etc. Google it. See MathWorld's article on it, or perhaps the Wikipedia entry.

6. Thanks!
this is my solution:

tangens = tan(number1);
answer = (2 * M_PI) * tangens;
aswer /= 360;

http://www.functionx.com/bcb/math/PI.htm says:
"....Borland had included its value in the math.h library as M_PI 3.14159265358979323846."

But when I

cout << M_PI;

the output is:
3.14159

And because of that there is still a little difference between the answers:
0.017455064 (in school calc.)
0.0174533 (in my calc.)

Can I change that configuration or is that impossible with a Borland compiler?

7. cout will use formatting rules for output which concatenate the number.

The Windows calculator will give you .017452406

Which just goes to show that you shouldn't trust default math libraries for extreme precision.

8. I'm pretty sure your school calculator's answer of 0.017455064 is off the mark, because I have two calculators that give 1.7452406437, which agrees with the Windows calc. Your calculator's "answer" of 0.0174533 is an approximation of pi/180.

And it looks like that's what you're doing, too.
Code:
```answer = (2 * M_PI) * tangens;
aswer /= 360;
What is this supposed to do? This is equivalent to
Code:
`answer = (2 * M_PI) * tangens / 360 / tangens`
which means that you're merely finding a convoluted way of assigning pi/180 to answer.

9. Originally Posted by Ideswa
tangens = tan(number1);
answer = (2 * M_PI) * tangens;
aswer /= 360;
This is completely wrong (as has been said).

The reason it almost works is that
tan(1 degree) = tan( pi/180 radians) ~= pi/180.

For small angles (in radians), tan(x)~=sin(x)~=x. This approximation is often used in mechanics.

EDIT: Approximations of tan(1 degree):
Windows calc: 0.017455064928217585765128895219728
MATLAB: 0.01745506492822

10. Can I change that configuration or is that impossible with a Borland compiler?
You can #undef PI * and then redefine PI:
Code:
```#undef PI
#define PI 3.1415926535897932384626433832795028841971693993751\
05820974944592307816406286208998628034825342117067\
98214808651328230664709384460955058223172535940812\
84811174502841027019385211055596446229489549303819\
64428810975665933446128475648233786783165271201909\
14564856692346034861045432664821339360726024914127\
37245870066063155881748815209209628292540917153643\
67892590360011330530548820466521384146951941511609\
43305727036575959195309218611738193261179310511854\
80744623799627495673518857527248912279381830119491\```
* You may not have to #undef PI, but I would put it in anyway.

11. dwks, but the double can hold only so much.
so even if you gave one million digits the resultant number would be rounded?

12. Originally Posted by dwks
You can #undef PI * and then redefine PI:
Code:
```#undef PI
#define PI 3.1415926535897932384626433832795028841971693993751\
05820974944592307816406286208998628034825342117067\
98214808651328230664709384460955058223172535940812\
84811174502841027019385211055596446229489549303819\
64428810975665933446128475648233786783165271201909\
14564856692346034861045432664821339360726024914127\
37245870066063155881748815209209628292540917153643\
67892590360011330530548820466521384146951941511609\
43305727036575959195309218611738193261179310511854\
80744623799627495673518857527248912279381830119491\```
* You may not have to #undef PI, but I would put it in anyway.
That's a complete waste of time. M_PI is already defined with as many digits as can be stored in a double, so you're longer digit sequence is not going to add anything at all. Besides, theres probably more inaccuracy in the sin function, than in your digit sequence above.

Just use this:
inline double sin_degrees(double x)
{
return sin(x * (M_PI / 180.0));
}

13. Technically, M_PI is non-portable. While it's true several compilers (or their libraries) support it, there is no requirement in the C++ standard that it be supported.

Popular pages Recent additions