|
Line 0
Link Here
|
|
|
1 |
/*- |
| 2 |
* Copyright (c) 2005, Steven G. Kargl |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice unmodified, this list of conditions, and the following |
| 10 |
* 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 |
* |
| 15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 20 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
/* |
| 28 |
* Compute the natural logrithm of x by decomposing x into its base 2 |
| 29 |
* representation. |
| 30 |
* |
| 31 |
* log(x) = log(f * 2**n) |
| 32 |
* = log(f) + n * log(2) |
| 33 |
* |
| 34 |
* where f = [1/2,1). |
| 35 |
* |
| 36 |
* Use a Taylor's series about f0 = 0.75 to compute log(f). |
| 37 |
* |
| 38 |
* log(f) = log(f0) + sum{(1/n) * (-1)**(n-1) * ((f - f0)/f0)**n}. |
| 39 |
*/ |
| 40 |
|
| 41 |
#include <math.h> |
| 42 |
#include <float.h> |
| 43 |
|
| 44 |
#define LOGL2 6.931471805599453094e-1L |
| 45 |
#define LOGLF0 -2.876820724517809274e-1L |
| 46 |
|
| 47 |
static long double zero = 0.0L; |
| 48 |
|
| 49 |
long double logl(long double x) { |
| 50 |
|
| 51 |
int i, n; |
| 52 |
long double f, t, val, oval; |
| 53 |
|
| 54 |
/* log(x) = sNaN if x < 0. */ |
| 55 |
if (x < 0.0L) |
| 56 |
return (x - x) / (x - x); |
| 57 |
|
| 58 |
/* log(+Inf) = +Inf */ |
| 59 |
if (isinf(x)) |
| 60 |
return x*x+x; |
| 61 |
|
| 62 |
/* log(+-0) = -Inf with signal */ |
| 63 |
if (x == 0.0L) |
| 64 |
return (- 1.0L / zero); |
| 65 |
|
| 66 |
/* log(NaN) = NaN with signal */ |
| 67 |
if (isnan(x)) |
| 68 |
return (x+x); |
| 69 |
|
| 70 |
/* |
| 71 |
* Special case for values near log(1). Use the series expansion |
| 72 |
* for log(1+x) = x - (1/2) * x**2 + (1/3) * x**3 + ... |
| 73 |
*/ |
| 74 |
if (0.95L < x && x < 1.05L) { |
| 75 |
|
| 76 |
f = t = x - 1.0L; |
| 77 |
oval = val = 0.L; |
| 78 |
|
| 79 |
for (i = 1; ; i++) { |
| 80 |
val += t / (long double) i; |
| 81 |
t *= -f; |
| 82 |
if (fabsl(oval - val) < LDBL_EPSILON) break; |
| 83 |
oval = val; |
| 84 |
} |
| 85 |
|
| 86 |
return (val); |
| 87 |
} |
| 88 |
|
| 89 |
f = frexpl(x, &n); |
| 90 |
f = t = (f - 0.75L) / 0.75L; |
| 91 |
|
| 92 |
oval = val = LOGLF0; |
| 93 |
for (i = 1; ; i++) { |
| 94 |
val += t / (long double) i; |
| 95 |
t *= -f; |
| 96 |
if (fabsl(oval-val) < LDBL_EPSILON) break; |
| 97 |
oval = val; |
| 98 |
} |
| 99 |
|
| 100 |
return (val + n * LOGL2); |
| 101 |
|
| 102 |
} |