Post Reply 
Arctangent function program [HP-42S, Free42]
09-17-2024, 04:32 PM
Post: #22
RE: Arctangent function program [HP-42S, Free42]
(09-17-2024 04:23 AM)Gerson W. Barbosa Wrote:  Anyway, I’ve found out that [for tan(x)] more than 9 iterations are needed.
Apparently no accuracy improvement beyond 11 or 12.

It depends on domain. see https://demonstrations.wolfram.com/Conti...ntFunction
If tan argument limited to ±pi/4, 8 iterations already have 16+ digits accuracy.

Instead of getting CF result bottom-up, with fixed iterations, we can also do this top-down.
see https://www.ams.org/journals/mcom/1952-0...9650-3.pdf, Method III

Note: there is a typo in formula, it should be 1+p(i) = 1 / (1 + r(i)*(1+p(i-1)))
This is why I use variable p1 = 1+p instead.

Code:
function my_tan(x)
    local a, b = x, 1    -- CF = x/(1 - x^2/(3 - x^2(5 - x^2(7 - ...
    local t = a/b
    local s = t  
    local p1 = 1        -- = p+1
    for k=1,inf do      -- other CF terms
        a, b, b0 = -x^2, b+2, b
        p1 = b0 / (b0 + p1*a/b)
        t = t * (p1-1)
        s = s + t
        print(k, t, s)
        if s==s+t then return s end
    end
end


lua> my_tan(pi/4)
1      0.2032910765367568      0.9886892399342051
2      0.011098440980763355    0.9997876809149685
3      0.00021018750072653323  0.999997868415695
4      2.1181106602376684e-06  0.9999999865263552
5      1.341497025916972e-08   0.9999999999413255
6      5.84877225566155e-11    0.9999999999998133
7      1.8641515888296674e-13  0.9999999999999997
8      4.534798657058108e-16   1.0000000000000002
9      8.698065146274166e-19   1.0000000000000002
1.0000000000000002
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Arctangent function program [HP-42S, Free42] - Albert Chan - 09-17-2024 04:32 PM



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