1 /* $OpenBSD: e_logl.c,v 1.1 2011/07/06 00:02:42 martynas Exp $ */ 2 3 /* 4 * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /* logl.c 20 * 21 * Natural logarithm, long double precision 22 * 23 * 24 * 25 * SYNOPSIS: 26 * 27 * long double x, y, logl(); 28 * 29 * y = logl( x ); 30 * 31 * 32 * 33 * DESCRIPTION: 34 * 35 * Returns the base e (2.718...) logarithm of x. 36 * 37 * The argument is separated into its exponent and fractional 38 * parts. If the exponent is between -1 and +1, the logarithm 39 * of the fraction is approximated by 40 * 41 * log(1+x) = x - 0.5 x**2 + x**3 P(x)/Q(x). 42 * 43 * Otherwise, setting z = 2(x-1)/x+1), 44 * 45 * log(x) = z + z**3 P(z)/Q(z). 46 * 47 * 48 * 49 * ACCURACY: 50 * 51 * Relative error: 52 * arithmetic domain # trials peak rms 53 * IEEE 0.5, 2.0 150000 8.71e-20 2.75e-20 54 * IEEE exp(+-10000) 100000 5.39e-20 2.34e-20 55 * 56 * In the tests over the interval exp(+-10000), the logarithms 57 * of the random arguments were uniformly distributed over 58 * [-10000, +10000]. 59 * 60 * ERROR MESSAGES: 61 * 62 * log singularity: x = 0; returns -INFINITY 63 * log domain: x < 0; returns NAN 64 */ 65 66 #include <math.h> 67 68 /* Coefficients for log(1+x) = x - x**2/2 + x**3 P(x)/Q(x) 69 * 1/sqrt(2) <= x < sqrt(2) 70 * Theoretical peak relative error = 2.32e-20 71 */ 72 static long double P[] = { 73 4.5270000862445199635215E-5L, 74 4.9854102823193375972212E-1L, 75 6.5787325942061044846969E0L, 76 2.9911919328553073277375E1L, 77 6.0949667980987787057556E1L, 78 5.7112963590585538103336E1L, 79 2.0039553499201281259648E1L, 80 }; 81 static long double Q[] = { 82 /* 1.0000000000000000000000E0,*/ 83 1.5062909083469192043167E1L, 84 8.3047565967967209469434E1L, 85 2.2176239823732856465394E2L, 86 3.0909872225312059774938E2L, 87 2.1642788614495947685003E2L, 88 6.0118660497603843919306E1L, 89 }; 90 91 /* Coefficients for log(x) = z + z^3 P(z^2)/Q(z^2), 92 * where z = 2(x-1)/(x+1) 93 * 1/sqrt(2) <= x < sqrt(2) 94 * Theoretical peak relative error = 6.16e-22 95 */ 96 97 static long double R[4] = { 98 1.9757429581415468984296E-3L, 99 -7.1990767473014147232598E-1L, 100 1.0777257190312272158094E1L, 101 -3.5717684488096787370998E1L, 102 }; 103 static long double S[4] = { 104 /* 1.00000000000000000000E0L,*/ 105 -2.6201045551331104417768E1L, 106 1.9361891836232102174846E2L, 107 -4.2861221385716144629696E2L, 108 }; 109 static long double C1 = 6.9314575195312500000000E-1L; 110 static long double C2 = 1.4286068203094172321215E-6L; 111 112 #define SQRTH 0.70710678118654752440L 113 114 extern long double __polevll(long double, void *, int); 115 extern long double __p1evll(long double, void *, int); 116 117 long double 118 logl(long double x) 119 { 120 long double y, z; 121 int e; 122 123 if( isnan(x) ) 124 return(x); 125 if( x == INFINITY ) 126 return(x); 127 /* Test for domain */ 128 if( x <= 0.0L ) 129 { 130 if( x == 0.0L ) 131 return( -INFINITY ); 132 else 133 return( NAN ); 134 } 135 136 /* separate mantissa from exponent */ 137 138 /* Note, frexp is used so that denormal numbers 139 * will be handled properly. 140 */ 141 x = frexpl( x, &e ); 142 143 /* logarithm using log(x) = z + z**3 P(z)/Q(z), 144 * where z = 2(x-1)/x+1) 145 */ 146 if( (e > 2) || (e < -2) ) 147 { 148 if( x < SQRTH ) 149 { /* 2( 2x-1 )/( 2x+1 ) */ 150 e -= 1; 151 z = x - 0.5L; 152 y = 0.5L * z + 0.5L; 153 } 154 else 155 { /* 2 (x-1)/(x+1) */ 156 z = x - 0.5L; 157 z -= 0.5L; 158 y = 0.5L * x + 0.5L; 159 } 160 x = z / y; 161 z = x*x; 162 z = x * ( z * __polevll( z, R, 3 ) / __p1evll( z, S, 3 ) ); 163 z = z + e * C2; 164 z = z + x; 165 z = z + e * C1; 166 return( z ); 167 } 168 169 170 /* logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) */ 171 172 if( x < SQRTH ) 173 { 174 e -= 1; 175 x = ldexpl( x, 1 ) - 1.0L; /* 2x - 1 */ 176 } 177 else 178 { 179 x = x - 1.0L; 180 } 181 z = x*x; 182 y = x * ( z * __polevll( x, P, 6 ) / __p1evll( x, Q, 6 ) ); 183 y = y + e * C2; 184 z = y - ldexpl( z, -1 ); /* y - 0.5 * z */ 185 /* Note, the sum of above terms does not exceed x/4, 186 * so it contributes at most about 1/4 lsb to the error. 187 */ 188 z = z + x; 189 z = z + e * C1; /* This sum has an error of 1/2 lsb. */ 190 return( z ); 191 } 192