From c5bd9b8d68cefe97c2194cea8dd6aa82ba654512 Mon Sep 17 00:00:00 2001 From: Tyson Cung Date: Sat, 11 Apr 2026 23:57:40 +0000 Subject: [PATCH] fix(trigonometry): add arcsin() function (#110) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implement arcsin(x) using the identity arcsin(x) = arctan(x / sqrt(1-x²)). - Accepts float or int in the range [-1, 1] - Raises ValueError for inputs outside the valid domain - Handles edge cases x = ±1 directly (returns ±π/2) - Returns result in radians, consistent with arctan() --- simple_equ/geometry/trigonometry.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/simple_equ/geometry/trigonometry.py b/simple_equ/geometry/trigonometry.py index fd5a569..c736d4a 100644 --- a/simple_equ/geometry/trigonometry.py +++ b/simple_equ/geometry/trigonometry.py @@ -107,3 +107,25 @@ def arctan(x: float | int, iter=20): # which corresponds to x in [-1, 1]. theta = theta - (_tan_rad(theta) - x) / (1 + _tan_rad(theta)**2) return theta + + +def arcsin(x: float | int) -> float: + """[Summary]: Return the arcsine of x in radians. + + [Description]: Computes the inverse sine of x using the identity + arcsin(x) = arctan(x / sqrt(1 - x^2)). The input must be in the range + [-1, 1]; values outside this range raise a ValueError. + + [Usage]: Typical usage example: + + result = arcsin(0.5) # returns approximately π/6 (0.5236...) + print(result) + """ + if x < -1 or x > 1: + raise ValueError("arcsin(x) is only defined for x in [-1, 1]") + if x == 1: + return constants.pi / 2 + if x == -1: + return -constants.pi / 2 + # arcsin(x) = arctan(x / sqrt(1 - x^2)) + return arctan(x / algebra.sqrt(1 - x * x))