Link to home
Start Free TrialLog in
Avatar of _Katka_
_Katka_Flag for Czechia

asked on

How to calculate ArcTan(x) ?

Hi, I have a tricky question. I need an algorithm to calculate the inverse tangent (tan ^ -1) for a specific floating-point x (-inf < x < inf). It has to converge fast or at least there must be a mechanism to calculate it to a specific number of digits ?

I've tried many algorithms (Wiki, MathWorld) but don't feel constraint by any of them (I probably got them wrong).. the priority is a functional one.. You can use almost any approach but try to avoid complex numbers, other inverse functions (I intend to use ArcTan to calculate the others).

So the priorities are:

1) functioning algorithm (customizable digit precision)
2) fast algorithm
3) less complex algorithm to implement or one using less special constants and operations (Bernoulli numbers or other specialities)

thanks in advance and best regards,
Kate
Avatar of ozo
ozo
Flag of United States of America image

Avatar of _Katka_

ASKER

Thanks for the answer but I need an arbitrary precision to a specific digit. This only returns the "standard" double format (exponential format) I should use Math.Atan(x) for this. I need an algorithm to calculate ArcTan to 1 - (2^32-1) floating-point digits (at least theoretically). Basically I need an algorithm from the Windows Calculator :P

regards,
Kate

P.S.: I forgot to mention I prefer these languages C#, Pascal, Object pascal, C, C++, Python, VB.NET or just plain explanation using meta-language (maybe even the equation in format ArcTan(x) = ..)
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of _Katka_

ASKER

Ok, I was able to implement the algorithm for the interval [-1, 1] suddenly the Taylor series started to give the useful results. So what I need now is an ArcTan for the rest of the values (-, -1) and (1, ).

The used Taylor series for {XR | -1 e x e 1}

(-1 ^ n) ((x ^ (2n + 1)) / (2n + 1))

I tried to lower the x by this rule:

ArcTan(x) = (À / 2) - ArcTan(1 / x)

But it doesn't converge quickly enough. I hope someone will enlight me.

regards,
Kate
Avatar of _Katka_

ASKER

Ok, thx ozo, I was writing my comment while you posted. I'll try this.. :)

regards,
Kate
Avatar of _Katka_

ASKER

Hi.. the normalization..

ArcTan(x) = 2 * ArcTan((Sqrt(1 + x ^ 2) - 1) / x)

..was working fine but doesn't converged fast enough for me.. I searched the old normalizations I tried before when the main loop wasn't working (from what I know now) and one of them satisfied the rate of convergence.

ArcTan(x) = PI / 6 + ArcTan((Sqrt(3)*x - 1) / (Sqrt(3) + x)) for x > 2 - Sqrt(3)

thanks anyway.. somehow you pushed me in the right direction..

regards,
Kate