Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

Math.h

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Lamp : Open source game middleware
00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //------------------------------------------------------------------------------
00019 
00020 /** @file
00021  * 数学ヘッダ
00022  * @author Junpee
00023  */
00024 
00025 #ifndef MATH_H_
00026 #define MATH_H_
00027 
00028 #include <cmath>
00029 #include <cfloat>
00030 
00031 namespace Lamp{
00032 
00033 //------------------------------------------------------------------------------
00034 /**
00035  * 数学ヘッダ
00036  */
00037 class Math{
00038 public:
00039     //--------------------------------------------------------------------------
00040     // 定数
00041     //--------------------------------------------------------------------------
00042     /// パイ
00043     static const float PI;
00044 
00045     /// パイ * 4
00046     static const float quadruplePI;
00047 
00048     /// パイ * 2
00049     static const float doublePI;
00050 
00051     /// パイ / 2
00052     static const float halfPI;
00053 
00054     /// パイ / 4
00055     static const float quadrantPI;
00056 
00057     /// 自然対数の底
00058     static const float E;
00059 
00060     /// イプシロン
00061     static const float epsilon;
00062 
00063     //--------------------------------------------------------------------------
00064     // 単位変換
00065     //--------------------------------------------------------------------------
00066     /**
00067      * 度からラジアンへの変換
00068      * @param degree 度
00069      * @return ラジアン
00070      */
00071     static inline float toRadian(float degree){
00072         static const float scale = PI / 180.f;
00073         return degree * scale;
00074     }
00075 
00076     /**
00077      * ラジアンから度への変換
00078      * @param radian ラジアン
00079      * @return 度
00080      */
00081     static inline float toDegree(float radian){
00082         static const float scale = 180.f / PI;
00083         return radian * scale;
00084     }
00085 
00086     //--------------------------------------------------------------------------
00087     // Maximum, Minimum
00088     //--------------------------------------------------------------------------
00089     /**
00090      * Maximum
00091      * @param a 左辺値
00092      * @param b 右辺値
00093      * @return 大きいほうの値を返す
00094      */
00095     static inline int maximum(int a, int b){
00096         if(a > b){ return a; }
00097         else{ return b; }
00098     }
00099 
00100     /**
00101      * Maximum
00102      * @param a 左辺値
00103      * @param b 右辺値
00104      * @return 大きいほうの値を返す
00105      */
00106     static inline u_int maximum(u_int a, u_int b){
00107         if(a > b){ return a; }
00108         else{ return b; }
00109     }
00110 
00111     /**
00112      * Maximum
00113      * @param a 左辺値
00114      * @param b 右辺値
00115      * @return 大きいほうの値を返す
00116      */
00117     static inline float maximum(float a, float b){
00118         if(a > b){ return a; }
00119         else{ return b; }
00120     }
00121 
00122     /**
00123      * Minimum
00124      * @param a 左辺値
00125      * @param b 右辺値
00126      * @return 小さいほうの値を返す
00127      */
00128     static inline int minimum(int a, int b){
00129         if(a < b){ return a; }
00130         else{ return b; }
00131     }
00132 
00133     /**
00134      * Minimum
00135      * @param a 左辺値
00136      * @param b 右辺値
00137      * @return 小さいほうの値を返す
00138      */
00139     static inline int minimum(u_int a, u_int b){
00140         if(a < b){ return a; }
00141         else{ return b; }
00142     }
00143 
00144     /**
00145      * Minimum
00146      * @param a 左辺値
00147      * @param b 右辺値
00148      * @return 小さいほうの値を返す
00149      */
00150     static inline float minimum(float a, float b){
00151         if(a < b){ return a; }
00152         else{ return b; }
00153     }
00154 
00155     //--------------------------------------------------------------------------
00156     // スワップ
00157     //--------------------------------------------------------------------------
00158     /**
00159      * スワップ
00160      * @param a 左辺値
00161      * @param b 右辺値
00162      */
00163     static inline void swap(int& a, int& b){
00164         int tmp = a;
00165         a = b;
00166         b = tmp;
00167     }
00168 
00169     /**
00170      * スワップ
00171      * @param a 左辺値
00172      * @param b 右辺値
00173      */
00174     static inline void swap(u_int& a, u_int& b){
00175         u_int tmp = a;
00176         a = b;
00177         b = tmp;
00178     }
00179 
00180     /**
00181      * スワップ
00182      * @param a 左辺値
00183      * @param b 右辺値
00184      */
00185     static inline void swap(float& a, float& b){
00186         float tmp = a;
00187         a = b;
00188         b = tmp;
00189     }
00190 
00191     //--------------------------------------------------------------------------
00192     // 絶対値
00193     //--------------------------------------------------------------------------
00194     /**
00195      * 絶対値
00196      * @param x 絶対値にする数字
00197      * @return 絶対値
00198      */
00199     static inline int abs(int x){ return ::abs(x); }
00200 
00201     /**
00202      * 絶対値
00203      * @param x 絶対値にする数字
00204      * @return 絶対値
00205      */
00206     static inline float abs(float x){ return ::fabsf(x); }
00207 
00208     //--------------------------------------------------------------------------
00209     // 切り捨て、切り上げ
00210     //--------------------------------------------------------------------------
00211     /**
00212      * 小数部の切り捨て
00213      * @param x 小数部を切り捨てる値
00214      * @return 小数部が切り捨てられた値
00215      */
00216     static inline float floor(float x){ return ::floorf(x); }
00217 
00218     /**
00219      * 小数部の切り上げ
00220      * @param x 小数部を切り上げる値
00221      * @return 小数部が切り上げられた値
00222      */
00223     static inline float ceil(float x){ return ::ceilf(x); }
00224 
00225     //--------------------------------------------------------------------------
00226     // 浮動小数値演算
00227     //--------------------------------------------------------------------------
00228     /**
00229      * 余り
00230      * @param x 分母
00231      * @param y 分子
00232      * @return xをyで割った際の余りを返します
00233      */
00234     static inline float fmod(float x, float y){ return ::fmodf(x, y); }
00235 
00236     /**
00237      * 整数部と小数部への分離
00238      * @param x 対象となる浮動小数値
00239      * @param integer [out] xの整数部を格納するポインタ
00240      * @return xの小数部分
00241      */
00242     static inline float modf(float x, float* integer){
00243         return ::modff(x, integer);
00244     }
00245 
00246     /**
00247      * 平方根
00248      * @param x 平方根をとる正の値
00249      * @return 平方根
00250      */
00251     static inline float sqrt(float x){
00252         Assert(x >= 0.f);
00253         return ::sqrtf(x);
00254     }
00255 
00256     /**
00257      * 浮動小数点チェック
00258      * @param x チェックする値
00259      * @return 正常な値ならtrue
00260      */
00261     static inline bool classCheck(float x){
00262         int result = ::_fpclass(x);
00263         if((result == _FPCLASS_SNAN) ||     // シグナル型非数
00264             (result == _FPCLASS_QNAN) ||    // クワイエット型非数
00265             (result == _FPCLASS_PINF) ||    // 正の無限大
00266             (result == _FPCLASS_NINF)){     // 負の無限大
00267             return false;
00268         }
00269         return true;
00270     }
00271 
00272     //--------------------------------------------------------------------------
00273     // 指数、対数
00274     //--------------------------------------------------------------------------
00275     /**
00276      * べき乗
00277      * @param x 底
00278      * @param y 指数
00279      * @return xのy乗を返します
00280      */
00281     static inline float pow(float x, float y){ return ::powf(x, y); }
00282 
00283     /**
00284      * 指数
00285      * @param x 乗数
00286      * @return 自然対数の底 Math::E のx乗の値を返します
00287      */
00288     static inline float exp(float x){ return ::expf(x); }
00289 
00290     /**
00291      * 自然対数
00292      * @param x 自然対数をとる対象値
00293      * @return xの自然対数を返します
00294      */
00295     static inline float log(float x){ return ::logf(x); }
00296 
00297     /**
00298      * 常用対数
00299      * @param x 常用対数をとる対象値
00300      * @return xの常用対数を返します
00301      */
00302     static inline float log10(float x){ return ::log10f(x); }
00303 
00304     /**
00305      * 2の累乗かどうか
00306      * @return 2の累乗ならtrue
00307      */
00308     static inline bool checkPow2(int value){
00309         while(true){
00310             if(value == 1){ return true; }
00311             if((value % 2) == 1){ return false; }
00312             value /= 2;
00313         }
00314     }
00315 
00316     //--------------------------------------------------------------------------
00317     // 三角関数
00318     //--------------------------------------------------------------------------
00319     /**
00320      * 正弦
00321      * @param x 正弦を計算する値
00322      * @return xの正弦
00323      */
00324     static inline float sin(float x){ return ::sinf(x); }
00325 
00326     /**
00327      * 余弦
00328      * @param x 余弦を計算する値
00329      * @return xの余弦
00330      */
00331     static inline float cos(float x){ return ::cosf(x); }
00332 
00333     /**
00334      * 正接
00335      * @param x 正接を計算する値
00336      * @return xの正接
00337      */
00338     static inline float tan(float x){ return ::tanf(x); }
00339 
00340     /**
00341      * 双曲正弦
00342      * @param x 双曲正弦を計算する値
00343      * @return xの双曲正弦
00344      */
00345     static inline float sinh(float x){ return ::sinhf(x); }
00346 
00347     /**
00348      * 双曲余弦
00349      * @param x 双曲余弦を計算する値
00350      * @return xの双曲余弦
00351      */
00352     static inline float cosh(float x){ return ::coshf(x); }
00353 
00354     /**
00355      * 双曲正接
00356      * @param x 双曲正接を計算する値
00357      * @return xの双曲正接
00358      */
00359     static inline float tanh(float x){ return ::tanhf(x); }
00360 
00361     /**
00362      * 逆正弦
00363      * @param x 逆正弦を計算する値。-1 〜 1
00364      * @return xの逆正弦。-π/2 〜 π/2
00365      */
00366     static inline float asin(float x){ return ::asinf(x); }
00367 
00368     /**
00369      * 逆余弦
00370      * @param x 逆余弦を計算する値。-1 〜 1
00371      * @return xの逆余弦。-π/2 〜 π/2
00372      */
00373     static inline float acos(float x){ return ::acosf(x); }
00374 
00375     /**
00376      * 逆正接
00377      * @param x 逆正接を計算する値
00378      * @return xの逆正接。-π/2 〜 π/2
00379      */
00380     static inline float atan(float x){ return ::atanf(x); }
00381 
00382     /**
00383      * 逆正接
00384      * @param y 逆正接を計算するy値
00385      * @param x 逆正接を計算するx値
00386      * @return (x, y)の逆正接。-π 〜 π
00387      */
00388     static inline float atan2(float y, float x){ return ::atan2f(y, x); }
00389 
00390 
00391 private:
00392     // コンストラクタの隠蔽
00393     Math();
00394 
00395 };
00396 //------------------------------------------------------------------------------
00397 } // End of namespace Lamp
00398 #endif // End of MATH_H_
00399 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:32 2005 for Lamp by doxygen 1.3.2