xref: /csrg-svn/lib/libm/common_source/asinh.c (revision 34931)
134124Sbostic /*
224588Szliu  * Copyright (c) 1985 Regents of the University of California.
334124Sbostic  * All rights reserved.
434124Sbostic  *
534124Sbostic  * Redistribution and use in source and binary forms are permitted
6*34931Sbostic  * provided that the above copyright notice and this paragraph are
7*34931Sbostic  * duplicated in all such forms and that any documentation,
8*34931Sbostic  * advertising materials, and other materials related to such
9*34931Sbostic  * distribution and use acknowledge that the software was developed
10*34931Sbostic  * by the University of California, Berkeley.  The name of the
11*34931Sbostic  * University may not be used to endorse or promote products derived
12*34931Sbostic  * from this software without specific prior written permission.
13*34931Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34931Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34931Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1634124Sbostic  *
1734124Sbostic  * All recipients should regard themselves as participants in an ongoing
1834124Sbostic  * research project and hence should feel obligated to report their
1934124Sbostic  * experiences (good or bad) with these elementary function codes, using
2034124Sbostic  * the sendbug(8) program, to the authors.
2124588Szliu  */
2224588Szliu 
2324588Szliu #ifndef lint
24*34931Sbostic static char sccsid[] = "@(#)asinh.c	5.3 (Berkeley) 06/30/88";
2534124Sbostic #endif /* not lint */
2624588Szliu 
2724588Szliu /* ASINH(X)
2824588Szliu  * RETURN THE INVERSE HYPERBOLIC SINE OF X
2924588Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
3024588Szliu  * CODED IN C BY K.C. NG, 2/16/85;
3124588Szliu  * REVISED BY K.C. NG on 3/7/85, 3/24/85, 4/16/85.
3224588Szliu  *
3324588Szliu  * Required system supported functions :
3424588Szliu  *	copysign(x,y)
3524588Szliu  *	sqrt(x)
3624588Szliu  *
3724588Szliu  * Required kernel function:
3824588Szliu  *	log1p(x) 		...return log(1+x)
3924588Szliu  *
4024588Szliu  * Method :
4124588Szliu  *	Based on
4224588Szliu  *		asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ]
4324588Szliu  *	we have
4424588Szliu  *	asinh(x) := x  if  1+x*x=1,
4524588Szliu  *		 := sign(x)*(log1p(x)+ln2))	 if sqrt(1+x*x)=x, else
4624588Szliu  *		 := sign(x)*log1p(|x| + |x|/(1/|x| + sqrt(1+(1/|x|)^2)) )
4724588Szliu  *
4824588Szliu  * Accuracy:
4924588Szliu  *	asinh(x) returns the exact inverse hyperbolic sine of x nearly rounded.
5024588Szliu  *	In a test run with 52,000 random arguments on a VAX, the maximum
5124588Szliu  *	observed error was 1.58 ulps (units in the last place).
5224588Szliu  *
5324588Szliu  * Constants:
5424588Szliu  * The hexadecimal values are the intended ones for the following constants.
5524588Szliu  * The decimal values may be used, provided that the compiler will convert
5624588Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
5724588Szliu  * shown.
5824588Szliu  */
5924588Szliu 
6031853Szliu #if defined(vax)||defined(tahoe)	/* VAX/TAHOE D format */
6131853Szliu #ifdef vax
6231812Szliu #define _0x(A,B)	0x/**/A/**/B
6331853Szliu #else	/* vax */
6431812Szliu #define _0x(A,B)	0x/**/B/**/A
6531853Szliu #endif	/* vax */
6624588Szliu /* static double */
6724588Szliu /* ln2hi  =  6.9314718055829871446E-1    , Hex  2^  0   *  .B17217F7D00000 */
6824588Szliu /* ln2lo  =  1.6465949582897081279E-12   ; Hex  2^-39   *  .E7BCD5E4F1D9CC */
6931812Szliu static long     ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)};
7031812Szliu static long     ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)};
7124588Szliu #define    ln2hi    (*(double*)ln2hix)
7224588Szliu #define    ln2lo    (*(double*)ln2lox)
7331853Szliu #else	/* defined(vax)||defined(tahoe) */
7424588Szliu static double
7524588Szliu ln2hi  =  6.9314718036912381649E-1    , /*Hex  2^ -1   *  1.62E42FEE00000 */
7624588Szliu ln2lo  =  1.9082149292705877000E-10   ; /*Hex  2^-33   *  1.A39EF35793C76 */
7731853Szliu #endif	/* defined(vax)||defined(tahoe) */
7824588Szliu 
7924588Szliu double asinh(x)
8024588Szliu double x;
8124588Szliu {
8224588Szliu 	double copysign(),log1p(),sqrt(),t,s;
8324588Szliu 	static double small=1.0E-10,	/* fl(1+small*small) == 1 */
8424588Szliu 		      big  =1.0E20,	/* fl(1+big) == big */
8524588Szliu 		      one  =1.0   ;
8624588Szliu 
8731853Szliu #if !defined(vax)&&!defined(tahoe)
8824588Szliu 	if(x!=x) return(x);	/* x is NaN */
8931853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
9024588Szliu 	if((t=copysign(x,one))>small)
9124588Szliu 	    if(t<big) {
9224588Szliu 	     	s=one/t; return(copysign(log1p(t+t/(s+sqrt(one+s*s))),x)); }
9324588Szliu 	    else	/* if |x| > big */
9424588Szliu 		{s=log1p(t)+ln2lo; return(copysign(s+ln2hi,x));}
9524588Szliu 	else	/* if |x| < small */
9624588Szliu 	    return(x);
9724588Szliu }
98