xref: /minix3/lib/libm/noieee_src/n_expm1.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*      $NetBSD: n_expm1.c,v 1.8 2013/11/24 18:50:58 martin Exp $ */
22fe8fb19SBen Gras /*
32fe8fb19SBen Gras  * Copyright (c) 1985, 1993
42fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
52fe8fb19SBen Gras  *
62fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
72fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
82fe8fb19SBen Gras  * are met:
92fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
102fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
112fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
122fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
132fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
142fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
152fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
162fe8fb19SBen Gras  *    without specific prior written permission.
172fe8fb19SBen Gras  *
182fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
192fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
222fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
232fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
242fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
252fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
262fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
272fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
282fe8fb19SBen Gras  * SUCH DAMAGE.
292fe8fb19SBen Gras  */
302fe8fb19SBen Gras 
312fe8fb19SBen Gras #ifndef lint
322fe8fb19SBen Gras #if 0
332fe8fb19SBen Gras static char sccsid[] = "@(#)expm1.c	8.1 (Berkeley) 6/4/93";
342fe8fb19SBen Gras #endif
352fe8fb19SBen Gras #endif /* not lint */
362fe8fb19SBen Gras 
372fe8fb19SBen Gras /* EXPM1(X)
382fe8fb19SBen Gras  * RETURN THE EXPONENTIAL OF X MINUS ONE
392fe8fb19SBen Gras  * DOUBLE PRECISION (IEEE 53 BITS, VAX D FORMAT 56 BITS)
402fe8fb19SBen Gras  * CODED IN C BY K.C. NG, 1/19/85;
412fe8fb19SBen Gras  * REVISED BY K.C. NG on 2/6/85, 3/7/85, 3/21/85, 4/16/85.
422fe8fb19SBen Gras  *
432fe8fb19SBen Gras  * Required system supported functions:
442fe8fb19SBen Gras  *	scalb(x,n)
452fe8fb19SBen Gras  *	copysign(x,y)
462fe8fb19SBen Gras  *	finite(x)
472fe8fb19SBen Gras  *
482fe8fb19SBen Gras  * Kernel function:
492fe8fb19SBen Gras  *	exp__E(x,c)
502fe8fb19SBen Gras  *
512fe8fb19SBen Gras  * Method:
522fe8fb19SBen Gras  *	1. Argument Reduction: given the input x, find r and integer k such
532fe8fb19SBen Gras  *	   that
542fe8fb19SBen Gras  *	                   x = k*ln2 + r,  |r| <= 0.5*ln2 .
552fe8fb19SBen Gras  *	   r will be represented as r := z+c for better accuracy.
562fe8fb19SBen Gras  *
572fe8fb19SBen Gras  *	2. Compute EXPM1(r)=exp(r)-1 by
582fe8fb19SBen Gras  *
592fe8fb19SBen Gras  *			EXPM1(r=z+c) := z + exp__E(z,c)
602fe8fb19SBen Gras  *
612fe8fb19SBen Gras  *	3. EXPM1(x) =  2^k * ( EXPM1(r) + 1-2^-k ).
622fe8fb19SBen Gras  *
632fe8fb19SBen Gras  * 	Remarks:
642fe8fb19SBen Gras  *	   1. When k=1 and z < -0.25, we use the following formula for
652fe8fb19SBen Gras  *	      better accuracy:
662fe8fb19SBen Gras  *			EXPM1(x) = 2 * ( (z+0.5) + exp__E(z,c) )
672fe8fb19SBen Gras  *	   2. To avoid rounding error in 1-2^-k where k is large, we use
682fe8fb19SBen Gras  *			EXPM1(x) = 2^k * { [z+(exp__E(z,c)-2^-k )] + 1 }
692fe8fb19SBen Gras  *	      when k>56.
702fe8fb19SBen Gras  *
712fe8fb19SBen Gras  * Special cases:
722fe8fb19SBen Gras  *	EXPM1(INF) is INF, EXPM1(NaN) is NaN;
732fe8fb19SBen Gras  *	EXPM1(-INF)= -1;
742fe8fb19SBen Gras  *	for finite argument, only EXPM1(0)=0 is exact.
752fe8fb19SBen Gras  *
762fe8fb19SBen Gras  * Accuracy:
772fe8fb19SBen Gras  *	EXPM1(x) returns the exact (exp(x)-1) nearly rounded. In a test run with
782fe8fb19SBen Gras  *	1,166,000 random arguments on a VAX, the maximum observed error was
792fe8fb19SBen Gras  *	.872 ulps (units of the last place).
802fe8fb19SBen Gras  *
812fe8fb19SBen Gras  * Constants:
822fe8fb19SBen Gras  * The hexadecimal values are the intended ones for the following constants.
832fe8fb19SBen Gras  * The decimal values may be used, provided that the compiler will convert
842fe8fb19SBen Gras  * from decimal to binary accurately enough to produce the hexadecimal values
852fe8fb19SBen Gras  * shown.
862fe8fb19SBen Gras  */
872fe8fb19SBen Gras 
882fe8fb19SBen Gras #define _LIBM_STATIC
892fe8fb19SBen Gras #include "mathimpl.h"
902fe8fb19SBen Gras 
912fe8fb19SBen Gras vc(ln2hi,  6.9314718055829871446E-1  ,7217,4031,0000,f7d0,   0, .B17217F7D00000)
922fe8fb19SBen Gras vc(ln2lo,  1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC)
932fe8fb19SBen Gras vc(lnhuge, 9.4961163736712506989E1   ,ec1d,43bd,9010,a73e,   7, .BDEC1DA73E9010)
942fe8fb19SBen Gras vc(invln2, 1.4426950408889634148E0   ,aa3b,40b8,17f1,295c,   1, .B8AA3B295C17F1)
952fe8fb19SBen Gras 
962fe8fb19SBen Gras ic(ln2hi,  6.9314718036912381649E-1,   -1, 1.62E42FEE00000)
972fe8fb19SBen Gras ic(ln2lo,  1.9082149292705877000E-10, -33, 1.A39EF35793C76)
982fe8fb19SBen Gras ic(lnhuge, 7.1602103751842355450E2,     9, 1.6602B15B7ECF2)
992fe8fb19SBen Gras ic(invln2, 1.4426950408889633870E0,     0, 1.71547652B82FE)
1002fe8fb19SBen Gras 
1012fe8fb19SBen Gras #ifdef vccast
1022fe8fb19SBen Gras #define	ln2hi	vccast(ln2hi)
1032fe8fb19SBen Gras #define	ln2lo	vccast(ln2lo)
1042fe8fb19SBen Gras #define	lnhuge	vccast(lnhuge)
1052fe8fb19SBen Gras #define	invln2	vccast(invln2)
1062fe8fb19SBen Gras #endif
1072fe8fb19SBen Gras 
1082fe8fb19SBen Gras #if defined(__vax__)||defined(tahoe)
1092fe8fb19SBen Gras #define PREC	56
1102fe8fb19SBen Gras #else	/* defined(__vax__)||defined(tahoe) */
1112fe8fb19SBen Gras #define PREC	53
1122fe8fb19SBen Gras #endif	/* defined(__vax__)||defined(tahoe) */
1132fe8fb19SBen Gras 
114*84d9c625SLionel Sambuc float
expm1f(float x)115*84d9c625SLionel Sambuc expm1f(float x)
116*84d9c625SLionel Sambuc {
117*84d9c625SLionel Sambuc 	return (float)expm1(x);
118*84d9c625SLionel Sambuc }
119*84d9c625SLionel Sambuc 
1202fe8fb19SBen Gras double
expm1(double x)1212fe8fb19SBen Gras expm1(double x)
1222fe8fb19SBen Gras {
1232fe8fb19SBen Gras 	static const double one=1.0, half=1.0/2.0;
1242fe8fb19SBen Gras 	double  z,hi,lo,c;
1252fe8fb19SBen Gras 	int k;
1262fe8fb19SBen Gras 
1272fe8fb19SBen Gras #if !defined(__vax__)&&!defined(tahoe)
1282fe8fb19SBen Gras 	if(x!=x) return(x);	/* x is NaN */
1292fe8fb19SBen Gras #endif	/* !defined(__vax__)&&!defined(tahoe) */
1302fe8fb19SBen Gras 
1312fe8fb19SBen Gras 	if( x <= lnhuge ) {
1322fe8fb19SBen Gras 		if( x >= -40.0 ) {
1332fe8fb19SBen Gras 
1342fe8fb19SBen Gras 		    /* argument reduction : x - k*ln2 */
1352fe8fb19SBen Gras 			k= invln2 *x+copysign(0.5,x);	/* k=NINT(x/ln2) */
1362fe8fb19SBen Gras 			hi=x-k*ln2hi ;
1372fe8fb19SBen Gras 			z=hi-(lo=k*ln2lo);
1382fe8fb19SBen Gras 			c=(hi-z)-lo;
1392fe8fb19SBen Gras 
1402fe8fb19SBen Gras 			if(k==0) return(z+__exp__E(z,c));
1412fe8fb19SBen Gras 			if(k==1)
1422fe8fb19SBen Gras 			    if(z< -0.25)
1432fe8fb19SBen Gras 				{x=z+half;x +=__exp__E(z,c); return(x+x);}
1442fe8fb19SBen Gras 			    else
1452fe8fb19SBen Gras 				{z+=__exp__E(z,c); x=half+z; return(x+x);}
1462fe8fb19SBen Gras 		    /* end of k=1 */
1472fe8fb19SBen Gras 
1482fe8fb19SBen Gras 			else {
1492fe8fb19SBen Gras 			    if(k<=PREC)
1502fe8fb19SBen Gras 			      { x=one-scalb(one,-k); z += __exp__E(z,c);}
1512fe8fb19SBen Gras 			    else if(k<100)
1522fe8fb19SBen Gras 			      { x = __exp__E(z,c)-scalb(one,-k); x+=z; z=one;}
1532fe8fb19SBen Gras 			    else
1542fe8fb19SBen Gras 			      { x = __exp__E(z,c)+z; z=one;}
1552fe8fb19SBen Gras 
1562fe8fb19SBen Gras 			    return (scalb(x+z,k));
1572fe8fb19SBen Gras 			}
1582fe8fb19SBen Gras 		}
1592fe8fb19SBen Gras 		/* end of x > lnunfl */
1602fe8fb19SBen Gras 
1612fe8fb19SBen Gras 		else
1622fe8fb19SBen Gras 		     /* expm1(-big#) rounded to -1 (inexact) */
1632fe8fb19SBen Gras 		     if(finite(x))
1642fe8fb19SBen Gras 			 { c=ln2hi+ln2lo; return(-one);} /* ??? -ragge */
1652fe8fb19SBen Gras 
1662fe8fb19SBen Gras 		     /* expm1(-INF) is -1 */
1672fe8fb19SBen Gras 		     else return(-one);
1682fe8fb19SBen Gras 	}
1692fe8fb19SBen Gras 	/* end of x < lnhuge */
1702fe8fb19SBen Gras 
1712fe8fb19SBen Gras 	else
1722fe8fb19SBen Gras 	/*  expm1(INF) is INF, expm1(+big#) overflows to INF */
1732fe8fb19SBen Gras 	    return( finite(x) ?  scalb(one,5000) : x);
1742fe8fb19SBen Gras }
175