The tan Function in C++: Understanding Angles and Ratios
Hello! In this article, we’ll talk about a handy C++ function for dealing with angles – tan
. We’ll start by getting to know this function and figure out how to use it the right way. We’ll check how it behaves with some examples and end by creating a small program using tan
.
What is the tan function?
To work with angles in C/C++, you can use the tan
function. Here’s what it looks like:
double tan (double x);
 It takes an angle value as input.
 It gives back a number based on that angle. This is the tangent value.
Let’s see a simple program that shows the result of the tan
function:
#include <cmath> // This is where tan lives
#include <iostream>
using namespace std;
int main() {
cout << "tan(45) = " << tan(M_PI / 4) << endl;
return 0;
}
If you run it, you might see:
tan(45) = 1
Why 1? Because the tangent of 45 degrees is 1.
Create your program using tan
Let’s get creative. Suppose you’re building a game where a character jumps on platforms. Knowing the platform’s slope (angle) can help your character decide if it’s too steep.
#include <iostream>
#include <cmath>
using namespace std;
double degreesToRadians(double degrees) {
return degrees * (M_PI / 180);
}
int main() {
double platformAngle;
cout << "Enter platform angle (in degrees): ";
cin >> platformAngle;
double slope = tan(degreesToRadians(platformAngle));
if (slope > 2) {
cout << "Too steep! Don't jump!" << endl;
} else {
cout << "Safe to jump!" << endl;
}
return 0;
}
Now, every time your game character sees a platform, you can use this simple program to check if it’s safe to jump!
Wrapping it up
The tan
function is more than just some math. It’s a bridge between our world of angles and the computer’s world of calculations. With a bit of creativity, you can use it in many interesting ways. Keep exploring and happy coding!
Exercises

Understanding
tan
: Write a C++ program that asks the user to input three different angles (in degrees).
 For each angle, calculate its tangent value using the
tan
function and display the result.  Ensure your program converts the angles from degrees to radians before applying the
tan
function.

Platform Jump Simulation:
 Extend the platform jump program provided in the article.
 Add an option for the user to input multiple platform angles at once (e.g., 30, 45, 60) and check if they’re safe to jump on.
 Display a summarized report at the end, showing the number of safe platforms and the number of risky platforms based on the input angles.

Creative Use of
tan
: Think of a realworld application where the
tan
function might be useful (other than the platform game example).  Briefly describe the scenario and write a simple C++ program that demonstrates the application of
tan
in that context.
 Think of a realworld application where the
Discussion