log.c 1.24 KB
/*
=============================================================================
        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;
}