1*0a6a1f1dSLionel Sambuc /* $NetBSD: n_tanh.c,v 1.7 2014/03/06 10:59:00 martin Exp $ */
22fe8fb19SBen Gras /*
32fe8fb19SBen Gras * Copyright (c) 1985, 1993
42fe8fb19SBen Gras * The Regents of the University of California. All rights reserved.
52fe8fb19SBen Gras *
62fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
72fe8fb19SBen Gras * modification, are permitted provided that the following conditions
82fe8fb19SBen Gras * are met:
92fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
102fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
112fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
122fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
132fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
142fe8fb19SBen Gras * 3. Neither the name of the University nor the names of its contributors
152fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
162fe8fb19SBen Gras * without specific prior written permission.
172fe8fb19SBen Gras *
182fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
192fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
222fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
232fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
242fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
252fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
262fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
272fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
282fe8fb19SBen Gras * SUCH DAMAGE.
292fe8fb19SBen Gras */
302fe8fb19SBen Gras
312fe8fb19SBen Gras #ifndef lint
322fe8fb19SBen Gras #if 0
332fe8fb19SBen Gras static char sccsid[] = "@(#)tanh.c 8.1 (Berkeley) 6/4/93";
342fe8fb19SBen Gras #endif
352fe8fb19SBen Gras #endif /* not lint */
362fe8fb19SBen Gras
372fe8fb19SBen Gras /* TANH(X)
382fe8fb19SBen Gras * RETURN THE HYPERBOLIC TANGENT OF X
392fe8fb19SBen Gras * DOUBLE PRECISION (VAX D FORMAT 56 BITS, IEEE DOUBLE 53 BITS)
402fe8fb19SBen Gras * CODED IN C BY K.C. NG, 1/8/85;
412fe8fb19SBen Gras * REVISED BY K.C. NG on 2/8/85, 2/11/85, 3/7/85, 3/24/85.
422fe8fb19SBen Gras *
432fe8fb19SBen Gras * Required system supported functions :
442fe8fb19SBen Gras * copysign(x,y)
452fe8fb19SBen Gras * finite(x)
462fe8fb19SBen Gras *
472fe8fb19SBen Gras * Required kernel function:
482fe8fb19SBen Gras * expm1(x) ...exp(x)-1
492fe8fb19SBen Gras *
502fe8fb19SBen Gras * Method :
512fe8fb19SBen Gras * 1. reduce x to non-negative by tanh(-x) = - tanh(x).
522fe8fb19SBen Gras * 2.
532fe8fb19SBen Gras * 0 < x <= 1.e-10 : tanh(x) := x
542fe8fb19SBen Gras * -expm1(-2x)
552fe8fb19SBen Gras * 1.e-10 < x <= 1 : tanh(x) := --------------
562fe8fb19SBen Gras * expm1(-2x) + 2
572fe8fb19SBen Gras * 2
582fe8fb19SBen Gras * 1 <= x <= 22.0 : tanh(x) := 1 - ---------------
592fe8fb19SBen Gras * expm1(2x) + 2
602fe8fb19SBen Gras * 22.0 < x <= INF : tanh(x) := 1.
612fe8fb19SBen Gras *
622fe8fb19SBen Gras * Note: 22 was chosen so that fl(1.0+2/(expm1(2*22)+2)) == 1.
632fe8fb19SBen Gras *
642fe8fb19SBen Gras * Special cases:
652fe8fb19SBen Gras * tanh(NaN) is NaN;
662fe8fb19SBen Gras * only tanh(0)=0 is exact for finite argument.
672fe8fb19SBen Gras *
682fe8fb19SBen Gras * Accuracy:
692fe8fb19SBen Gras * tanh(x) returns the exact hyperbolic tangent of x nealy rounded.
702fe8fb19SBen Gras * In a test run with 1,024,000 random arguments on a VAX, the maximum
712fe8fb19SBen Gras * observed error was 2.22 ulps (units in the last place).
722fe8fb19SBen Gras */
732fe8fb19SBen Gras
742fe8fb19SBen Gras #include "mathimpl.h"
752fe8fb19SBen Gras
762fe8fb19SBen Gras double
tanh(double x)772fe8fb19SBen Gras tanh(double x)
782fe8fb19SBen Gras {
792fe8fb19SBen Gras static const double one=1.0, two=2.0, small = 1.0e-10, big = 1.0e10;
802fe8fb19SBen Gras double t, sign;
812fe8fb19SBen Gras
822fe8fb19SBen Gras #if !defined(__vax__)&&!defined(tahoe)
832fe8fb19SBen Gras if(x!=x) return(x); /* x is NaN */
842fe8fb19SBen Gras #endif /* !defined(__vax__)&&!defined(tahoe) */
852fe8fb19SBen Gras
862fe8fb19SBen Gras sign=copysign(one,x);
872fe8fb19SBen Gras x=copysign(x,one);
882fe8fb19SBen Gras if(x < 22.0)
892fe8fb19SBen Gras if( x > one )
902fe8fb19SBen Gras return(copysign(one-two/(expm1(x+x)+two),sign));
912fe8fb19SBen Gras else if ( x > small )
922fe8fb19SBen Gras {t= -expm1(-(x+x)); return(copysign(t/(two-t),sign));}
932fe8fb19SBen Gras else /* raise the INEXACT flag for non-zero x */
942fe8fb19SBen Gras { t = big+x; return(copysign(x,sign));} /* ??? -ragge */
952fe8fb19SBen Gras else if(finite(x))
962fe8fb19SBen Gras return (sign+1.0E-37); /* raise the INEXACT flag */
972fe8fb19SBen Gras else
982fe8fb19SBen Gras return(sign); /* x is +- INF */
992fe8fb19SBen Gras }
100*0a6a1f1dSLionel Sambuc
101*0a6a1f1dSLionel Sambuc float
tanhf(float x)102*0a6a1f1dSLionel Sambuc tanhf(float x)
103*0a6a1f1dSLionel Sambuc {
104*0a6a1f1dSLionel Sambuc return tanh(x);
105*0a6a1f1dSLionel Sambuc }
106