Returns the cotangent of a double. - Java java.lang

Java examples for java.lang:Math Trigonometric Function

Description

Returns the cotangent of a double.

Demo Code

/*/*from   w  ww  . j  av a2  s.  com*/
 * ------------------------------------------------------------------------=
 * Copyright (C) 1997 - 1998 by Visual Numerics, Inc. All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software is freely
 * granted by Visual Numerics, Inc., provided that the copyright notice
 * above and the following warranty disclaimer are preserved in human
 * readable form.
 *
 * Because this software is licenses free of charge, it is provided
 * "AS IS", with NO WARRANTY.  TO THE EXTENT PERMITTED BY LAW, VNI
 * DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO ITS PERFORMANCE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 * VNI WILL NOT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING OUT OF THE USE
 * OF OR INABILITY TO USE THIS SOFTWARE, INCLUDING BUT NOT LIMITED TO DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, PUNITIVE, AND EXEMPLARY DAMAGES, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 
 *
 * ------------------------------------------------------------------------=
 */
//package com.java2s;

public class Main {
    private static final double COT_COEF[] = {
            .240259160982956302509553617744970e+0,
            -.165330316015002278454746025255758e-1,
            -.429983919317240189356476228239895e-4,
            -.159283223327541046023490851122445e-6,
            -.619109313512934872588620579343187e-9,
            -.243019741507264604331702590579575e-11,
            -.956093675880008098427062083100000e-14,
            -.376353798194580580416291539706666e-16,
            -.148166574646746578852176794666666e-18 };

    /**
     * Returns the cotangent of a double.
     * 
     * @param x
     *            A double value.
     * @return The cotangent of x. If x is NaN, the result is NaN.
     */
    static public double cot(double x) {
        double ans, ainty, ainty2, prodbg, y, yrem;
        double pi2rec = 0.011619772367581343075535053490057; // 2/PI - 0.625

        y = Math.abs(x);

        if (y > 4.5036e+15) {
            // 4.5036e+15 = 1.0/EPSILON_LARGE
            return Double.NaN;
        }

        // Carefully compute
        // Y * (2/PI) = (AINT(Y) + REM(Y)) * (.625 + PI2REC)
        // = AINT(.625*Y) + REM(.625*Y) + Y*PI2REC = AINT(.625*Y) + Z
        // = AINT(.625*Y) + AINT(Z) + REM(Z)
        ainty = (int) y;
        yrem = y - ainty;
        prodbg = 0.625 * ainty;
        ainty = (int) prodbg;
        y = (prodbg - ainty) + 0.625 * yrem + y * pi2rec;
        ainty2 = (int) y;
        ainty = ainty + ainty2;
        y = y - ainty2;

        int ifn = (int) (ainty % 2.0);
        if (ifn == 1)
            y = 1.0 - y;

        if (y == 0.0) {
            ans = Double.POSITIVE_INFINITY;
        } else if (y <= 1.82501e-08) {
            // 1.82501e-08 = Math.sqrt(3.0*EPSILON_SMALL)
            ans = 1.0 / y;
        } else if (y <= 0.25) {
            ans = (0.5 + csevl(32.0 * y * y - 1.0, COT_COEF)) / y;
        } else if (y <= 0.5) {
            ans = (0.5 + csevl(8.0 * y * y - 1.0, COT_COEF)) / (0.5 * y);
            ans = (ans * ans - 1.0) * 0.5 / ans;
        } else {
            ans = (0.5 + csevl(2.0 * y * y - 1.0, COT_COEF)) / (0.25 * y);
            ans = (ans * ans - 1.0) * 0.5 / ans;
            ans = (ans * ans - 1.0) * 0.5 / ans;
        }
        if (x != 0.0)
            ans = sign(ans, x);
        if (ifn == 1)
            ans = -ans;
        return ans;
    }

    static double csevl(double x, double coef[]) {
        double b0, b1, b2, twox;
        int i;
        b1 = 0.0;
        b0 = 0.0;
        b2 = 0.0;
        twox = 2.0 * x;
        for (i = coef.length - 1; i >= 0; i--) {
            b2 = b1;
            b1 = b0;
            b0 = twox * b1 - b2 + coef[i];
        }
        return 0.5 * (b0 - b2);
    }

    static private double sign(double x, double y) {
        double abs_x = ((x < 0) ? -x : x);
        return (y < 0.0) ? -abs_x : abs_x;
    }
}

Related Tutorials