C++ tanh()
Published Nov 21, 2022
Contribute to Docs
The Math.tanh() function returns the hyperbolic tangent of an angle given in radians.
Syntax
std::tanh(angle)
The angle is measured in radians and the return type is a double, float, or long double.
The mathematical formula used in tanh() looks like this:
tanh(x) = (ex - e-x) / (ex + e-x)
Example
The following example demonstrates the application of .tanh() function:
#include <iostream>#include <cmath>int main() {// Values in degreedouble value1 = -30.0;double value2 = 120.0;double value3 = 180.0;// Convert degree to radianvalue1 = value1 * 3.1415 / 180;value2 = value2 * 3.1415 / 180;value3 = value3 * 3.1415 / 180;double result1 = std::tanh(value1);double result2 = std::tanh(value2);double result3 = std::tanh(value3);std::cout << "The value of tanh(" << value1 << ") is: " << result1 << "\n";std::cout << "The value of tanh(" << value2 << ") is: " << result2 << "\n";std::cout << "The value of tanh(" << value3 << ") is: " << result3 << "\n";}
This produces the following output:
The value of tanh(-0.523583) is: -0.480461The value of tanh(2.09433) is: 0.97012The value of tanh(3.1415) is: 0.996271
Codebyte Example
This example uses tanh() to return the hyperbolic tangent of 1.0 radians:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
- Beginner Friendly.11 hours