xref: /csrg-svn/lib/libm/common_source/sinh.c (revision 31853)
124606Szliu /*
224606Szliu  * Copyright (c) 1985 Regents of the University of California.
324606Szliu  *
424606Szliu  * Use and reproduction of this software are granted  in  accordance  with
524606Szliu  * the terms and conditions specified in  the  Berkeley  Software  License
624606Szliu  * Agreement (in particular, this entails acknowledgement of the programs'
724606Szliu  * source, and inclusion of this notice) with the additional understanding
824606Szliu  * that  all  recipients  should regard themselves as participants  in  an
924606Szliu  * ongoing  research  project and hence should  feel  obligated  to report
1024606Szliu  * their  experiences (good or bad) with these elementary function  codes,
1124606Szliu  * using "sendbug 4bsd-bugs@BERKELEY", to the authors.
1224606Szliu  */
1324606Szliu 
1424606Szliu #ifndef lint
1524706Selefunt static char sccsid[] =
16*31853Szliu "@(#)sinh.c	4.3 (Berkeley) 8/21/85; 1.6 (ucb.elefunt) 07/13/87";
17*31853Szliu #endif	/* not lint */
1824606Szliu 
1924606Szliu /* SINH(X)
2024606Szliu  * RETURN THE HYPERBOLIC SINE OF X
2124606Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
2224606Szliu  * CODED IN C BY K.C. NG, 1/8/85;
2324606Szliu  * REVISED BY K.C. NG on 2/8/85, 3/7/85, 3/24/85, 4/16/85.
2424606Szliu  *
2524606Szliu  * Required system supported functions :
2624606Szliu  *	copysign(x,y)
2724606Szliu  *	scalb(x,N)
2824606Szliu  *
2924606Szliu  * Required kernel functions:
3024606Szliu  *	expm1(x)	...return exp(x)-1
3124606Szliu  *
3224606Szliu  * Method :
3324606Szliu  *	1. reduce x to non-negative by sinh(-x) = - sinh(x).
3424606Szliu  *	2.
3524606Szliu  *
3624606Szliu  *	                                      expm1(x) + expm1(x)/(expm1(x)+1)
3724606Szliu  *	    0 <= x <= lnovfl     : sinh(x) := --------------------------------
3824606Szliu  *			       		                      2
3924606Szliu  *     lnovfl <= x <= lnovfl+ln2 : sinh(x) := expm1(x)/2 (avoid overflow)
4024606Szliu  * lnovfl+ln2 <  x <  INF        :  overflow to INF
4124606Szliu  *
4224606Szliu  *
4324606Szliu  * Special cases:
4424606Szliu  *	sinh(x) is x if x is +INF, -INF, or NaN.
4524606Szliu  *	only sinh(0)=0 is exact for finite argument.
4624606Szliu  *
4724606Szliu  * Accuracy:
4824606Szliu  *	sinh(x) returns the exact hyperbolic sine of x nearly rounded. In
4924606Szliu  *	a test run with 1,024,000 random arguments on a VAX, the maximum
5024606Szliu  *	observed error was 1.93 ulps (units in the last place).
5124606Szliu  *
5224606Szliu  * Constants:
5324606Szliu  * The hexadecimal values are the intended ones for the following constants.
5424606Szliu  * The decimal values may be used, provided that the compiler will convert
5524606Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
5624606Szliu  * shown.
5724606Szliu  */
58*31853Szliu #if defined(vax)||defined(tahoe)
59*31853Szliu #ifdef vax
6031812Szliu #define _0x(A,B)	0x/**/A/**/B
61*31853Szliu #else	/* vax */
6231812Szliu #define _0x(A,B)	0x/**/B/**/A
63*31853Szliu #endif	/* vax */
6426893Selefunt /* static double */
6524606Szliu /* mln2hi =  8.8029691931113054792E1     , Hex  2^  7   *  .B00F33C7E22BDB */
6624606Szliu /* mln2lo = -4.9650192275318476525E-16   , Hex  2^-50   * -.8F1B60279E582A */
6724606Szliu /* lnovfl =  8.8029691931113053016E1     ; Hex  2^  7   *  .B00F33C7E22BDA */
6831812Szliu static long    mln2hix[] = { _0x(0f33,43b0), _0x(2bdb,c7e2)};
6931812Szliu static long    mln2lox[] = { _0x(1b60,a70f), _0x(582a,279e)};
7031812Szliu static long    lnovflx[] = { _0x(0f33,43b0), _0x(2bda,c7e2)};
7124606Szliu #define   mln2hi    (*(double*)mln2hix)
7224606Szliu #define   mln2lo    (*(double*)mln2lox)
7324606Szliu #define   lnovfl    (*(double*)lnovflx)
74*31853Szliu #else	/* defined(vax)||defined(tahoe) */
7526893Selefunt static double
7624606Szliu mln2hi =  7.0978271289338397310E2     , /*Hex  2^ 10   *  1.62E42FEFA39EF */
7724606Szliu mln2lo =  2.3747039373786107478E-14   , /*Hex  2^-45   *  1.ABC9E3B39803F */
7824606Szliu lnovfl =  7.0978271289338397310E2     ; /*Hex  2^  9   *  1.62E42FEFA39EF */
79*31853Szliu #endif	/* defined(vax)||defined(tahoe) */
8024606Szliu 
81*31853Szliu #if defined(vax)||defined(tahoe)
8224606Szliu static max = 126                      ;
83*31853Szliu #else	/* defined(vax)||defined(tahoe) */
8424606Szliu static max = 1023                     ;
85*31853Szliu #endif	/* defined(vax)||defined(tahoe) */
8624606Szliu 
8724606Szliu 
8824606Szliu double sinh(x)
8924606Szliu double x;
9024606Szliu {
9124606Szliu 	static double  one=1.0, half=1.0/2.0 ;
9224606Szliu 	double expm1(), t, scalb(), copysign(), sign;
93*31853Szliu #if !defined(vax)&&!defined(tahoe)
9424606Szliu 	if(x!=x) return(x);	/* x is NaN */
95*31853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
9624606Szliu 	sign=copysign(one,x);
9724606Szliu 	x=copysign(x,one);
9824606Szliu 	if(x<lnovfl)
9924606Szliu 	    {t=expm1(x); return(copysign((t+t/(one+t))*half,sign));}
10024606Szliu 
10124606Szliu 	else if(x <= lnovfl+0.7)
10224606Szliu 		/* subtract x by ln(2^(max+1)) and return 2^max*exp(x)
10324606Szliu 	    		to avoid unnecessary overflow */
10424606Szliu 	    return(copysign(scalb(one+expm1((x-mln2hi)-mln2lo),max),sign));
10524606Szliu 
10624606Szliu 	else  /* sinh(+-INF) = +-INF, sinh(+-big no.) overflow to +-INF */
10724606Szliu 	    return( expm1(x)*sign );
10824606Szliu }
109