HP 17B Solver - another ARCTAN(Y/X) approximation
05-31-2021, 09:51 PM (This post was last modified: 06-12-2021 03:10 PM by Albert Chan.)
Post: #10
 Albert Chan Senior Member Posts: 2,066 Joined: Jul 2018
RE: HP 17B Solver - another ARCTAN(Y/X) approximation
(05-31-2021 04:42 PM)Albert Chan Wrote:  $$\displaystyle\arctan(x) = 2\arctan\left( {x \over \sqrt{2\sqrt{1+x^2}+x^2+2}} \right)$$

There is no need to use RC formulas to solve for h, for atan(x) = 2*atan(h)

Let h = tan(θ), |θ| ≤ pi/4

2*θ = atan(tan(2*θ))
2*atan(h) = atan(2*h/(1-h*h)) = atan(x)

2*h/(1-h*h) = x
x*h^2 + 2*h - x = 0

Remove quadratic solution of wrong sign (h and x sign should match), we have:

h = (√(1+x²)-1) / x = x / (√(1+x²)+1)

This h is same as above quoted formula, only simpler

$$\displaystyle\arctan(x) = 2\arctan\left( {x \over \sqrt{1+x^2}+1} \right)$$

Code:
from math import sqrt def myatan(x, k=1):     y = x*x     if y > 1e-3: return myatan(x/(sqrt(1+y)+1), k+k)     return k*(x - x*y*(1/3 - y*(1/5 - y*(1/7 - y*(1/9)))))

>>> myatan(1)
0.78539816339744839
>>> myatan(.5)
0.46364760900080615

>>> myatan(10) # |atan(x) = 2θ| ≤ pi/2
1.4711276743037345
>>> myatan(100)
1.5607966601082317

Update: Thomas Kleem beats me to it 2 years ago