t2ex/bsd_source/lib/libc/src_bsd/math/b_exp__D.c | bare source | permlink (0.02 seconds) |
1: /* $OpenBSD: b_exp__D.c,v 1.5 2009/10/27 23:59:29 deraadt Exp $ */ 2: /* 3: * Copyright (c) 1985, 1993 4: * The Regents of the University of California. All rights reserved. 5: * 6: * Redistribution and use in source and binary forms, with or without 7: * modification, are permitted provided that the following conditions 8: * are met: 9: * 1. Redistributions of source code must retain the above copyright 10: * notice, this list of conditions and the following disclaimer. 11: * 2. Redistributions in binary form must reproduce the above copyright 12: * notice, this list of conditions and the following disclaimer in the 13: * documentation and/or other materials provided with the distribution. 14: * 3. Neither the name of the University nor the names of its contributors 15: * may be used to endorse or promote products derived from this software 16: * without specific prior written permission. 17: * 18: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28: * SUCH DAMAGE. 29: */ 30: 31: /* EXP(X) 32: * RETURN THE EXPONENTIAL OF X 33: * DOUBLE PRECISION (IEEE 53 bits, VAX D FORMAT 56 BITS) 34: * CODED IN C BY K.C. NG, 1/19/85; 35: * REVISED BY K.C. NG on 2/6/85, 2/15/85, 3/7/85, 3/24/85, 4/16/85, 6/14/86. 36: * 37: * Required system supported functions: 38: * scalb(x,n) 39: * copysign(x,y) 40: * finite(x) 41: * 42: * Method: 43: * 1. Argument Reduction: given the input x, find r and integer k such 44: * that 45: * x = k*ln2 + r, |r| <= 0.5*ln2 . 46: * r will be represented as r := z+c for better accuracy. 47: * 48: * 2. Compute exp(r) by 49: * 50: * exp(r) = 1 + r + r*R1/(2-R1), 51: * where 52: * R1 = x - x^2*(p1+x^2*(p2+x^2*(p3+x^2*(p4+p5*x^2)))). 53: * 54: * 3. exp(x) = 2^k * exp(r) . 55: * 56: * Special cases: 57: * exp(INF) is INF, exp(NaN) is NaN; 58: * exp(-INF)= 0; 59: * for finite argument, only exp(0)=1 is exact. 60: * 61: * Accuracy: 62: * exp(x) returns the exponential of x nearly rounded. In a test run 63: * with 1,156,000 random arguments on a VAX, the maximum observed 64: * error was 0.869 ulps (units in the last place). 65: */ 66: 67: #include "math.h" 68: #include "math_private.h" 69: 70: static const double p1 = 0x1.555555555553ep-3; 71: static const double p2 = -0x1.6c16c16bebd93p-9; 72: static const double p3 = 0x1.1566aaf25de2cp-14; 73: static const double p4 = -0x1.bbd41c5d26bf1p-20; 74: static const double p5 = 0x1.6376972bea4d0p-25; 75: static const double ln2hi = 0x1.62e42fee00000p-1; 76: static const double ln2lo = 0x1.a39ef35793c76p-33; 77: static const double lnhuge = 0x1.6602b15b7ecf2p9; 78: static const double lntiny = -0x1.77af8ebeae354p9; 79: static const double invln2 = 0x1.71547652b82fep0; 80: 81: /* returns exp(r = x + c) for |c| < |x| with no overlap. */ 82: 83: double 84: __exp__D(double x, double c) 85: { 86: double z, hi, lo; 87: int k; 88: 89: if (isnan(x)) /* x is NaN */ 90: return(x); 91: if ( x <= lnhuge ) { 92: if ( x >= lntiny ) { 93: 94: /* argument reduction : x --> x - k*ln2 */ 95: z = invln2*x; 96: k = z + copysign(.5, x); 97: 98: /* express (x+c)-k*ln2 as hi-lo and let x=hi-lo rounded */ 99: 100: hi=(x-k*ln2hi); /* Exact. */ 101: x= hi - (lo = k*ln2lo-c); 102: /* return 2^k*[1+x+x*c/(2+c)] */ 103: z=x*x; 104: c= x - z*(p1+z*(p2+z*(p3+z*(p4+z*p5)))); 105: c = (x*c)/(2.0-c); 106: 107: return scalb(1.+(hi-(lo - c)), k); 108: } 109: /* end of x > lntiny */ 110: 111: else 112: /* exp(-big#) underflows to zero */ 113: if(finite(x)) return(scalb(1.0,-5000)); 114: 115: /* exp(-INF) is zero */ 116: else return(0.0); 117: } 118: /* end of x < lnhuge */ 119: 120: else 121: /* exp(INF) is INF, exp(+big#) overflows to INF */ 122: return( finite(x) ? scalb(1.0,5000) : x); 123: }