tanhl.c revision 61ec6b12089c560a32ebd9efdbb057ff92665e60
2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
2N/A */
2N/A/*
2N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma weak __tanhl = tanhl
2N/A
2N/A/*
2N/A * tanhl(x) returns the hyperbolic tangent of x
2N/A *
2N/A * Method :
2N/A * 1. reduce x to non-negative: tanhl(-x) = - tanhl(x).
2N/A * 2.
2N/A * 0 < x <= small : tanhl(x) := x
2N/A * -expm1l(-2x)
2N/A * small < x <= 1 : tanhl(x) := --------------
2N/A * expm1l(-2x) + 2
2N/A * 2
2N/A * 1 <= x <= threshold : tanhl(x) := 1 - ---------------
2N/A * expm1l(2x) + 2
2N/A * threshold < x <= INF : tanhl(x) := 1.
2N/A *
2N/A * where
2N/A * single : small = 1.e-5 threshold = 11.0
2N/A * double : small = 1.e-10 threshold = 22.0
2N/A * quad : small = 1.e-20 threshold = 45.0
2N/A *
2N/A * Note: threshold was chosen so that
2N/A * fl(1.0+2/(expm1(2*threshold)+2)) == 1.
2N/A *
2N/A * Special cases:
2N/A * tanhl(NaN) is NaN;
2N/A * only tanhl(0.0)=0.0 is exact for finite argument.
2N/A */
2N/A
2N/A#include "libm.h"
2N/A#include "longdouble.h"
2N/A
2N/Astatic const long double small = 1.0e-20L, one = 1.0, two = 2.0,
2N/A#ifndef lint
2N/A big = 1.0e+20L,
2N/A#endif
2N/A threshold = 45.0L;
2N/A
2N/Along double
2N/Atanhl(long double x) {
2N/A long double t, y, z;
2N/A int signx;
2N/A#ifndef lint
2N/A volatile long double dummy;
2N/A#endif
2N/A
2N/A if (isnanl(x))
2N/A return (x + x); /* x is NaN */
2N/A signx = signbitl(x);
2N/A t = fabsl(x);
2N/A z = one;
2N/A if (t <= threshold) {
2N/A if (t > one)
2N/A z = one - two / (expm1l(t + t) + two);
2N/A else if (t > small) {
2N/A y = expm1l(-t - t);
2N/A z = -y / (y + two);
2N/A } else {
2N/A#ifndef lint
2N/A dummy = t + big;
2N/A /* inexact if t != 0 */
2N/A#endif
2N/A return (x);
2N/A }
2N/A } else if (!finitel(t))
2N/A return (copysignl(one, x));
2N/A else
2N/A return (signx ? -z + small * small : z - small * small);
2N/A return (signx ? -z : z);
2N/A}
2N/A