00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Base Library 00005 * Module : Math 00006 * Package : Zeus.ZeusBase.Math 00007 * Author : Benjamin Hadorn 00008 * Date : 27.12.2011 00009 * System : Zeus-Framework 00010 ***************************************************************************** 00011 * Licence: * 00012 * This library is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU Lesser General Public License as * 00014 * published by the Free Software Foundation; either version * 00015 * 2.1 of the License, or (at your option) any later version. * 00016 * * 00017 * This library is distributed in the hope that it will be useful, * 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00020 * GNU Lesser General Public License for more details. * 00021 * * 00022 * You should have received a copy of the GNU Lesser General Public * 00023 * License along with this library; if not, write to the Free Software * 00024 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA * 00025 *****************************************************************************/ 00026 00027 /***************************************************************************** 00028 * Changes: 00029 * 27.12.2011 bha: created zeus 2.0 00030 *****************************************************************************/ 00031 00032 #ifndef Zeus__MathHPP 00033 #define Zeus__MathHPP 00034 00035 #include <zeusbase/Config/PlatformDefines.hpp> 00036 #include <math.h> 00037 00038 BEGIN_NAMESPACE_Zeus 00039 00040 /**************************************************************************/ 00044 /**************************************************************************/ 00045 class TMath 00046 { 00047 public: 00048 /**************************************************************************/ 00051 /**************************************************************************/ 00052 inline static Float ceil(const Float& rfValue) 00053 { 00054 #if defined(ENABLE_LINUX_BINDING) 00055 return ::ceil(rfValue); 00056 #else 00057 #if defined(_MSC_VER) 00058 return ::ceil(rfValue); 00059 #else 00060 return std::ceil(rfValue); 00061 #endif 00062 #endif 00063 } 00064 00065 /**************************************************************************/ 00068 /**************************************************************************/ 00069 inline static Int ceilInt(const Float& rfValue) 00070 { 00071 return static_cast<Int>(TMath::ceil(rfValue)); 00072 } 00073 00074 /**************************************************************************/ 00077 /**************************************************************************/ 00078 inline static Float exp(const Float& rfX) 00079 { 00080 return ::exp(rfX); 00081 } 00082 00083 /**************************************************************************/ 00086 /**************************************************************************/ 00087 inline static Int expInt(const Float& rfX) 00088 { 00089 return roundInt(TMath::exp(rfX)); 00090 } 00091 00092 /**************************************************************************/ 00095 /**************************************************************************/ 00096 inline static Float floor(const Float& rfValue) 00097 { 00098 #if defined(ENABLE_LINUX_BINDING) 00099 return ::floor(rfValue); 00100 #else 00101 #if defined(_MSC_VER) 00102 return ::floor(rfValue); 00103 #else 00104 return std::floor(rfValue); 00105 #endif 00106 #endif 00107 } 00108 00109 /**************************************************************************/ 00112 /**************************************************************************/ 00113 inline static Int floorInt(const Float& rfValue) 00114 { 00115 return static_cast<Int>(TMath::floor(rfValue)); 00116 } 00117 00118 /************************************************************************/ 00121 /************************************************************************/ 00122 inline static Float ln(const Float& rfValue) 00123 { 00124 return ::log(rfValue); 00125 } 00126 00127 /************************************************************************/ 00130 /************************************************************************/ 00131 inline static Int lnInt(const Float& rfValue) 00132 { 00133 return roundInt(TMath::ln(rfValue)); 00134 } 00135 00136 /************************************************************************/ 00139 /************************************************************************/ 00140 inline static Float log(const Float& rfValue, const Float& rfBase) 00141 { 00142 return (::log(rfValue) / ::log(rfBase)); 00143 } 00144 00145 /************************************************************************/ 00148 /************************************************************************/ 00149 inline static Int logInt(const Float& rfValue, const Float& rfBase) 00150 { 00151 return static_cast<Int>(TMath::log(rfValue, rfBase)); 00152 } 00153 00154 /**********************************************************************/ 00160 /**********************************************************************/ 00161 inline static Float maxFloat(Float fD1, Float fD2) 00162 { 00163 Float fRetval = fD1; 00164 if (fD1 < fD2) 00165 { 00166 fRetval = fD2; 00167 } 00168 return fRetval; 00169 } 00170 00171 /**********************************************************************/ 00177 /**********************************************************************/ 00178 inline static Float minFloat(Float fD1, Float fD2) 00179 { 00180 Float fRetval = fD1; 00181 if (fD1 > fD2) 00182 { 00183 fRetval = fD2; 00184 } 00185 return fRetval; 00186 } 00187 00188 /**********************************************************************/ 00194 /**********************************************************************/ 00195 inline static Int maxInt(Int iD1, Int iD2) 00196 { 00197 Int iRetval = iD1; 00198 if (iD1 < iD2) 00199 { 00200 iRetval = iD2; 00201 } 00202 return iRetval; 00203 } 00204 00205 /**********************************************************************/ 00211 /**********************************************************************/ 00212 inline static Int minInt(Int iD1, Int iD2) 00213 { 00214 Int iRetval = iD1; 00215 if (iD1 > iD2) 00216 { 00217 iRetval = iD2; 00218 } 00219 return iRetval; 00220 } 00221 00222 /**********************************************************************/ 00228 /**********************************************************************/ 00229 inline static Float pow(const Float& rfBase, const Float& rfExp) 00230 { 00231 return ::pow(rfBase, rfExp); 00232 } 00233 00234 /**********************************************************************/ 00240 /**********************************************************************/ 00241 inline static Int powInt(const Float& rfBase, const Float& rfExp) 00242 { 00243 return static_cast<Int>(::pow(rfBase, rfExp)); 00244 } 00245 00246 /**********************************************************************/ 00252 /**********************************************************************/ 00253 static Float roundEx(Float fValue, Float fPrecision) 00254 { 00255 Float fRetval = fValue; 00256 if (fPrecision != 0.0) 00257 { 00258 Float fX = fValue/fPrecision; 00259 Float dXTest = fX - floor(fX); 00260 00261 if (dXTest < 0.5) 00262 { 00263 fRetval = floor(fX)*fPrecision; 00264 } 00265 else 00266 { 00267 fRetval = ceil(fX)*fPrecision; 00268 } 00269 } 00270 return fRetval; 00271 } 00272 00273 /**********************************************************************/ 00278 /**********************************************************************/ 00279 inline static Float round(const Float& rfValue) 00280 { 00281 return roundEx(rfValue, 1.0); 00282 } 00283 00284 /**********************************************************************/ 00289 /**********************************************************************/ 00290 inline static Int roundInt(const Float& rfValue) 00291 { 00292 return static_cast<Int>(roundEx(rfValue, 1.0)); 00293 } 00294 00295 /**********************************************************************/ 00298 /**********************************************************************/ 00299 inline static Float sqrt(const Float& rfValue) 00300 { 00301 return ::sqrt(rfValue); 00302 } 00303 00304 /**********************************************************************/ 00307 /**********************************************************************/ 00308 inline static Int sqrtInt(const Float& rfValue) 00309 { 00310 return roundInt(TMath::sqrt(rfValue)); 00311 } 00312 00313 /**********************************************************************/ 00321 /**********************************************************************/ 00322 inline static Int gcd(Int iA, Int iB) 00323 { 00324 while (iB != 0) 00325 { 00326 Int iTemp = iB; 00327 iB = iA % iB; 00328 iA = iTemp; 00329 } 00330 return iA; 00331 } 00332 00333 /**********************************************************************/ 00345 /**********************************************************************/ 00346 static Int gcd(Int iA, Int iB, Int& rX, Int& rY) 00347 { 00348 rX = 1; 00349 rY = 0; 00350 Int iX1 = 0; 00351 Int iY1 = 1; 00352 while (iB != 0) 00353 { 00354 Int iTemp = iB; 00355 Int iQuotient = iA / iB; //integer div 00356 iB = iA % iB; 00357 iA = iTemp; 00358 00359 iTemp = iX1; 00360 iX1 = rX - iQuotient * iX1; 00361 rX = iTemp; 00362 00363 iTemp = iY1; 00364 iY1 = rY - iQuotient * iY1; 00365 rY = iTemp; 00366 } 00367 return iA; 00368 } 00369 00370 protected: 00371 inline TMath() {} 00372 inline virtual ~TMath() {} 00373 }; 00374 00375 END_NAMESPACE_Zeus 00376 00377 #endif 00378