refactor: Make code more simple in maclaurin_series (#7522)

This commit is contained in:
Caeden Perelli-Harris 2022-10-23 12:01:51 +01:00 committed by GitHub
parent f32f78a9e0
commit a0cbc2056e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -52,8 +52,7 @@ def maclaurin_sin(theta: float, accuracy: int = 30) -> float:
div = theta // (2 * pi)
theta -= 2 * div * pi
return sum(
(((-1) ** r) * ((theta ** (2 * r + 1)) / factorial(2 * r + 1)))
for r in range(accuracy)
(-1) ** r * theta ** (2 * r + 1) / factorial(2 * r + 1) for r in range(accuracy)
)
@ -104,12 +103,14 @@ def maclaurin_cos(theta: float, accuracy: int = 30) -> float:
theta = float(theta)
div = theta // (2 * pi)
theta -= 2 * div * pi
return sum(
(((-1) ** r) * ((theta ** (2 * r)) / factorial(2 * r))) for r in range(accuracy)
)
return sum((-1) ** r * theta ** (2 * r) / factorial(2 * r) for r in range(accuracy))
if __name__ == "__main__":
import doctest
doctest.testmod()
print(maclaurin_sin(10))
print(maclaurin_sin(-10))
print(maclaurin_sin(10, 15))