Java atanh atanh(double x)

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

Description

Returns the inverse (arc) hyperbolic tangent of a double.

License

Open Source License

Parameter

Parameter Description
x A double value.

Return

The arc hyperbolic tangent of x. If x is NaN or |x|>1, the result is NaN.

Declaration

static public double atanh(double x) 

Method Source Code

//package com.java2s;
/*/*  w  ww  . j  a  va  2s  .c om*/
 * =========================================================================
 * 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. 
 *
 * =========================================================================
 */

public class Main {
    private static final double ATANH_COEF[] = { .9439510239319549230842892218633e-1,
            .4919843705578615947200034576668e-1, .2102593522455432763479327331752e-2,
            .1073554449776116584640731045276e-3, .5978267249293031478642787517872e-5,
            .3505062030889134845966834886200e-6, .2126374343765340350896219314431e-7,
            .1321694535715527192129801723055e-8, .8365875501178070364623604052959e-10,
            .5370503749311002163881434587772e-11, .3486659470157107922971245784290e-12,
            .2284549509603433015524024119722e-13, .1508407105944793044874229067558e-14,
            .1002418816804109126136995722837e-15, .6698674738165069539715526882986e-17,
            .4497954546494931083083327624533e-18 };

    /**
     * Returns the inverse (arc) hyperbolic tangent of a double.
     * 
     * @param x
     *            A double value.
     * @return The arc hyperbolic tangent of x. If x is NaN or |x|>1, the result
     *         is NaN.
     */
    static public double atanh(double x) {
        double y = Math.abs(x);
        double ans;

        if (Double.isNaN(x)) {
            ans = Double.NaN;
        } else if (y < 1.82501e-08) {
            // 1.82501e-08 = Math.sqrt(3.0*EPSILON_SMALL)
            ans = x;
        } else if (y <= 0.5) {
            ans = x * (1.0 + csevl(8.0 * x * x - 1.0, ATANH_COEF));
        } else if (y < 1.0) {
            ans = 0.5 * Math.log((1.0 + x) / (1.0 - x));
        } else if (y == 1.0) {
            ans = x * Double.POSITIVE_INFINITY;
        } else {
            ans = Double.NaN;
        }
        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);
    }
}

Related

  1. atanh(double a)
  2. atanh(double z)