xref: /csrg-svn/lib/libm/common_source/sinh.c (revision 34126)
1*34126Sbostic /*
224606Szliu  * Copyright (c) 1985 Regents of the University of California.
3*34126Sbostic  * All rights reserved.
4*34126Sbostic  *
5*34126Sbostic  * Redistribution and use in source and binary forms are permitted
6*34126Sbostic  * provided that this notice is preserved and that due credit is given
7*34126Sbostic  * to the University of California at Berkeley. The name of the University
8*34126Sbostic  * may not be used to endorse or promote products derived from this
9*34126Sbostic  * software without specific prior written permission. This software
10*34126Sbostic  * is provided ``as is'' without express or implied warranty.
11*34126Sbostic  *
12*34126Sbostic  * All recipients should regard themselves as participants in an ongoing
13*34126Sbostic  * research project and hence should feel obligated to report their
14*34126Sbostic  * experiences (good or bad) with these elementary function codes, using
15*34126Sbostic  * the sendbug(8) program, to the authors.
1624606Szliu  */
1724606Szliu 
1824606Szliu #ifndef lint
19*34126Sbostic static char sccsid[] = "@(#)sinh.c	5.2 (Berkeley) 04/29/88";
20*34126Sbostic #endif /* not lint */
2124606Szliu 
2224606Szliu /* SINH(X)
2324606Szliu  * RETURN THE HYPERBOLIC SINE OF X
2424606Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
2524606Szliu  * CODED IN C BY K.C. NG, 1/8/85;
2624606Szliu  * REVISED BY K.C. NG on 2/8/85, 3/7/85, 3/24/85, 4/16/85.
2724606Szliu  *
2824606Szliu  * Required system supported functions :
2924606Szliu  *	copysign(x,y)
3024606Szliu  *	scalb(x,N)
3124606Szliu  *
3224606Szliu  * Required kernel functions:
3324606Szliu  *	expm1(x)	...return exp(x)-1
3424606Szliu  *
3524606Szliu  * Method :
3624606Szliu  *	1. reduce x to non-negative by sinh(-x) = - sinh(x).
3724606Szliu  *	2.
3824606Szliu  *
3924606Szliu  *	                                      expm1(x) + expm1(x)/(expm1(x)+1)
4024606Szliu  *	    0 <= x <= lnovfl     : sinh(x) := --------------------------------
4124606Szliu  *			       		                      2
4224606Szliu  *     lnovfl <= x <= lnovfl+ln2 : sinh(x) := expm1(x)/2 (avoid overflow)
4324606Szliu  * lnovfl+ln2 <  x <  INF        :  overflow to INF
4424606Szliu  *
4524606Szliu  *
4624606Szliu  * Special cases:
4724606Szliu  *	sinh(x) is x if x is +INF, -INF, or NaN.
4824606Szliu  *	only sinh(0)=0 is exact for finite argument.
4924606Szliu  *
5024606Szliu  * Accuracy:
5124606Szliu  *	sinh(x) returns the exact hyperbolic sine of x nearly rounded. In
5224606Szliu  *	a test run with 1,024,000 random arguments on a VAX, the maximum
5324606Szliu  *	observed error was 1.93 ulps (units in the last place).
5424606Szliu  *
5524606Szliu  * Constants:
5624606Szliu  * The hexadecimal values are the intended ones for the following constants.
5724606Szliu  * The decimal values may be used, provided that the compiler will convert
5824606Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
5924606Szliu  * shown.
6024606Szliu  */
6131853Szliu #if defined(vax)||defined(tahoe)
6231853Szliu #ifdef vax
6331812Szliu #define _0x(A,B)	0x/**/A/**/B
6431853Szliu #else	/* vax */
6531812Szliu #define _0x(A,B)	0x/**/B/**/A
6631853Szliu #endif	/* vax */
6726893Selefunt /* static double */
6824606Szliu /* mln2hi =  8.8029691931113054792E1     , Hex  2^  7   *  .B00F33C7E22BDB */
6924606Szliu /* mln2lo = -4.9650192275318476525E-16   , Hex  2^-50   * -.8F1B60279E582A */
7024606Szliu /* lnovfl =  8.8029691931113053016E1     ; Hex  2^  7   *  .B00F33C7E22BDA */
7131812Szliu static long    mln2hix[] = { _0x(0f33,43b0), _0x(2bdb,c7e2)};
7231812Szliu static long    mln2lox[] = { _0x(1b60,a70f), _0x(582a,279e)};
7331812Szliu static long    lnovflx[] = { _0x(0f33,43b0), _0x(2bda,c7e2)};
7424606Szliu #define   mln2hi    (*(double*)mln2hix)
7524606Szliu #define   mln2lo    (*(double*)mln2lox)
7624606Szliu #define   lnovfl    (*(double*)lnovflx)
7731853Szliu #else	/* defined(vax)||defined(tahoe) */
7826893Selefunt static double
7924606Szliu mln2hi =  7.0978271289338397310E2     , /*Hex  2^ 10   *  1.62E42FEFA39EF */
8024606Szliu mln2lo =  2.3747039373786107478E-14   , /*Hex  2^-45   *  1.ABC9E3B39803F */
8124606Szliu lnovfl =  7.0978271289338397310E2     ; /*Hex  2^  9   *  1.62E42FEFA39EF */
8231853Szliu #endif	/* defined(vax)||defined(tahoe) */
8324606Szliu 
8431853Szliu #if defined(vax)||defined(tahoe)
8524606Szliu static max = 126                      ;
8631853Szliu #else	/* defined(vax)||defined(tahoe) */
8724606Szliu static max = 1023                     ;
8831853Szliu #endif	/* defined(vax)||defined(tahoe) */
8924606Szliu 
9024606Szliu 
9124606Szliu double sinh(x)
9224606Szliu double x;
9324606Szliu {
9424606Szliu 	static double  one=1.0, half=1.0/2.0 ;
9524606Szliu 	double expm1(), t, scalb(), copysign(), sign;
9631853Szliu #if !defined(vax)&&!defined(tahoe)
9724606Szliu 	if(x!=x) return(x);	/* x is NaN */
9831853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
9924606Szliu 	sign=copysign(one,x);
10024606Szliu 	x=copysign(x,one);
10124606Szliu 	if(x<lnovfl)
10224606Szliu 	    {t=expm1(x); return(copysign((t+t/(one+t))*half,sign));}
10324606Szliu 
10424606Szliu 	else if(x <= lnovfl+0.7)
10524606Szliu 		/* subtract x by ln(2^(max+1)) and return 2^max*exp(x)
10624606Szliu 	    		to avoid unnecessary overflow */
10724606Szliu 	    return(copysign(scalb(one+expm1((x-mln2hi)-mln2lo),max),sign));
10824606Szliu 
10924606Szliu 	else  /* sinh(+-INF) = +-INF, sinh(+-big no.) overflow to +-INF */
11024606Szliu 	    return( expm1(x)*sign );
11124606Szliu }
112