C++ std::tan
In C++, std::tan takes an angle in radians and returns its tangent. Where it is defined, tangent is sine divided by cosine.
A simple example
std::tan
- Start with 0 radians
- Do this Find rise divided by run
- Result 0
#include <iostream>
#include <cmath>
int main() {
std::cout << (std::tan(0.0)) << '\n';
}
0
Syntax
std::tan(angle)
Run these examples in C++
Save one example at a time as lesson.cpp. Use C++17 and a Unix-style shell.
c++ -std=c++17 -Wall -Wextra lesson.cpp -o lesson
./lessonCalculate rise per metre of horizontal run
A surveyor has the ramp angle and wants to know the rise for each horizontal metre. Express that relationship as a ratio the drawing team can apply to different ramp lengths.
#include <cmath>
#include <cstdio>
int main() {
const double angle = 0.7853981633974483;
std::printf("%.3f\n", std::tan(angle));
return 0;
}
1.000
At 45 degrees the ramp rises one metre for every horizontal metre.
Check that a platform is level
A surveyor describes a platform or ramp using its rise and horizontal run. The drawing tool must convert between slope and angle, including the level case where the rise is zero.
#include <cmath>
#include <cstdio>
int main() {
const double angle = 0.0;
std::printf("%.3f\n", std::tan(angle));
return 0;
}
0.000
A level platform has zero rise per unit of horizontal run.
Draw a sloping exhibit roof
A model roof rises at 30° across a 3 m horizontal run. A student needs the vertical rise to place the far edge in the drawing.
Multiply horizontal run by tangent to get rise.
What to watch for
Tangent becomes very large near a right angle. Use measured rise and run for a near-vertical slope.