/* ******************************************************************* app_math_math.c ==================================================================== void app_math_init_table() -------------------------------------------------------------------- 機能: 下記の配列に,三角関数のテーブルを作成 配列名・サイズ: Gf_math_sin_table[N_SIN_TABLE + 1] 有効数字は,N_SIN_TABLE が 512 の場合で5桁,2048 の場合で 7 桁. その他の数学関数使用開始前に,必ず一度実行すること. 引数: 無し 戻り値: 無し ==================================================================== double app_math_sin( double f_rad ) double app_math_cos( double f_rad ) -------------------------------------------------------------------- 機能: 位相(radian)を与えると,三角関数の sin および cos の値を返す. 引数: double f_rad 位相(radian) 戻り値: sin および cos の値 ==================================================================== double app_math_atan( double f_tangent ) -------------------------------------------------------------------- 機能: Tangent 値を与えると,偏角(radian)を返す. 偏角は,0 ≦ θ < 2π の範囲で返る. 引数: double f_rad Tangent 値 戻り値: 偏角(radian,0 ≦ θ < 2π) ==================================================================== double app_math_log( double f_org ) -------------------------------------------------------------------- 機能: 値を与えると,その自然対数を返す. 値が0以下の場合,可能な限り負に大きい数を返す(本来,-Inf.). 引数: double f_org 自然対数を求める値( f_org > 0 ) 戻り値: 自然対数 ==================================================================== double app_math_log10( double f_org ) -------------------------------------------------------------------- 機能: 値を与えると,その常用対数を返す. 値が0以下の場合,可能な限り負に大きい数を返す(本来,-Inf.). 引数: double f_org 常用対数を求める値( f_org > 0 ) 戻り値: 常用対数 ==================================================================== double app_math_exp( double f_org ) -------------------------------------------------------------------- 機能: 指数を与えると,その e を底とするべき乗を返す. 引数: double f_org e を底とするべき乗を求める指数 戻り値: e のべき乗 ==================================================================== double app_math_pow( double f_base, double f_exp ) -------------------------------------------------------------------- 機能: 底と指数を与えると,べき乗を返す. 引数: double f_base べき乗を求める底 double f_exp べき乗を求める指数 戻り値: べき乗 ==================================================================== double app_math_sqrt( double f_org ) -------------------------------------------------------------------- 機能: 値を与えると,その平方根を返す. 引数: double f_org 平方根を求める値( f_org > 0 ) 戻り値: 平方根 ==================================================================== double app_math_rad2deg( double f_rad ) -------------------------------------------------------------------- 機能: Radian を与えると,degree を返す. 戻り値は 0 ≦ θ < 360 の範囲で返る. 引数: double f_rad 位相(radian) 戻り値: 位相(degree, 0 ≦ θ < 360) ==================================================================== double app_math_regulate_rad( double f_rad ) -------------------------------------------------------------------- 機能: 位相(radian)を 0 ≦ θ < 2π の範囲に正規化する. 引数: double f_rad 位相(radian) 戻り値: 位相(radian, 0 ≦ θ < 2π) ==================================================================== ******************************************************************* */ #include "app_common.h" #define MATH_PI (3.1415926536) #define MATH_LN10 (2.302585093) #define MATH_N_NEWTON 10 /* double app_math_sin( double f_rad ); double app_math_cos( double f_rad ); double app_math_atan( double f_tangent ); double app_math_log( double f_org ); double app_math_log10( double f_org ); double app_math_exp( double f_org ); double app_math_pow( double f_base, double f_exp ); double app_math_sqrt( double f_org ); double app_math_rad2deg( double f_rad ); double app_math_regulate_rad( double f_rad ); */ // sin テーブルを生成 void app_math_init_table(){ double f_rad, f_val, f_tmp; double f_i; int i; // sin の値が入った配列を生成. // 0 ≦ θ < π/2 の範囲を N_SIN_TABLE で分割. for( i=0; i= 1e30 ){ return 1e30; } // 0 以下の場合,可能な限り負に大きい数を返す(本来 -Inf). if( f_org <= 0 ){ return -1e30; } // Taylor 級数で正しく求まる 1 付近になるまで平方根を取る. for ( i=0, f_param = f_org; f_param > 1.3; i++ ){ f_param = app_math_sqrt( f_param ); } f_val = log_base( f_param ); // 平方根を取った回数だけ2倍する. for ( j=0; j 1; i_times++ ){ f_base /= 2; } // Taylor 級数で exp を計算 f_val = 1; f_tmp1 = 1; f_tmp2 = 1; for ( f_i = 1; f_i < 12; f_i++ ){ f_tmp1 *= f_base; // Taylor 級数の分子 f_tmp2 *= f_i; // Taylor 級数の分母 f_val += f_tmp1 / f_tmp2; } // 2で除算した回数だけ自乗 for( i=0; i 1 ) f_s = f_x; else f_s = 1; i = 0; do { i++; f_last = f_s; f_s = ( f_x / f_s + f_s ) * 0.5; } while( f_s < f_last ); return f_last; } /* double app_math_rad2deg( double f_rad ) { double f_deg; f_deg = f_rad * 180 / MATH_PI; f_deg -= (int)( f_deg / 360 ) * 360; if ( f_deg < 0 ) f_deg += 360; return f_deg; } */ double app_math_regulate_rad( double f_rad ) { f_rad -= (double)( (int)( f_rad / ( MATH_PI * 2 ) ) ) * MATH_PI * 2; if ( f_rad < 0 ){ f_rad += MATH_PI * 2; } return f_rad; }