xref: /csrg-svn/lib/libm/common_source/acosh.c (revision 35679)
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
634931Sbostic  * provided that the above copyright notice and this paragraph are
734931Sbostic  * duplicated in all such forms and that any documentation,
834931Sbostic  * advertising materials, and other materials related to such
934931Sbostic  * distribution and use acknowledge that the software was developed
1034931Sbostic  * by the University of California, Berkeley.  The name of the
1134931Sbostic  * University may not be used to endorse or promote products derived
1234931Sbostic  * from this software without specific prior written permission.
1334931Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434931Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534931Sbostic  * 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*35679Sbostic static char sccsid[] = "@(#)acosh.c	5.4 (Berkeley) 09/22/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 
64*35679Sbostic #include "mathimpl.h"
6524586Szliu 
66*35679Sbostic vc(ln2hi, 6.9314718055829871446E-1  ,7217,4031,0000,f7d0,   0, .B17217F7D00000)
67*35679Sbostic vc(ln2lo, 1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC)
68*35679Sbostic 
69*35679Sbostic ic(ln2hi, 6.9314718036912381649E-1,  -1, 1.62E42FEE00000)
70*35679Sbostic ic(ln2lo, 1.9082149292705877000E-10,-33, 1.A39EF35793C76)
71*35679Sbostic 
72*35679Sbostic #ifdef vccast
73*35679Sbostic #define    ln2hi    vccast(ln2hi)
74*35679Sbostic #define    ln2lo    vccast(ln2lo)
75*35679Sbostic #endif
76*35679Sbostic 
7724586Szliu double acosh(x)
7824586Szliu double x;
7924586Szliu {
80*35679Sbostic 	double t,big=1.E20; /* big+1==big */
8124586Szliu 
8231853Szliu #if !defined(vax)&&!defined(tahoe)
8324586Szliu 	if(x!=x) return(x);	/* x is NaN */
8431853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
8524586Szliu 
8624586Szliu     /* return log1p(x) + log(2) if x is large */
8724586Szliu 	if(x>big) {t=log1p(x)+ln2lo; return(t+ln2hi);}
8824586Szliu 
8924586Szliu 	t=sqrt(x-1.0);
9024586Szliu 	return(log1p(t*(t+sqrt(x+1.0))));
9124586Szliu }
92