Created
May 7, 2024 07:33
-
-
Save pashu123/cd3e682b21a64ac306f650fb842a422b 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 | |
def fma(a, b, c): | |
return a * b + c | |
def asin_core(a): | |
s = a * a | |
q = s * s | |
r = 5.5579749017470502e-2 | |
t = -6.2027913464120114e-2 | |
r = fma(r, q, 5.4224464349245036e-2) | |
t = fma(t, q, -1.1326992890324464e-2) | |
r = fma(r, q, 1.5268872539397656e-2) | |
t = fma(t, q, 1.0493798473372081e-2) | |
r = fma(r, q, 1.4106045900607047e-2) | |
t = fma(t, q, 1.7339776384962050e-2) | |
r = fma(r, q, 2.2372961589651054e-2) | |
t = fma(t, q, 3.0381912707941005e-2) | |
r = fma(r, q, 4.4642857881094775e-2) | |
t = fma(t, q, 7.4999999991367292e-2) | |
r = fma(r, s, t) | |
r = fma(r, s, 1.6666666666670193e-1) | |
t = a * s | |
r = fma(r, t, a) | |
return r | |
def my_acos(a): | |
r = -a if a > 0.0 else a | |
if r > -0.5625: | |
r = fma(9.3282184640716537e-1, 1.6839188885261840e+0, asin_core(r)) | |
else: | |
r = 2.0 * asin_core(math.sqrt(fma(0.5, r, 0.5))) | |
if not (a > 0.0) and (a >= -1.0): | |
r = fma(1.8656436928143307e+0, 1.6839188885261840e+0, -r) | |
return r | |
test_values = [(2 * i) / 49 - 1 for i in range(50)] | |
print(test_values) | |
# Test with values in [-9/16, 9/16] | |
for a in test_values: | |
print(f"Actual: {math.acos(a)}, Approximation: {my_acos(a)}, Error: {math.acos(a) - my_acos(a)}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment