xref: /csrg-svn/lib/libm/common_source/acosh.c (revision 34124)
1*34124Sbostic /*
224586Szliu  * Copyright (c) 1985 Regents of the University of California.
3*34124Sbostic  * All rights reserved.
4*34124Sbostic  *
5*34124Sbostic  * Redistribution and use in source and binary forms are permitted
6*34124Sbostic  * provided that this notice is preserved and that due credit is given
7*34124Sbostic  * to the University of California at Berkeley. The name of the University
8*34124Sbostic  * may not be used to endorse or promote products derived from this
9*34124Sbostic  * software without specific prior written permission. This software
10*34124Sbostic  * is provided ``as is'' without express or implied warranty.
11*34124Sbostic  *
12*34124Sbostic  * All recipients should regard themselves as participants in an ongoing
13*34124Sbostic  * research project and hence should feel obligated to report their
14*34124Sbostic  * experiences (good or bad) with these elementary function codes, using
15*34124Sbostic  * the sendbug(8) program, to the authors.
1624586Szliu  */
1724586Szliu 
1824586Szliu #ifndef lint
19*34124Sbostic static char sccsid[] = "@(#)acosh.c	5.2 (Berkeley) 04/29/88";
20*34124Sbostic #endif /* not lint */
21*34124Sbostic 
2224586Szliu /* ACOSH(X)
2324586Szliu  * RETURN THE INVERSE HYPERBOLIC COSINE OF X
2424586Szliu  * DOUBLE PRECISION (VAX D FORMAT 56 BITS, IEEE DOUBLE 53 BITS)
2524586Szliu  * CODED IN C BY K.C. NG, 2/16/85;
2624586Szliu  * REVISED BY K.C. NG on 3/6/85, 3/24/85, 4/16/85, 8/17/85.
2724586Szliu  *
2824586Szliu  * Required system supported functions :
2924586Szliu  *	sqrt(x)
3024586Szliu  *
3124586Szliu  * Required kernel function:
3224586Szliu  *	log1p(x) 		...return log(1+x)
3324586Szliu  *
3424586Szliu  * Method :
3524586Szliu  *	Based on
3624586Szliu  *		acosh(x) = log [ x + sqrt(x*x-1) ]
3724586Szliu  *	we have
3824586Szliu  *		acosh(x) := log1p(x)+ln2,	if (x > 1.0E20); else
3924586Szliu  *		acosh(x) := log1p( sqrt(x-1) * (sqrt(x-1) + sqrt(x+1)) ) .
4024586Szliu  *	These formulae avoid the over/underflow complication.
4124586Szliu  *
4224586Szliu  * Special cases:
4324586Szliu  *	acosh(x) is NaN with signal if x<1.
4424586Szliu  *	acosh(NaN) is NaN without signal.
4524586Szliu  *
4624586Szliu  * Accuracy:
4724586Szliu  *	acosh(x) returns the exact inverse hyperbolic cosine of x nearly
4824586Szliu  *	rounded. In a test run with 512,000 random arguments on a VAX, the
4924586Szliu  *	maximum observed error was 3.30 ulps (units of the last place) at
5024586Szliu  *	x=1.0070493753568216 .
5124586Szliu  *
5224586Szliu  * Constants:
5324586Szliu  * The hexadecimal values are the intended ones for the following constants.
5424586Szliu  * The decimal values may be used, provided that the compiler will convert
5524586Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
5624586Szliu  * shown.
5724586Szliu  */
5824586Szliu 
5931853Szliu #if defined(vax)||defined(tahoe)	/* VAX D format */
6031853Szliu #ifdef vax
6131812Szliu #define _0x(A,B)	0x/**/A/**/B
6231853Szliu #else	/* vax */
6331812Szliu #define _0x(A,B)	0x/**/B/**/A
6431853Szliu #endif	/* vax */
6524586Szliu /* static double */
6624586Szliu /* ln2hi  =  6.9314718055829871446E-1    , Hex  2^  0   *  .B17217F7D00000 */
6724586Szliu /* ln2lo  =  1.6465949582897081279E-12   ; Hex  2^-39   *  .E7BCD5E4F1D9CC */
6831812Szliu static long     ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)};
6931812Szliu static long     ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)};
7024586Szliu #define    ln2hi    (*(double*)ln2hix)
7124586Szliu #define    ln2lo    (*(double*)ln2lox)
7231853Szliu #else	/* defined(vax)||defined(tahoe) */
7324586Szliu static double
7424586Szliu ln2hi  =  6.9314718036912381649E-1    , /*Hex  2^ -1   *  1.62E42FEE00000 */
7524586Szliu ln2lo  =  1.9082149292705877000E-10   ; /*Hex  2^-33   *  1.A39EF35793C76 */
7631853Szliu #endif	/* defined(vax)||defined(tahoe) */
7724586Szliu 
7824586Szliu double acosh(x)
7924586Szliu double x;
8024586Szliu {
8124586Szliu 	double log1p(),sqrt(),t,big=1.E20; /* big+1==big */
8224586Szliu 
8331853Szliu #if !defined(vax)&&!defined(tahoe)
8424586Szliu 	if(x!=x) return(x);	/* x is NaN */
8531853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
8624586Szliu 
8724586Szliu     /* return log1p(x) + log(2) if x is large */
8824586Szliu 	if(x>big) {t=log1p(x)+ln2lo; return(t+ln2hi);}
8924586Szliu 
9024586Szliu 	t=sqrt(x-1.0);
9124586Szliu 	return(log1p(t*(t+sqrt(x+1.0))));
9224586Szliu }
93