1 /* $OpenBSD: e_log10l.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 /* log10l.c 20 * 21 * Common logarithm, long double precision 22 * 23 * 24 * 25 * SYNOPSIS: 26 * 27 * long double x, y, log10l(); 28 * 29 * y = log10l( x ); 30 * 31 * 32 * 33 * DESCRIPTION: 34 * 35 * Returns the base 10 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 30000 9.0e-20 2.6e-20 54 * IEEE exp(+-10000) 30000 6.0e-20 2.3e-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 MINLOG 63 * log domain: x < 0; returns MINLOG 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 = 6.2e-22 71 */ 72 static long double P[] = { 73 4.9962495940332550844739E-1L, 74 1.0767376367209449010438E1L, 75 7.7671073698359539859595E1L, 76 2.5620629828144409632571E2L, 77 4.2401812743503691187826E2L, 78 3.4258224542413922935104E2L, 79 1.0747524399916215149070E2L, 80 }; 81 static long double Q[] = { 82 /* 1.0000000000000000000000E0,*/ 83 2.3479774160285863271658E1L, 84 1.9444210022760132894510E2L, 85 7.7952888181207260646090E2L, 86 1.6911722418503949084863E3L, 87 2.0307734695595183428202E3L, 88 1.2695660352705325274404E3L, 89 3.2242573199748645407652E2L, 90 }; 91 92 /* Coefficients for log(x) = z + z^3 P(z^2)/Q(z^2), 93 * where z = 2(x-1)/(x+1) 94 * 1/sqrt(2) <= x < sqrt(2) 95 * Theoretical peak relative error = 6.16e-22 96 */ 97 98 static long double R[4] = { 99 1.9757429581415468984296E-3L, 100 -7.1990767473014147232598E-1L, 101 1.0777257190312272158094E1L, 102 -3.5717684488096787370998E1L, 103 }; 104 static long double S[4] = { 105 /* 1.00000000000000000000E0L,*/ 106 -2.6201045551331104417768E1L, 107 1.9361891836232102174846E2L, 108 -4.2861221385716144629696E2L, 109 }; 110 /* log10(2) */ 111 #define L102A 0.3125L 112 #define L102B -1.1470004336018804786261e-2L 113 /* log10(e) */ 114 #define L10EA 0.5L 115 #define L10EB -6.5705518096748172348871e-2L 116 117 #define SQRTH 0.70710678118654752440L 118 119 extern long double __polevll(long double, void *, int); 120 extern long double __p1evll(long double, void *, int); 121 122 long double 123 log10l(long double x) 124 { 125 long double y; 126 volatile long double z; 127 int e; 128 129 if( isnan(x) ) 130 return(x); 131 /* Test for domain */ 132 if( x <= 0.0L ) 133 { 134 if( x == 0.0L ) 135 return (-1.0L / (x - x)); 136 else 137 return (x - x) / (x - x); 138 } 139 if( x == INFINITY ) 140 return(INFINITY); 141 /* separate mantissa from exponent */ 142 143 /* Note, frexp is used so that denormal numbers 144 * will be handled properly. 145 */ 146 x = frexpl( x, &e ); 147 148 149 /* logarithm using log(x) = z + z**3 P(z)/Q(z), 150 * where z = 2(x-1)/x+1) 151 */ 152 if( (e > 2) || (e < -2) ) 153 { 154 if( x < SQRTH ) 155 { /* 2( 2x-1 )/( 2x+1 ) */ 156 e -= 1; 157 z = x - 0.5L; 158 y = 0.5L * z + 0.5L; 159 } 160 else 161 { /* 2 (x-1)/(x+1) */ 162 z = x - 0.5L; 163 z -= 0.5L; 164 y = 0.5L * x + 0.5L; 165 } 166 x = z / y; 167 z = x*x; 168 y = x * ( z * __polevll( z, R, 3 ) / __p1evll( z, S, 3 ) ); 169 goto done; 170 } 171 172 173 /* logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) */ 174 175 if( x < SQRTH ) 176 { 177 e -= 1; 178 x = ldexpl( x, 1 ) - 1.0L; /* 2x - 1 */ 179 } 180 else 181 { 182 x = x - 1.0L; 183 } 184 z = x*x; 185 y = x * ( z * __polevll( x, P, 6 ) / __p1evll( x, Q, 7 ) ); 186 y = y - ldexpl( z, -1 ); /* -0.5x^2 + ... */ 187 188 done: 189 190 /* Multiply log of fraction by log10(e) 191 * and base 2 exponent by log10(2). 192 * 193 * ***CAUTION*** 194 * 195 * This sequence of operations is critical and it may 196 * be horribly defeated by some compiler optimizers. 197 */ 198 z = y * (L10EB); 199 z += x * (L10EB); 200 z += e * (L102B); 201 z += y * (L10EA); 202 z += x * (L10EA); 203 z += e * (L102A); 204 205 return( z ); 206 } 207