Post Reply 
Question for Trig Gurus
07-30-2022, 06:27 PM (This post was last modified: 07-31-2022 11:02 AM by Albert Chan.)
Post: #26
RE: Question for Trig Gurus
For 5+ digits accuracy, code is simple, using only 2 square roots.
Code:
function atand(x) -- = deg(atan(x)), |x| <= 1, 5+ digits accuracy
    local g = sqrt(1+x*x)
    local a = (1+g)/2
    a = 1 + 12*a + 32*sqrt(a*g)
    return x/a * 8100/pi
end

Assume we have signed zero, atand(x) = sign(x)*90 - atand(1/x)
--> maximum error (both absolute and relative) at x = ±1

lua> r3 = sqrt(3)
lua> atand(1/(2+r3)), atand(1/r3), atand(1)
15.000000161153318      30.000021200229554      45.00037955691671

Update: for |x| = 1/√3 .. √3, we can map it within ±1/√3, getting 6+ digits accuracy

atand(x) = sign(x)*45 - atand((1-x*x)/(2*x)) / 2

---

Code based on sequence ak = (x/2^k) / tan(atan(x)/2^k)
see An algorithm for computing Logarithms and ArcTangents, by B. C. Carlson

a0 = x/x = 1, g0 = sqrt(1+x*x),
a1 = (x/2) / tan(atan(x)/2) = (a0+g0)/2, g1 = sqrt(a1*g0)
a2 = (x/4) / tan(atan(x)/4) = (a1+g1)/2

We use Richardson extrapolation to extrapolate for a = x / atan(x)
Since we don't care intermediate values, we can apply weight directly.
We just need to know what weight to use ...

CAS> [1]
CAS> 4^len(Ans)*Ans - extend([0],Ans)
[4, -1]
[64, -20, 1]
[4096, -1344, 84, -1]
[1048576, -348160, 22848, -340, 1]
...

45*a ≈ (a0 - 20*a1 + 64*a2) = (a0 - 20*a1 + 32*(a1+g1)) = (a0 + 12*a1 + 32*g1)
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Question for Trig Gurus - Namir - 12-01-2014, 07:49 PM
RE: Question for Trig Gurus - toml_12953 - 12-01-2014, 08:08 PM
RE: Question for Trig Gurus - PANAMATIK - 12-01-2014, 08:46 PM
RE: Question for Trig Gurus - Namir - 12-01-2014, 10:54 PM
RE: Question for Trig Gurus - toml_12953 - 12-02-2014, 02:30 AM
RE: Question for Trig Gurus - Namir - 12-02-2014, 09:21 AM
RE: Question for Trig Gurus - Namir - 12-02-2014, 04:57 PM
RE: Question for Trig Gurus - Albert Chan - 07-29-2022, 03:19 PM
RE: Question for Trig Gurus - Mark Hardman - 12-01-2014, 11:00 PM
RE: Question for Trig Gurus - Thomas Klemm - 12-02-2014, 12:09 AM
RE: Question for Trig Gurus - Namir - 12-02-2014, 12:16 AM
RE: Question for Trig Gurus - Thomas Klemm - 12-02-2014, 01:12 AM
RE: Question for Trig Gurus - Namir - 12-02-2014, 01:50 AM
RE: Question for Trig Gurus - Namir - 12-02-2014, 01:49 AM
RE: Question for Trig Gurus - Namir - 12-05-2014, 02:48 AM
RE: Question for Trig Gurus - Namir - 12-09-2014, 02:19 PM
RE: Question for Trig Gurus - Thomas Klemm - 07-29-2022, 12:59 PM
RE: Question for Trig Gurus - ttw - 07-29-2022, 10:19 PM
RE: Question for Trig Gurus - Thomas Klemm - 07-30-2022, 08:26 AM
RE: Question for Trig Gurus - Albert Chan - 07-30-2022 06:27 PM
RE: Question for Trig Gurus - Thomas Klemm - 07-30-2022, 09:38 AM
RE: Question for Trig Gurus - Thomas Klemm - 07-31-2022, 11:08 AM
RE: Question for Trig Gurus - Albert Chan - 07-31-2022, 09:54 PM
RE: Question for Trig Gurus - Thomas Klemm - 08-01-2022, 05:19 AM
RE: Question for Trig Gurus - Albert Chan - 08-01-2022, 02:36 PM
RE: Question for Trig Gurus - Thomas Klemm - 08-02-2022, 06:35 AM
RE: Question for Trig Gurus - Albert Chan - 08-02-2022, 05:28 PM
RE: Question for Trig Gurus - Thomas Klemm - 08-03-2022, 04:42 PM



User(s) browsing this thread: 3 Guest(s)