Java acos acos(double a)

Here you can find the source of acos(double a)

Description

Returns the arc cosine of an angle, in the range of 0 through pi.

License

Open Source License

Parameter

Parameter Description
a the value whose arc cosine is to be returned.

Return

the arc cosine of the argument.

Declaration

public static double acos(double a) 

Method Source Code

//package com.java2s;
/*/*ww w.  j  a  va  2  s .c o m*/
 * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * only, as published by the Free Software Foundation.
 * 
 * This code is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License version 2 for more details (a copy is
 * included in the LICENSE file that accompanied this code).
 * 
 * You should have received a copy of the GNU General Public License
 * version 2 along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 * 
 * Please contact Sun Microsystems, Inc., 16 Network Circle, Menlo
 * Park, CA 94025 or visit www.sun.com if you need additional
 * information or have any questions.
 */

public class Main {
    private static final long no_sign_mask = 0x7FFFFFFFFFFFFFFFL;
    private static final long one = 0x3ff0000000000000L;
    private static final long half = 0x3fe0000000000000L;
    private static final long high_bits_mask = 0xffffffff00000000L;
    private static final double pio2_hi = 1.57079632679489655800e+00;
    private static final double pio2_lo = 6.12323399573676603587e-17;
    private static final double arc_pS0 = 1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */
            arc_pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */
            arc_pS2 = 2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */
            arc_pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */
            arc_pS4 = 7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */
            arc_pS5 = 3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */
            arc_qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */
            arc_qS2 = 2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */
            arc_qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */
            arc_qS4 = 7.70381505559019352791e-02;

    /**
     * Returns the arc cosine of an angle, in the range of 0 through <i>pi</i>. 
     * Special cases:
     * <ul>
     *  <li>If the argument is NaN or its absolute value is greater than 1, then the result is NaN.
     * </ul>
     *
     * @param a the value whose arc cosine is to be returned.
     * @return the arc cosine of the argument.
     */
    public static double acos(double a) {
        double z, p, q, r, w, s, c, df;
        long hx = Double.doubleToLongBits(a);
        long ix = hx & no_sign_mask;

        /* |x| >= 1 */
        if (Math.abs(a) >= 1) {
            /* |x| == 1*/
            if (ix == one) {
                if (hx > 0)
                    return (0.0);
                else
                    return (Math.PI + 2.0 * pio2_lo);
            }
            /* acos(|x| > 1) is NaN */
            return ((a - a) / (a - a));
        }

        /* |x| < 0.5 */
        if (ix < half) {
            /* if |x| < 2^-57 */
            if (ix <= 0x3c600000ffffffffL)
                return (pio2_hi + pio2_lo);
            z = a * a;
            p = z * (arc_pS0 + z * (arc_pS1 + z * (arc_pS2 + z * (arc_pS3 + z * (arc_pS4 + z * arc_pS5)))));
            q = 1.0 + z * (arc_qS1 + z * (arc_qS2 + z * (arc_qS3 + z * arc_qS4)));
            r = p / q;
            return (pio2_hi - (a - (pio2_lo - a * r)));
        }
        /* x < -0.5 */
        else if (hx < 0) {
            z = (1.0 + a) * 0.5;
            p = z * (arc_pS0 + z * (arc_pS1 + z * (arc_pS2 + z * (arc_pS3 + z * (arc_pS4 + z * arc_pS5)))));
            q = 1.0 + z * (arc_qS1 + z * (arc_qS2 + z * (arc_qS3 + z * arc_qS4)));
            s = Math.sqrt(z);
            r = p / q;
            w = r * s - pio2_lo;
            return (Math.PI - 2.0 * (s + w));
        }
        /* x > 0.5 */
        else {
            z = (1.0 - a) * 0.5;
            s = Math.sqrt(z);
            df = s;
            df = Double.longBitsToDouble(Double.doubleToLongBits(df) & high_bits_mask);
            c = (z - df * df) / (s + df);
            p = z * (arc_pS0 + z * (arc_pS1 + z * (arc_pS2 + z * (arc_pS3 + z * (arc_pS4 + z * arc_pS5)))));
            q = 1.0 + z * (arc_qS1 + z * (arc_qS2 + z * (arc_qS3 + z * arc_qS4)));
            r = p / q;
            w = r * s + c;
            return (2.0 * (df + w));
        }
    }
}

Related

  1. acos(double a)
  2. acos(double d)
  3. acos(double x)
  4. acos(double x)
  5. acos(final float a)