C++ std::sin
In C++, std::sin takes an angle in radians and returns its sine. In a right triangle, sine is the opposite side divided by the hypotenuse.
A simple example
std::sin
- Start with 0 radians
- Do this Find the vertical component
- Result 0
#include <iostream>
#include <cmath>
int main() {
std::cout << (std::sin(0.0)) << '\n';
}
0
Syntax
std::sin(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
./lessonFind the vertical share of a ladder
A maintenance team is sketching a ladder or lifting arm against a wall. Use the known length and angle or height to recover the missing dimension and check the units before entering it in the drawing.
#include <cmath>
#include <cstdio>
int main() {
const double angle = 0.5235987755982988;
std::printf("%.3f\n", std::sin(angle));
return 0;
}
0.500
At 30 degrees, the vertical rise is half the length of the ladder. The input is 30 degrees expressed in radians.
Check a vertical lifting arm
A hoist diagram shows an arm pointing straight up. Evaluate that limiting position to check how much of its length contributes to the vertical component.
#include <cmath>
#include <cstdio>
int main() {
const double angle = 1.5707963267948966;
std::printf("%.3f\n", std::sin(angle));
return 0;
}
1.000
An arm at a right angle to the floor directs its whole length vertically.
Measure a scenery ladder
A theatre student builds a scale drawing of a ladder for a stage set. Its length is 4 m and its angle to the floor is 30°. How high is its tip in the drawing’s full-size dimensions?
Multiply the length by the sine of the angle measured from the floor.
Code and output in C++
#include <cmath>
#include <iostream>
#include <iomanip>
int main() {
double base = 0.0, length = 4.0;
double angle = 30.0 * std::acos(-1.0) / 180.0;
std::cout << std::fixed << std::setprecision(2)
<< base + length * std::sin(angle) << " m\n";
return 0;
}
2.00 m
Locate a Ferris wheel cabin
A wheel in an animation has an 8 m radius and its hub is 10 m above the ground. When the cabin is 30° above the horizontal on its way up, the animator needs its height above ground.
The cabin is 4 m above the hub. Add that rise to the hub height.
Code and output in C++
#include <cmath>
#include <iostream>
#include <iomanip>
int main() {
double base = 10.0, length = 8.0;
double angle = 30.0 * std::acos(-1.0) / 180.0;
std::cout << std::fixed << std::setprecision(2)
<< base + length * std::sin(angle) << " m\n";
return 0;
}
14.00 m
What to watch for
Convert a degree measurement to radians before calling a trigonometric function.