A fast implementation of Math#atan(double) . - Java java.lang

Java examples for java.lang:Math Trigonometric Function

Description

A fast implementation of Math#atan(double) .

Demo Code


//package com.java2s;

public class Main {
    /** A quarter pi. */
    public static final double M_PI_4 = Math.PI * 0.25;

    /**/*from w ww .java2  s  . co  m*/
     * A fast implementation of {@link Math#atan(double)}. The maximum error is
     * <code>0.0015</code> radians. The behavior is the same as the library
     * function. The algorithm comes from:
     * <em>"Efficient approximations for the arctangent function",
     * Rajan, S. Sichun Wang Inkol, R. Joyal, A., May 2006</em>
     * 
     * @param a The value whose arc tangent is to be returned.
     * @return The arc tangent of the argument.
     * @see Math#atan(double)
     */
    public static double fastArcTan(final double a) {
        if (a < -1 || a > 1)
            return Math.atan(a);
        return M_PI_4 * a - a * (Math.abs(a) - 1)
                * (0.2447 + 0.0663 * Math.abs(a));
    }
}

Related Tutorials