I need to calculate the arc tan value from my Blackberry Java app. Unfortunately, the blackberry 4.2 api doesn't have the Math.atan() function. Version 4.6 of the Blackberry JDE has it, but not 4.2.
Does anyone know of a workaround to calculate atan?
When all else fails, one could probably obtain a decent value by estimating the result of an infinite series of the
arctan
function.The Wikipedia page on inverse trigonometic functions has a section on the infinite series of inverse trigonometric functions, including
arctan
. In order to obtain an estimate, one would carry out the infinite series until the desired precision is obtained.As for the reason why the
arctan
function is not included, it is probably because the processor in the Blackberry isn't very powerful, and would take a lot of processor resources to perform the calculation.Also, looking at the Blackberry JDE 4.2 API documentation, there appears to be a fixed-point math library called
Fixed32
which offers two flavors of arctan. They perform the calculation with 32-bit integers, so they probably offer some performance advantages over performing floating-point arithmetic.From Arctan in J2ME by Stephen Zimmerman:
First implement the standard
arctan(x)
using Taylor series (as described at http://en.wikipedia.org/wiki/Inverse_trigonometric_functions#Infinite_series)The do the following before calling arctan:
1) First do this check.
2) if
|x| > 1
, computearctan(1/x)
and finally subtract the result fromPi/2
3) if
|x|
is close to1
, compute arctan of the half angle using the half angle formulaarctan(x) = 2*arctan(x/(1+sqrt(1+x*x)))
. That is, first compute the half angle and then multiply the result by 2. Otherwise, for|x|
close to 1, arctan converges very slowly.Here is the function I use (no guarantees that it is very fast):
I had the same problem... the missing math functions can be found in the following package:
net.rim.device.api.util.MathUtilities