# Something about a bit trigeometry and C++

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-23-2006
ThWolf
Something about a bit trigeometry and C++
Okay its suppose to be trigonometry in the title, my mistake.
To all those that know what sin and cos are - I wrote a program that gets a number from a user and shows its sin and cos. Now, I tried it, entering 90 as a number.
sin(90) equals 1, and cos (90) equals 0, but for some reason this output is been displayed:
sin(90) = 0.893997
cos(90) = -0.448074

Here is the code:
Code:

```#include <iostream> #include <cmath> using namespace std; int main() {         cout << "Enter a number";         int num;         cin >> num;         cout << "sin(" << num << ") = " << sin(num) << "\n";         cout << "cos(" << num << ") = " << cos(num);         return 0; }```
Oh, and I know it has nothing to do with the post, but those anybody know how to prevent the program from displaying that "Press any key to continue"?
• 08-23-2006
Cat
Because sin() and cos() expect the parameter in radians, not degrees. sin(90) = sin (90 radians) = sin (5156.6 degrees) = 0.89.

You really mean sin(pi/2) = 1, and cos(pi/2) = 0. Pi/2 is approx 1.5708
• 08-23-2006
ThWolf
Ohh... logical. So is there no way to tell the program to expect the number in degrees?
• 08-23-2006
Cat
Only way is to just convert to radians ;) radians are not arbitrary like degrees, so they're a better way to measure angles in, anyway.
• 08-23-2006
Sebastiani
just convert the degrees to radians with the formula (theta * pi) / 180.0.
• 08-23-2006
ThWolf
Quote:

Originally Posted by Sebastiani
just convert the degrees to radians with the formula (theta * pi) / 180.0.

Then I would prefer that pi would be accurate, not just 3.14. Does C++ know what pi is, so that it can be a bit more accurate than 3.14? Or do I have to define and declare pi as 3.14?
• 08-23-2006
SlyMaelstrom
Some [itex] implementations have a PI constant (usually M_PI from my experience) but even if it doesn't it's not hard to get a more accrurate value on the internet and define it yourself.
Code:

`#define PI  3.1415926535`
• 08-23-2006
ThWolf
Quote:

Originally Posted by SlyMaelstrom
Some [itex] implementations have a PI constant (usually M_PI from my experience) but even if it doesn't it's not hard to get a more accrurate value on the internet and define it yourself.
Code:

`#define PI  3.1415926535`

It would be great if I could use those constants... There is a problem with me, in math I have to be as accurate as possible, same when I used to learn phyiscs.
• 08-23-2006
SlyMaelstrom
I don't think the constant is more accurate than 12 or so digits, so if you want as accurate as possible. Find out the precision of a double on your system and define a constant to that many places. Or calculate it yourself using that many digits as your number of iterations.
• 08-23-2006
ThWolf
Okay something strange just happend. I've used that formula of course, and now if I enter 90 as a number, sin(90) is 1, as expected, but cos(90) is 4.48966e-011.
Now, I have already learned about E notation, but can't the number just be 0?
• 08-23-2006
SlyMaelstrom
Code:

```#include <iostream> #include <iomanip> using namespace std; cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint); cout << setprecision(15) << cos(90 * PI / 180);```
• 08-23-2006
ThWolf
Thank you very very very much. Just know that the last line doesn't work, as the compiler says that "setprecision' : undeclared identifier", but that doesn't matter, and now its much more accurate.
Just last question please, is there a way to tell the compiler to show only three significant digits after the period?
• 08-23-2006
SlyMaelstrom
Yes, it's called setprecision(). Why it's undeclared for you, I don't know. It's standard. Do you have "using namespace std" at the top of your program? If that doesn't work, try adding "cout.precision(3);" just after the setf() lines.
• 08-23-2006
ThWolf
Its been undeclared becuase you forgot to include the <iomanip> library (I didn't know about this library before), but now that you edited your post it worked just fine.
Anyway, I'm sorry for all my questions, but I have to know one more thing:
Is there a function in C++ for arctg, arcsin, etc...? I searched about it in MSDN and found nothing.
• 08-23-2006
SlyMaelstrom
http://www.cplusplus.com/ref/cmath/

Look for asin, acos, atan.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last