YouTip LogoYouTip

Ref Math Sin

## Python math.sin() Method The `math.sin()` method is part of Python's built-in `math` module. It returns the sine of a given angle expressed in **radians**. If you have an angle in degrees, you must first convert it to radians using the `math.radians()` method before passing it to `math.sin()`. ### Quick Info * **Python Version:** Introduced in Python 1.4 * **Module:** `math` --- ## Syntax ```python import math math.sin(x) ``` ### Parameters | Parameter | Type | Description | | :--- | :--- | :--- | | **x** | `float` or `int` | **Required.** A numeric value representing the angle in radians. | ### Return Value * **Type:** `float` * **Description:** Returns a floating-point value between `-1.0` and `1.0` (inclusive), representing the sine of the angle `x`. * **Exceptions:** Raises a `TypeError` if the input `x` is not a number. --- ## Code Examples ### Example 1: Basic Usage with Radians The following example demonstrates how to find the sine value of different numeric inputs (in radians): ```python import math # Calculate sine values for different inputs in radians print(math.sin(0.00)) # Sine of 0 print(math.sin(-1.23)) # Sine of a negative float print(math.sin(10)) # Sine of an integer print(math.sin(math.pi)) # Sine of Pi (approx. 180 degrees) print(math.sin(math.pi / 2)) # Sine of Pi/2 (approx. 90 degrees) ``` **Output:** ```text 0.0 -0.9424888019316975 -0.5440211108893699 1.2246467991473532e-16 1.0 ``` --- ### Example 2: Working with Degrees To calculate the sine of an angle specified in degrees, you must convert the degrees to radians first using `math.radians()`. ```python import math # Convert 30 degrees to radians, then calculate the sine sin_30 = math.sin(math.radians(30)) print("Sine of 30 degrees:", sin_30) # Convert 90 degrees to radians, then calculate the sine sin_90 = math.sin(math.radians(90)) print("Sine of 90 degrees:", sin_90) ``` **Output:** ```text Sine of 30 degrees: 0.49999999999999994 Sine of 90 degrees: 1.0 ``` --- ## Developer Considerations ### Floating-Point Precision You might notice that `math.sin(math.pi)` returns `1.2246467991473532e-16` instead of an exact `0.0`, and `math.sin(math.radians(30))` returns `0.49999999999999994` instead of exactly `0.5`. This behavior is due to the limitations of double-precision floating-point representation in computers (IEEE 754 standard). When writing conditional logic based on trigonometric outputs, it is best practice to use `math.isclose()` rather than direct equality operators (`==`). ```python import math # Recommended way to compare floating-point results result = math.sin(math.radians(30)) print(math.isclose(result, 0.5)) # Returns True ```
← Ref Math SinhRef Math Atanh β†’