Calculate arc tan - Java java.lang

Java examples for java.lang:Math Trigonometric Function

Description

Calculate arc tan

Demo Code

/*/*from  ww w .j a v a  2  s . c om*/
 * Copyright 2009 Andrey Khalzov, and individual contributors as indicated by the @author tag.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 */
//package com.java2s;

public class Main {
    public static final double PI_2 = Math.PI / 2;
    public static final double PI_6 = Math.PI / 6;
    public static final double SQRT_3 = Math.sqrt(3);

    public static double arctan(double x) {
        int sta = 0;
        int sp = 0;
        double x2;
        double a;

        /* check up the sign change */
        if (x < 0) {
            x = -x;
            sta |= 1;
        }

        /* check up the invertation */
        if (x > 1) {
            x = 1 / x;
            sta |= 2;
        }

        /* process shrinking the domain until x<PI/12 */
        while (x > PI_2) {
            sp++;
            a = x + SQRT_3;
            a = 1 / a;
            x *= SQRT_3;
            x -= 1;
            x *= a;
        }

        /* calculation core */
        x2 = x * x;
        a = x2 + 1.4087812;
        a = 0.55913709 / a;
        a += 0.60310579;
        a -= 0.05160454 * x2;
        a *= x;

        /* process until sp=0 */
        while (sp > 0) {
            a += PI_6;
            sp--;
        }
        /* invertation took place */
        if ((sta & 2) > 0) {
            a = PI_2 - a;
        }

        /* sign change took place */
        if ((sta & 1) > 0) {
            a = -a;
        }
        return a;
    }
}

Related Tutorials