Compute hyperbolic sine of a double. - Java java.lang

Java examples for java.lang:Math Operation

Description

Compute hyperbolic sine of a double.

Demo Code

/**   Basic numeric operations not included in standard Java run-time library.
 *
 *   <p>/*from   w  ww.  j av  a  2s .c  o m*/
 *   The binomial methods are modified from those in the Colt library.
 *   </p>
 *
 *   <p>
 *   The following methods for trigonometric functions come from the
 *   Sfun class written by Visual Numerics.
 *   </p>
 *
 *   <ul>
 *   <li>acosh</li>
 *   <li>asinh</li>
 *   <li>atanh</li>
 *   <li>cot</li>
 *   <li>cosh</li>
 *   <li>sinh</li>
 *   <li>tanh</li>
 *   </ul>
 *
 *   <p>
 *   These methods are covered by the following license.
 *   </p>
 *
 * -------------------------------------------------------------------------
 *   $Id: Sfun.java,v 1.1.1.1 1999/03/05 21:43:39 brophy Exp $
 * -------------------------------------------------------------------------
 * 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 licensed 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{
    public static void main(String[] argv) throws Exception{
        double x = 2.45678;
        System.out.println(sinh(x));
    }
    private static final double SINH_COEF[] = { 0.1730421940471796,
            0.08759422192276048, 0.00107947777456713, 0.00000637484926075,
            0.00000002202366404, 0.00000000004987940, 0.00000000000007973,
            0.00000000000000009 };
    /**   Compute hyperbolic sine of a double.
     *
     *   @param   x   The number whose hyperbolic sine is desired.
     *
     *   @return      The hyperbolic sine of x.
     *
     *   <p>
     *   This method is a modified version of the one in the
     *   Visual Numerics Sfun class.
     *   </p>
     */

    public static double sinh(double x) {
        double ans;

        double y = Math.abs(x);

        if (Double.isNaN(x)) {
            ans = Double.NaN;
        }

        else if (Double.isInfinite(y)) {
            return x;
        }
        // 2.58096e-08 = Math.sqrt( 6.0 * EPSILON_SMAL L)

        else if (y < 2.58096e-08) {
            ans = x;
        }

        else if (y <= 1.0D) {
            ans = x
                    * (1.0D + Polynomial.evaluateChebyschev(SINH_COEF, 2.0D
                            * x * x - 1.0D));
        }

        else {
            y = Math.exp(y);

            // 94906265.62 = 1.0/Math.sqrt(EPSILON_SMALL)

            if (y >= 94906265.62D) {
                ans = sign(0.5D * y, x);
            } else {
                ans = sign(0.5D * (y - 1.0D / y), x);
            }
        }

        return ans;
    }
    /**   Return sign of an integer.
     *
     *   @param   n   Number whose sign is desired.
     *
     *   @return      -1 if n < 0, 0 if n isn 0, 1 if n > 0.
     */

    public static int sign(int n) {
        if (n > 0) {
            return 1;
        } else if (n < 0) {
            return -1;
        }

        return 0;
    }
    /**   Return sign of a double.
     *
     *   @param   d   double whose sign is desired.
     *
     *   @return      -1 if d < 0, 0 if d is 0, 1 if d > 0.
     */

    public static int sign(double d) {
        if (d > 0.0D) {
            return 1;
        } else if (d < 0.0D) {
            return -1;
        }

        return 0;
    }
    /**   Return the value of a double with the sign of another double.
     *
     *   @param   x   First double.
     *   @param   y   Second double.
     *
     *   @return      x with the sign of y.
     */

    public static double sign(double x, double y) {
        double abs_x = ((x < 0) ? -x : x);

        return (y < 0.0) ? -abs_x : abs_x;
    }
}

Related Tutorials