|
STK++ 1.0
|
00001 /*--------------------------------------------------------------------*/ 00002 /* Copyright (C) 2004-2007 Serge Iovleff 00003 00004 This program is free software; you can redistribute it and/or modify 00005 it under the terms of the GNU Lesser General Public License as 00006 published by the Free Software Foundation; either version 2 of the 00007 License, or (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU Lesser General Public License for more details. 00013 00014 You should have received a copy of the GNU Lesser General Public 00015 License along with this program; if not, write to the 00016 Free Software Foundation, Inc., 00017 59 Temple Place, 00018 Suite 330, 00019 Boston, MA 02111-1307 00020 USA 00021 00022 Contact : Serge.Iovleff@stkpp.org 00023 */ 00024 00025 /* 00026 * Project: Base 00027 * Purpose: Define miscenaleous utility functions. 00028 * Author: Serge Iovleff, serge.iovleff@stkpp.org 00029 * 00030 **/ 00031 00037 #ifndef STK_MISC_H 00038 #define STK_MISC_H 00039 00040 #include <cmath> 00041 #include <cstdlib> // for rand 00042 00043 #include "STK_String.h" 00044 #include "STK_Integer.h" 00045 #include "STK_Real.h" 00046 #include "STK_Stream.h" 00047 #include "STK_Proxy.h" 00048 00049 namespace STK 00050 { 00058 template<class TYPE> 00059 inline TYPE sign(TYPE const& x, TYPE const& y = 1) 00060 { return( (x<0) ? -y : y); } 00061 00068 template<class TYPE> 00069 inline void swap(TYPE& x, TYPE& y) 00070 { 00071 TYPE aux(x); 00072 x = y; 00073 y = aux; 00074 } 00075 00082 template<class TYPE> 00083 inline TYPE const& min(TYPE const& x, TYPE const& y) 00084 { return( (x<y) ? x : y); } 00085 00092 template<class TYPE> 00093 inline TYPE const& max(TYPE const& x, TYPE const& y) 00094 { return( (x<y) ? y : x); } 00095 00102 template<class TYPE> 00103 inline TYPE abs(TYPE const& x) 00104 { return( (x<0) ? -x : x); } 00105 00111 inline Real frand() 00112 { return (Real)rand() / (RAND_MAX+1.0);} 00113 00122 inline bool isOdd(Integer const& x) 00123 { return( (x%2) == 1 ); } 00124 00133 inline bool isEven(Integer const& x) 00134 { return( (x%2) == 0 ); } 00135 00142 inline Integer round(Real const& x) 00143 { return( x < 0.0 ? Integer(x-0.5) : Integer(x+0.5));} 00144 00152 inline Real norm(Real const& x, Real const& y) 00153 { 00154 Real absx = abs(x), absy = abs(y); 00155 if (absx > absy) 00156 { 00157 return(absx * sqrt(double(1.0+(absy/absx)*(absy/absx)))); 00158 } 00159 else 00160 { 00161 return(absy == 0.0 ? 0.0 : absy * sqrt(double(1.0+(absx/absy)*(absx/absy)))); 00162 } 00163 } 00164 00165 } // namespace STK 00166 00167 #endif /* STK_MISC_H */