From a0cbc2056e9b9ff4f8c5da682061996e783b13e3 Mon Sep 17 00:00:00 2001 From: Caeden Perelli-Harris Date: Sun, 23 Oct 2022 12:01:51 +0100 Subject: [PATCH] refactor: Make code more simple in maclaurin_series (#7522) --- maths/maclaurin_series.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/maths/maclaurin_series.py b/maths/maclaurin_series.py index 57edc90bf..a2619d4e6 100644 --- a/maths/maclaurin_series.py +++ b/maths/maclaurin_series.py @@ -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))