Thread: Something about a bit trigeometry and C++

  1. #1
    Registered User
    Join Date
    Aug 2006
    Location
    Israel
    Posts
    33

    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"?
    Last edited by ThWolf; 08-23-2006 at 01:01 AM.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    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
    Last edited by Cat; 08-23-2006 at 01:03 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Location
    Israel
    Posts
    33
    Ohh... logical. So is there no way to tell the program to expect the number in degrees?

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    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.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    just convert the degrees to radians with the formula (theta * pi) / 180.0.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Aug 2006
    Location
    Israel
    Posts
    33
    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?

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Some <math> 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
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Aug 2006
    Location
    Israel
    Posts
    33
    Quote Originally Posted by SlyMaelstrom
    Some <math> 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.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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.
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    Aug 2006
    Location
    Israel
    Posts
    33
    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?

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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);
    Sent from my iPadŽ

  12. #12
    Registered User
    Join Date
    Aug 2006
    Location
    Israel
    Posts
    33
    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?

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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.
    Last edited by SlyMaelstrom; 08-23-2006 at 01:57 AM.
    Sent from my iPadŽ

  14. #14
    Registered User
    Join Date
    Aug 2006
    Location
    Israel
    Posts
    33
    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.
    Last edited by ThWolf; 08-23-2006 at 02:07 AM.

  15. #15
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    http://www.cplusplus.com/ref/cmath/

    Look for asin, acos, atan.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed