Created
April 26, 2024 11:08
-
-
Save pashu123/26c0c4e45b11634538a1637c115717fb 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 with values in [-9/16, 9/16] | |
for a in [-9/16, -3/8, -1/4, 0, 1/4, 3/8, 9/16]: | |
print(f"Actual: {math.acos(a)}, Approximation: {my_acos(a)}") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment