xref: /csrg-svn/lib/libm/common_source/acosh.c (revision 34931)
134124Sbostic /*
224586Szliu  * 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.
2124586Szliu  */
2224586Szliu 
2324586Szliu #ifndef lint
24*34931Sbostic static char sccsid[] = "@(#)acosh.c	5.3 (Berkeley) 06/30/88";
2534124Sbostic #endif /* not lint */
2634124Sbostic 
2724586Szliu /* ACOSH(X)
2824586Szliu  * RETURN THE INVERSE HYPERBOLIC COSINE OF X
2924586Szliu  * DOUBLE PRECISION (VAX D FORMAT 56 BITS, IEEE DOUBLE 53 BITS)
3024586Szliu  * CODED IN C BY K.C. NG, 2/16/85;
3124586Szliu  * REVISED BY K.C. NG on 3/6/85, 3/24/85, 4/16/85, 8/17/85.
3224586Szliu  *
3324586Szliu  * Required system supported functions :
3424586Szliu  *	sqrt(x)
3524586Szliu  *
3624586Szliu  * Required kernel function:
3724586Szliu  *	log1p(x) 		...return log(1+x)
3824586Szliu  *
3924586Szliu  * Method :
4024586Szliu  *	Based on
4124586Szliu  *		acosh(x) = log [ x + sqrt(x*x-1) ]
4224586Szliu  *	we have
4324586Szliu  *		acosh(x) := log1p(x)+ln2,	if (x > 1.0E20); else
4424586Szliu  *		acosh(x) := log1p( sqrt(x-1) * (sqrt(x-1) + sqrt(x+1)) ) .
4524586Szliu  *	These formulae avoid the over/underflow complication.
4624586Szliu  *
4724586Szliu  * Special cases:
4824586Szliu  *	acosh(x) is NaN with signal if x<1.
4924586Szliu  *	acosh(NaN) is NaN without signal.
5024586Szliu  *
5124586Szliu  * Accuracy:
5224586Szliu  *	acosh(x) returns the exact inverse hyperbolic cosine of x nearly
5324586Szliu  *	rounded. In a test run with 512,000 random arguments on a VAX, the
5424586Szliu  *	maximum observed error was 3.30 ulps (units of the last place) at
5524586Szliu  *	x=1.0070493753568216 .
5624586Szliu  *
5724586Szliu  * Constants:
5824586Szliu  * The hexadecimal values are the intended ones for the following constants.
5924586Szliu  * The decimal values may be used, provided that the compiler will convert
6024586Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
6124586Szliu  * shown.
6224586Szliu  */
6324586Szliu 
6431853Szliu #if defined(vax)||defined(tahoe)	/* VAX D format */
6531853Szliu #ifdef vax
6631812Szliu #define _0x(A,B)	0x/**/A/**/B
6731853Szliu #else	/* vax */
6831812Szliu #define _0x(A,B)	0x/**/B/**/A
6931853Szliu #endif	/* vax */
7024586Szliu /* static double */
7124586Szliu /* ln2hi  =  6.9314718055829871446E-1    , Hex  2^  0   *  .B17217F7D00000 */
7224586Szliu /* ln2lo  =  1.6465949582897081279E-12   ; Hex  2^-39   *  .E7BCD5E4F1D9CC */
7331812Szliu static long     ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)};
7431812Szliu static long     ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)};
7524586Szliu #define    ln2hi    (*(double*)ln2hix)
7624586Szliu #define    ln2lo    (*(double*)ln2lox)
7731853Szliu #else	/* defined(vax)||defined(tahoe) */
7824586Szliu static double
7924586Szliu ln2hi  =  6.9314718036912381649E-1    , /*Hex  2^ -1   *  1.62E42FEE00000 */
8024586Szliu ln2lo  =  1.9082149292705877000E-10   ; /*Hex  2^-33   *  1.A39EF35793C76 */
8131853Szliu #endif	/* defined(vax)||defined(tahoe) */
8224586Szliu 
8324586Szliu double acosh(x)
8424586Szliu double x;
8524586Szliu {
8624586Szliu 	double log1p(),sqrt(),t,big=1.E20; /* big+1==big */
8724586Szliu 
8831853Szliu #if !defined(vax)&&!defined(tahoe)
8924586Szliu 	if(x!=x) return(x);	/* x is NaN */
9031853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
9124586Szliu 
9224586Szliu     /* return log1p(x) + log(2) if x is large */
9324586Szliu 	if(x>big) {t=log1p(x)+ln2lo; return(t+ln2hi);}
9424586Szliu 
9524586Szliu 	t=sqrt(x-1.0);
9624586Szliu 	return(log1p(t*(t+sqrt(x+1.0))));
9724586Szliu }
98