pow.c 1.14 KB
/*
=============================================================================
        Copyright (C) 1997-1999 NINTENDO Co.,Ltd.
        
        $RCSfile: pow.c,v $
        $Revision: 1.1.1.1 $
        $Date: 2002/10/30 02:07:09 $
=============================================================================
関数名:pow
-----------------------------------------------------------------------------
書式:  #include <math.h>
        double pow(double x, double y);
引数:  x 底
        y 指数
戻り値:x の y 乗
説明:  x の y 乗を求める。
-----------------------------------------------------------------------------
*/
#include    "math.h"

double pow(double x, double y)
{
    int a;
    double b;

    a = (int)y;

    if (x != 0.0 && y == 0.0)   return  1.0;
    if (x == 0.0)               return  0.0;
    if ((x == 0.0 && y == 0.0) || (x < 0 && y != (double)a))    return 0.0;

    if (y != (double)a) x = exp(y * log(x));
    else    {
        if (y>0)    {
            y--;
            b = x;
            while(y--)  x *=b;
        }   else    {
            b = 1.0;
            while(y++)  b /= x;
            x = b;
        }
    }
    return  x;
}