trig in c++
To perform trigonometric calculations in C++, you can make use of the math library. Here's an example of how you can use trigonometric functions in C++:
- Include the math library:
#include <cmath>
- Declare variables for the angle and the result:
double angle = 45.0;
double result;
- Convert the angle from degrees to radians:
double radians = angle * M_PI / 180.0;
Note: M_PI is a constant defined in the math library, representing the value of pi.
- Perform the trigonometric calculation:
result = sin(radians);
In this example, we are calculating the sine of the angle. Other trigonometric functions available in C++ include cos(), tan(), asin(), acos(), and atan().
- Print the result:
std::cout << "Sine of " << angle << " degrees is " << result << std::endl;
This will output the result to the console.
Remember to compile your C++ program with the -lm
flag to link against the math library. For example:
g++ -o program program.cpp -lm
That's it! You now have an example of how to perform trigonometric calculations in C++ using the math library.