Java acos acos_v2(double x)

Here you can find the source of acos_v2(double x)

Description

acov

License

Apache License

Declaration

static public double acos_v2(double x) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2011 See AUTHORS file./*from  w  w  w .j av a 2  s .com*/
 * 
 * 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.
 ******************************************************************************/

public class Main {
    static public double acos_v2(double x) {
        double xa, t;
        xa = Math.abs(x);
        /* arcsin(x) = pi/2 - 2 * arcsin (sqrt ((1-x) / 2)) 
         * arccos(x) = pi/2 - arcsin(x)
         * arccos(x) = 2 * arcsin (sqrt ((1-x) / 2))
         */
        if (xa > 0.5625) {
            t = 2.0 * asin_core(sqrt(0.5 * (1.0 - xa)));
        } else {
            t = 1.5707963267948966 - asin_core(xa);
        }
        /* arccos (-x) = pi - arccos(x) */
        return (x < 0.0) ? (3.1415926535897932 - t) : t;
    }

    static public double asin_core(double x) {
        double x8, x4, x2;
        x2 = x * x;
        x4 = x2 * x2;
        x8 = x4 * x4;
        /* evaluate polynomial using a mix of Estrin's and Horner's scheme */
        return (((4.5334220547132049e-2 * x2 - 1.1226216762576600e-2) * x4
                + (2.6334281471361822e-2 * x2 + 2.0596336163223834e-2)) * x8
                + (3.0582043602875735e-2 * x2 + 4.4630538556294605e-2) * x4
                + (7.5000364034134126e-2 * x2 + 1.6666666300567365e-1)) * x2 * x + x;
    }

    /** Fast sqrt method. Default passes it through one round of Newton's method.
     * 
     * @param value
     * @return
     */
    static public double sqrt(double value) {
        double sqrt = Double.longBitsToDouble(((Double.doubleToLongBits(value) - (1l << 52)) >> 1) + (1l << 61));
        return (sqrt + value / sqrt) / 2.0;
    }
}

Related

  1. acos(float x)
  2. acos(int f)
  3. acos(Integer a)
  4. acos(Long a)
  5. acos(Number x)
  6. acosh(double a)
  7. acosh(double x)
  8. acosh(double x)
  9. acosh(double x)