import math
# Define constants
PI = 3.141592653589793
# Custom mathematical functions with limited iterations
def sin(degrees):
return math.sin(math.radians(degrees))
def cos(degrees):
return math.cos(math.radians(degrees))
def tan(degrees):
return math.tan(math.radians(degrees))
def asin(x):
return math.degrees(math.asin(x))
def acos(x):
return math.degrees(math.acos(x))
def atan(x):
return math.degrees(math.atan(x))
def mod(a, b):
return a % b # Use Python's built-in modulo operator
no imports, do it manually efficiently
Assistant
Sure! Below is the code that defines the trigonometric functions sin, cos, tan, asin, acos, atan, and the modulus function mod without using any imports. The implementations use efficient numerical approximations suitable for most practical purposes.
python
# Define constants
PI = 3.141592653589793
# Custom mathematical functions without imports
def sin(degrees):
"""Calculate the sine of an angle in degrees."""
# Normalize the angle to the range [-360, 360]
degrees = degrees % 360
if degrees > 180:
degrees -= 360
# Convert degrees to radians
radians = degrees * (PI / 180)
# Use a polynomial approximation for sine
return sine_approximation(radians)
def sine_approximation(x):
"""Approximate sin(x) using a Taylor series expansion."""
# Use the Taylor series expansion up to the x^9 term
x2 = x * x
x3 = x * x2
x5 = x3 * x2
x7 = x5 * x2
x9 = x7 * x2
return x - x3 / 6 + x5 / 120 - x7 / 5040 + x9 / 362880 # sin(x) ≈ x - x^3/3! + x^5/5! - x^7/7! + x^9/9!
def cos(degrees):
"""Calculate the cosine of an angle in degrees."""
# Normalize the angle to the range [-360, 360]
degrees = degrees % 360
if degrees > 180:
degrees -= 360
# Convert degrees to radians
radians = degrees * (PI / 180)
# Use a polynomial approximation for cosine
return cosine_approximation(radians)
def cosine_approximation(x):
"""Approximate cos(x) using a Taylor series expansion."""
x2 = x * x
x4 = x2 * x2
x6 = x4 * x2
x8 = x6 * x2
return 1 - x2 / 2 + x4 / 24 - x6 / 720 + x8 / 40320 # cos(x) ≈ 1 - x^2/2! + x^4/4! - x^6/6! + x^8/8!
def tan(degrees):
"""Calculate the tangent of an angle in degrees."""
# Calculate sine and cosine
sinx = sin(degrees)
cosx = cos(degrees)
if cosx == 0:
raise ValueError("Tangent is undefined at this angle.")
return sinx / cosx
def asin(x):
"""Calculate the arcsine (inverse sine) of x, result in degrees."""
if x < -1 or x > 1:
raise ValueError("Math domain error: input must be in [-1, 1].")
# Use a polynomial approximation for asin(x)
result = asin_approximation(x)
# Convert radians to degrees
return result * (180 / PI)
def asin_approximation(x):
"""Approximate asin(x) using a Taylor series expansion."""
# Taylor series expansion for asin(x) around 0
x3 = x ** 3
x5 = x ** 5
x7 = x ** 7
x9 = x ** 9
return x + x3 / 6 + 3 * x5 / 40 + 5 * x7 / 112 + 35 * x9 / 1152 # asin(x) ≈ x + x^3/6 + 3x^5/40 + ...
def acos(x):
"""Calculate the arccosine (inverse cosine) of x, result in degrees."""
if x < -1 or x > 1:
raise ValueError("Math domain error: input must be in [-1, 1].")
# acos(x) = PI/2 - asin(x)
return 90 - asin(x)
def atan(x):
"""Calculate the arctangent (inverse tangent) of x, result in degrees."""
# Use different approximations based on the value of x
if x == 0:
return 0
elif abs(x) <= 1:
return atan_approximation(x) * (180 / PI)
else:
# For |x| > 1, use atan(x) = sign(x) * 90 - atan(1/|x|)
atan_inv = atan_approximation(1 / abs(x)) * (180 / PI)
return (90 if x > 0 else -90) - atan_inv
def atan_approximation(x):
"""Approximate atan(x) using a Taylor series expansion."""
# Taylor series expansion for atan(x) around 0
x3 = x ** 3
x5 = x ** 5
x7 = x ** 7
x9 = x ** 9
return x - x3 / 3 + x5 / 5 - x7 / 7 + x9 / 9 # atan(x) ≈ x - x^3/3 + x^5/5 - x^7/7 + x^9/9
def mod(a, b):
"""Calculate the modulus of a by b without using the modulus operator."""
return a - b * (a // b)
Explanation:
Normalization: For sin, cos, and tan, the input angle in degrees is first normalized to the range ([-180^\circ, 180^\circ]) to improve the accuracy of the approximations.
Degree to Radian Conversion: The functions convert degrees to radians using the constant (\pi) before applying the approximations.
Approximations:
Sine and Cosine: Used the Taylor series expansion up to the 9th degree term for sin and 8th degree term for cos.
Arcsine: Used the Taylor series expansion for asin around 0, valid for (|x| \leq 1).
Arccosine: Computed as ( \arccos(x) = 90^\circ - \arcsin(x) ).
Arctangent: For (|x| \leq 1), used the Taylor series expansion. For (|x| > 1), used the identity ( \arctan(x) = 90^\circ - \arctan\left(\frac{1}{x}\right) ).
Modulus Function: Implemented mod(a, b) using integer division and subtraction to avoid using the modulo operator.
Note: The Taylor series approximations provide good accuracy for small values of ( x ), but their accuracy decreases for larger ( x ). For angles far from zero or inputs with large magnitudes, the results may not be precise. These implementations are suitable for educational purposes or applications where high precision is not critical.
Example Usage:
python
# Example usage
angle = 30
print(f"sin({angle}) = {sin(angle)}")
print(f"cos({angle}) = {cos(angle)}")
print(f"tan({angle}) = {tan(angle)}")
value = 0.5
print(f"asin({value}) = {asin(value)}")
print(f"acos({value}) = {acos(value)}")
print(f"atan({value}) = {atan(value)}")
a, b = 10, 3
print(f"mod({a}, {b}) = {mod(a, b)}")