YouTip LogoYouTip

Ref Math Acos

## Python math.acos() Method The `math.acos()` method is a built-in function in Python's standard `math` module. It returns the arc cosine (also known as the inverse cosine) of a given number. The returned angle is expressed in radians and falls within the range $[0, \pi]$. --- ### Syntax To use the `math.acos()` method, you must first import the `math` module: ```python import math math.acos(x) ``` ### Parameters | Parameter | Type | Description | | :--- | :--- | :--- | | `x` | `float` or `int` | **Required.** A numeric value between `-1` and `1` (inclusive). | ### Return Value * **Type:** `float` * **Description:** Returns the arc cosine of `x` in radians. * **Range:** The returned value is always between `0.0` and `3.141592653589793` (which is $\pi$). --- ### Code Examples The following example demonstrates how to use `math.acos()` with various positive, negative, and boundary values. ```python import math # Calculate the arc cosine of positive and negative decimals print("acos(0.55): ", math.acos(0.55)) print("acos(-0.55):", math.acos(-0.55)) # Calculate the arc cosine of 0 (corresponds to pi/2) print("acos(0): ", math.acos(0)) # Calculate the arc cosine of boundary values (-1 and 1) print("acos(1): ", math.acos(1)) # Corresponds to 0.0 print("acos(-1): ", math.acos(-1)) # Corresponds to pi ``` **Output:** ```text acos(0.55): 0.9884320889261531 acos(-0.55): 2.15316056466364 acos(0): 1.5707963267948966 acos(1): 0.0 acos(-1): 3.141592653589793 ``` --- ### Considerations & Exceptions When working with `math.acos()`, keep the following rules in mind to avoid runtime errors: #### 1. Value Error (Out of Range) The input parameter `x` must strictly be in the range $[-1, 1]$. If `x` is outside this range, Python will raise a `ValueError`. ```python import math # This will raise a ValueError: math domain error try: math.acos(1.5) except ValueError as e: print("Error:", e) ``` **Output:** ```text Error: math domain error ``` #### 2. Type Error The input parameter `x` must be a number. If you pass a string, list, or any other non-numeric type, Python will raise a `TypeError`. ```python import math # This will raise a TypeError try: math.acos("0.5") except TypeError as e: print("Error:", e) ``` **Output:** ```text Error: must be real number, not str ``` #### 3. Converting Radians to Degrees Because `math.acos()` returns the result in **radians**, you might want to convert it to **degrees** for certain applications. You can easily achieve this using `math.degrees()`: ```python import math radians_val = math.acos(0.5) degrees_val = math.degrees(radians_val) print(f"Radians: {radians_val}") print(f"Degrees: {degrees_val}Β°") ``` **Output:** ```text Radians: 1.0471975511965979 Degrees: 59.99999999999999Β° ```
← Ref Math AsinRef Math Pi β†’