log.c
1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
=============================================================================
Copyright (C) 1997-1999 NINTENDO Co.,Ltd.
$RCSfile: log.c,v $
$Revision: 1.1.1.1 $
$Date: 2002/10/30 02:07:09 $
=============================================================================
関数名:log
-----------------------------------------------------------------------------
書式: #include <math.h>
double log(double x);
引数: x
戻り値:引数 x の自然対数。
説明: 引数 x の自然対数を計算する。
log(x) = 2*((x-1)/(x+1)) + 2/3*((x-1)/(x+1))^3
+ (2/(2k+1))((x-1)/(x+1))^(2k+1) (k=0,1,2....)
log(2^k * x) = k * log(2) + log(x)
-----------------------------------------------------------------------------
*/
#include "math.h"
double log(double x)
{
int n;
double x0, ak, z, zbk;
if (x < 0) { return DVAL_ZERO; } /* error */
if (x == 0) { return DVAL_ZERO; } /* error */
z = 0;
while(x>=2) { x/=2; z+=DVAL_LOG2; }
while(x< 1) { x*=2; z-=DVAL_LOG2; }
n = 1;
x0 = (x-1)/(x+1);
ak = 2 * x0;
x0 = x0 * x0;
do {
zbk = z;
z += ak/n;
ak *= x0;
n += 2;
} while (z != zbk);
return z;
}