TANH()
Palavras-chave:
Publicado em: 05/08/2025Understanding and Implementing the Hyperbolic Tangent Function (TANH)
This article explores the hyperbolic tangent function (TANH), a crucial function in various fields, including machine learning and signal processing. We'll delve into its mathematical definition, demonstrate its implementation in C, analyze its complexity, and briefly discuss alternative approaches.
Fundamental Concepts / Prerequisites
Before diving into the implementation, a basic understanding of the following concepts is helpful:
- Exponential functions: You should be familiar with the exponential function, denoted as ex, where 'e' is Euler's number (approximately 2.71828).
- Mathematical functions: A general understanding of common mathematical operations (addition, subtraction, division) is assumed.
Implementation in C
The hyperbolic tangent function (tanh(x)) is defined as: tanh(x) = (ex - e-x) / (ex + e-x). The following C code provides an implementation of this function.
#include <math.h>
#include <stdio.h>
// Function to calculate the hyperbolic tangent (tanh) of a number
double tanh_custom(double x) {
// Calculate e^x
double exp_x = exp(x);
// Calculate e^-x
double exp_neg_x = exp(-x);
// Calculate tanh(x) using the formula: (e^x - e^-x) / (e^x + e^-x)
double tanh_x = (exp_x - exp_neg_x) / (exp_x + exp_neg_x);
return tanh_x;
}
int main() {
double input = 1.0;
double result = tanh_custom(input);
printf("tanh(%f) = %f\n", input, result); // Output: tanh(1.000000) = 0.761594
return 0;
}
Code Explanation
The tanh_custom
function calculates the hyperbolic tangent of a given input x
. It first calculates ex
and stores it in the exp_x
variable using the exp()
function from the math.h
library. Similarly, it calculates e-x
and stores it in exp_neg_x
. Finally, it applies the formula (ex - e-x) / (ex + e-x)
to compute tanh(x)
and returns the result.
The main
function demonstrates the use of the tanh_custom
function by calculating tanh(1.0)
and printing the result.
Complexity Analysis
The time complexity of the tanh_custom
function is primarily determined by the exp()
function call. The standard exp()
function typically has a time complexity of O(1), as it often relies on hardware-optimized implementations or precomputed lookup tables for common values. The remaining arithmetic operations (subtraction and division) also have a time complexity of O(1).
The space complexity is O(1) because the function uses a constant amount of extra space, regardless of the input value. Only a few variables are used to store intermediate results (exp_x
, exp_neg_x
, and tanh_x
), so the memory footprint remains constant.
Alternative Approaches
Another approach to calculate TANH is to use a lookup table for pre-computed values. This approach involves creating a table that stores the TANH values for a range of input values. This can be faster if TANH needs to be calculated very frequently and accuracy can be traded off for speed. However, it requires additional memory to store the lookup table and introduces quantization errors.
Conclusion
The hyperbolic tangent function (TANH) is an important mathematical function with applications in various domains. This article provided a clear implementation of TANH in C, along with a complexity analysis and a brief overview of an alternative approach. Understanding the properties and implementation of TANH is crucial for developers working in fields that utilize this function.