Last active
May 27, 2020 18:43
-
-
Save danieldk/a4a661536eaae7afd02287520bb6064e to your computer and use it in GitHub Desktop.
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
import math | |
import random | |
FAC3 = 6. | |
FAC5 = 120. | |
FAC7 = 5040. | |
def poorMenSine(x): | |
return x - (x ** 3 / FAC3) + (x ** 5 / FAC5) - (x ** 7 / FAC7) | |
nIter = 1_000_000 | |
deltaMax = 0. | |
deltaMin = math.inf | |
deltaSum = 0. | |
for _ in range(nIter): | |
v = random.uniform(-math.pi, math.pi) | |
delta = abs(math.sin(v) - poorMenSine(v)) | |
deltaSum += delta | |
if delta > deltaMax: | |
deltaMax = delta | |
if delta < deltaMin: | |
deltaMin = delta | |
print("Avg/min/max delta: {:.2e}/{:.2e}/{:.2e}" | |
.format(deltaSum/ nIter, deltaMin, deltaMax)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment