YouTip LogoYouTip

Func Number Sin

## Python sin() Function The `math.sin()` function in Python returns the sine of a given angle. The angle must be specified in radians. This function is part of Python's built-in `math` module, which provides access to the mathematical functions defined by the C standard. --- ## Syntax To use the `sin()` function, you must first import the `math` module. It cannot be called directly. ```python import math math.sin(x) ``` ### Parameters * **`x`**: A numeric value (integer or float) representing the angle in **radians**. ### Return Value * Returns a float value representing the sine of `x`. The returned value is always in the range **`[-1.0, 1.0]`**. --- ## Code Examples The following example demonstrates how to use the `math.sin()` function with different values, including positive numbers, negative numbers, zero, and constants like $\pi$ (`math.pi`). ```python #!/usr/bin/python3 import math # Calculate sine for different radian values print("sin(3) :", math.sin(3)) print("sin(-3) :", math.sin(-3)) print("sin(0) :", math.sin(0)) print("sin(math.pi) :", math.sin(math.pi)) print("sin(math.pi/2) :", math.sin(math.pi / 2)) ``` ### Output ```text sin(3) : 0.1411200080598672 sin(-3) : -0.1411200080598672 sin(0) : 0.0 sin(math.pi) : 1.2246467991473532e-16 sin(math.pi/2) : 1.0 ``` --- ## Important Considerations ### 1. Radians vs. Degrees The most common mistake when using `math.sin()` is passing the angle in degrees instead of radians. * **To convert degrees to radians**, use `math.radians(degrees)` or multiply by $\frac{\pi}{180}$. * **To convert radians to degrees**, use `math.degrees(radians)` or multiply by $\frac{180}{\pi}$. #### Example: Calculating $\sin(30^\circ)$ ```python import math degrees = 30 # Convert degrees to radians first radians = math.radians(degrees) # Calculate sine result = math.sin(radians) print(f"sin({degrees}Β°) = {result}") # Output: sin(30Β°) = 0.49999999999999994 (approximately 0.5) ``` ### 2. Floating-Point Precision As seen in the output of `math.sin(math.pi)`, the result is `1.2246467991473532e-16` instead of an exact `0.0`. This is due to the limitations of floating-point representation in computer hardware. If you need to check if a result is close to zero, use `math.isclose()`: ```python import math result = math.sin(math.pi) print(math.isclose(result, 0.0, abs_tol=1e-9)) # Output: True ```
← Func Number TanFunc Number Hypot β†’