Simple Application Framework  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Saf/Math/Basic.h
Go to the documentation of this file.
00001 /*
00002     This file is part of Simple Application Framework (Saf) library.
00003     Copyright (C) 2010 - 2012 Ondrej Danek <ondrej.danek@gmail.com>
00004 
00005     This library is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published 
00007     by the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     Saf 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
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with Simple Application Framework library. If not, 
00017     see <http://www.gnu.org/licenses/>.
00018 */
00019 
00028 #ifndef SAF_MATH_BASIC_H
00029 #define SAF_MATH_BASIC_H
00030 
00031 #include <math.h>
00032 #include "../Type.h"
00033 
00034 namespace Saf
00035 {
00037     namespace Math
00038     {
00040         template <class T> 
00041         inline T Abs(T x)
00042         {
00043             return x < 0 ? -x : x;
00044         }
00045 
00047         template <>
00048         inline Uint8 Abs(Uint8 x)
00049         {
00050             return x;
00051         }
00052 
00053         template <>
00054         inline Uint16 Abs(Uint16 x)
00055         {
00056             return x;
00057         }
00058 
00059         template <>
00060         inline Uint32 Abs(Uint32 x)
00061         {
00062             return x;
00063         }
00064 
00065         template <>
00066         inline Uint64 Abs(Uint64 x)
00067         {
00068             return x;
00069         }
00077         template <class T>
00078         inline Int8 Sgn(T val)
00079         {
00080             return (val < 0) ? -1 : ((val > 0) ? 1 : 0);
00081         }
00082 
00090         template <class T> 
00091         inline T Restrict(T x, T low, T high) 
00092         { 
00093             return x > high ? high : (x < low ? low : x); 
00094         }
00095 
00100         template <class T>
00101         inline T Round(T v)
00102         {
00103             return v;            
00104         }
00105 
00107         template <>
00108         inline Float32 Round(Float32 v)
00109         {
00110             return (v >= 0) ? floor(v + 0.5f) : ceil(v - 0.5f);
00111         }
00112 
00113         template <>
00114         inline Float64 Round(Float64 v)
00115         {
00116             return (v >= 0) ? floor(v + 0.5) : ceil(v - 0.5);
00117         }    
00121         template <class T>
00122         inline T IntegerDivCeil(T dividend, T divisor)
00123         {
00124             return (dividend == 0) ? 0 : (1 + (dividend - 1) / divisor);
00125         }
00126     }
00127 }
00128 
00129 #endif