YouTip LogoYouTip

Ref Math Atanh

## Python math.atanh() Method The `math.atanh()` method is a built-in function in Python's standard `math` module. It returns the inverse hyperbolic tangent of a given number $x$. Mathematically, the inverse hyperbolic tangent is defined as: $$\operatorname{atanh}(x) = \frac{1}{2} \ln\left(\frac{1 + x}{1 - x}\right)$$ This method is widely used in scientific computing, physics, and advanced mathematical modeling. --- ### Syntax To use the `math.atanh()` method, you must first import the `math` module: ```python import math math.atanh(x) ``` ### Parameters | Parameter | Type | Description | | :--- | :--- | :--- | | `x` | `float` or `int` | **Required.** A numeric value representing the value whose inverse hyperbolic tangent you want to calculate. The value of `x` must be strictly between **-1.0 and 1.0** (excluding -1.0 and 1.0). | ### Return Value * **Type:** `float` * **Description:** Returns the inverse hyperbolic tangent of the specified number `x`. --- ### Code Examples #### Example 1: Basic Usage The following example demonstrates how to calculate the inverse hyperbolic tangent for positive and negative values within the valid range. ```python # Import the math module import math # Calculate and print the inverse hyperbolic tangent print(math.atanh(0.59)) print(math.atanh(-0.12)) ``` **Output:** ```text 0.6776660677579618 -0.12058102840844402 ``` #### Example 2: Boundary Values As $x$ approaches $1$ or $-1$, the result approaches positive or negative infinity. ```python import math # Values close to the boundaries print(math.atanh(0.9999)) print(math.atanh(-0.9999)) ``` **Output:** ```text 4.951710188619111 -4.951710188619111 ``` --- ### Exceptions and Considerations When working with `math.atanh()`, you must ensure that the input values fall strictly within the mathematical domain of the function: $-1 < x < 1$. #### 1. ValueError (Out of Domain) If you pass a value $x \ge 1$ or $x \le -1$, Python will raise a `ValueError: math domain error`. ```python import math try: # This will raise a ValueError because 1.0 is out of the valid domain print(math.atanh(1.0)) except ValueError as e: print(f"Error: {e}") ``` **Output:** ```text Error: math domain error ``` #### 2. TypeError (Non-numeric Input) If you pass a non-numeric value (such as a string or a list), Python will raise a `TypeError`. ```python import math try: # This will raise a TypeError print(math.atanh("0.5")) except TypeError as e: print(f"Error: {e}") ``` **Output:** ```text Error: must be real number, not str ``` ### Version History * **Introduced in:** Python 2.6
← Ref Math SinRef Math Atan β†’