-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Maclaurin series approximation of sin #7451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
59fc97b
added maclaurin_sin.py function
ChrisO345 d95d329
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 36e8085
added type hints and fixed line overflows
ChrisO345 068020a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] aa5f203
removed incompatable type examples
ChrisO345 d03beff
Update maths/maclaurin_sin.py
ChrisO345 afd4d46
changed error details
ChrisO345 43c2b53
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 222d683
fixed grammatical errors
ChrisO345 0507353
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 257bb32
improved function accuracy and added test case
ChrisO345 3952724
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f276cb2
Update maths/maclaurin_sin.py
ChrisO345 655c050
removed redundant return
ChrisO345 921f361
fixed pytest
ChrisO345 cea7c63
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
""" | ||
https://en.wikipedia.org/wiki/Taylor_series#Trigonometric_functions | ||
""" | ||
from math import factorial, pi | ||
|
||
|
||
def maclaurin_sin(theta: float, accuracy: int = 30) -> float: | ||
""" | ||
Finds the maclaurin approximation of sin | ||
|
||
:param theta: the angle to which sin is found | ||
:param accuracy: the degree of accuracy wanted minimum ~ 1.5 theta | ||
:return: the value of sine in radians | ||
|
||
|
||
>>> from math import isclose, sin | ||
>>> all(isclose(maclaurin_sin(x, 50), sin(x)) for x in range(-25, 25)) | ||
True | ||
>>> maclaurin_sin(10) | ||
-0.544021110889369 | ||
>>> maclaurin_sin(-10) | ||
0.5440211108893703 | ||
>>> maclaurin_sin(10, 15) | ||
-0.5440211108893689 | ||
>>> maclaurin_sin(-10, 15) | ||
0.5440211108893703 | ||
>>> maclaurin_sin("10") | ||
Traceback (most recent call last): | ||
... | ||
ValueError: maclaurin_sin() requires either an int or float for theta | ||
>>> maclaurin_sin(10, -30) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: maclaurin_sin() requires a positive int for accuracy | ||
>>> maclaurin_sin(10, 30.5) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: maclaurin_sin() requires a positive int for accuracy | ||
>>> maclaurin_sin(10, "30") | ||
Traceback (most recent call last): | ||
... | ||
ValueError: maclaurin_sin() requires a positive int for accuracy | ||
""" | ||
|
||
if not isinstance(theta, (int, float)): | ||
raise ValueError("maclaurin_sin() requires either an int or float for theta") | ||
|
||
if not isinstance(accuracy, int) or accuracy <= 0: | ||
raise ValueError("maclaurin_sin() requires a positive int for accuracy") | ||
|
||
theta = float(theta) | ||
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) | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
ChrisO345 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print(maclaurin_sin(10)) | ||
print(maclaurin_sin(-10)) | ||
print(maclaurin_sin(10, 15)) | ||
print(maclaurin_sin(-10, 15)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.