acado_utils.cpp
Go to the documentation of this file.
00001 /*
00002  *    This file is part of ACADO Toolkit.
00003  *
00004  *    ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
00005  *    Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
00006  *    Milan Vukov, Rien Quirynen, KU Leuven.
00007  *    Developed within the Optimization in Engineering Center (OPTEC)
00008  *    under supervision of Moritz Diehl. All rights reserved.
00009  *
00010  *    ACADO Toolkit is free software; you can redistribute it and/or
00011  *    modify it under the terms of the GNU Lesser General Public
00012  *    License as published by the Free Software Foundation; either
00013  *    version 3 of the License, or (at your option) any later version.
00014  *
00015  *    ACADO Toolkit is distributed in the hope that it will be useful,
00016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  *    Lesser General Public License for more details.
00019  *
00020  *    You should have received a copy of the GNU Lesser General Public
00021  *    License along with ACADO Toolkit; if not, write to the Free Software
00022  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00023  *
00024  */
00025 
00026 
00033 #include <acado/utils/acado_utils.hpp>
00034 
00035 BEGIN_NAMESPACE_ACADO
00036 
00037 BooleanType acadoIsInteger( double x )
00038 {
00039         if ( fabs( x - floor( x + 0.5) ) < 1.0e-5 )
00040                 return BT_TRUE;
00041         else
00042                 return BT_FALSE;
00043 }
00044 
00045 
00046 double acadoDiv( double nom, double den )
00047 {
00048         if ( acadoIsInteger( nom/den ) == BT_TRUE )
00049                 return floor( nom/den + 0.5 );
00050         else
00051                 return floor( nom/den );
00052 }
00053 
00054 
00055 double acadoMod( double nom, double den )
00056 {
00057         if ( acadoIsInteger( nom/den ) == BT_TRUE )
00058                 return 0.0;
00059         else
00060                 return ( nom/den ) - floor( nom/den );
00061 }
00062 
00063 
00064 int acadoMax( const int x, const int y ){
00065 
00066     return (y<x)?x:y;
00067 }
00068 
00069 
00070 double acadoMax( const double x, const double y ){
00071 
00072     return (y<x)?x:y;
00073 }
00074 
00075 
00076 int acadoMin( const int x, const int y ){
00077 
00078     return (y>x)?x:y;
00079 }
00080 
00081 
00082 double acadoMin( const double x, const double y ){
00083 
00084     return (y>x)?x:y;
00085 }
00086 
00087 
00088 BooleanType acadoIsEqual( double x, double y, double TOL ){
00089 
00090   double maxabs= acadoMax(fabs(x),fabs(y));
00091         if(maxabs > 1)
00092         {
00093                 // use relative error
00094                 if ( fabs( x-y )/maxabs >= 10.0*TOL ) return BT_FALSE;
00095                 else                      return BT_TRUE ;
00096         }
00097         else
00098         {
00099                 // use absolute error
00100                 if ( fabs( x-y ) >= 10.0*TOL ) return BT_FALSE;
00101                 else                      return BT_TRUE ;
00102         }
00103 }
00104 
00105 
00106 BooleanType acadoIsGreater( double x, double y, double TOL ){
00107 
00108     if ( x >= y - TOL ) return BT_TRUE ;
00109     else                return BT_FALSE;
00110 }
00111 
00112 
00113 BooleanType acadoIsSmaller( double x, double y, double TOL ){
00114 
00115     if ( x <= y + TOL ) return BT_TRUE ;
00116     else                return BT_FALSE;
00117 }
00118 
00119 
00120 BooleanType acadoIsStrictlyGreater( double x, double y, double TOL ){
00121 
00122     if ( x > y + TOL ) return BT_TRUE ;
00123     else                return BT_FALSE;
00124 }
00125 
00126 
00127 BooleanType acadoIsStrictlySmaller( double x, double y, double TOL ){
00128 
00129     if ( x < y - TOL ) return BT_TRUE ;
00130     else                return BT_FALSE;
00131 }
00132 
00133 
00134 BooleanType acadoIsPositive( double x, double TOL ){
00135 
00136     if ( x >= TOL ) return BT_TRUE ;
00137     else            return BT_FALSE;
00138 }
00139 
00140 
00141 BooleanType acadoIsNegative( double x, double TOL ){
00142 
00143     if ( x <= -TOL ) return BT_TRUE ;
00144     else             return BT_FALSE;
00145 }
00146 
00147 
00148 BooleanType acadoIsZero( double x, double TOL ){
00149 
00150     return acadoIsEqual( x, 0.0, TOL );
00151 }
00152 
00153 
00154 BooleanType acadoIsInfty( double x, double TOL )
00155 {
00156         if ( ( acadoIsGreater( x, INFTY, TOL ) == BT_TRUE ) ||
00157                  ( acadoIsSmaller( x,-INFTY, TOL ) == BT_TRUE ) )
00158                 return BT_TRUE;
00159         else
00160                 return BT_FALSE;
00161 }
00162 
00163 
00164 BooleanType acadoIsFinite( double x, double TOL )
00165 {
00166         if ( ( acadoIsStrictlySmaller( x, INFTY, TOL ) == BT_TRUE ) &&
00167                  ( acadoIsStrictlyGreater( x,-INFTY, TOL ) == BT_TRUE ) )
00168                 return BT_TRUE;
00169         else
00170                 return BT_FALSE;
00171 }
00172 
00173 
00174 BooleanType acadoIsNaN( double x
00175                                                 )
00176 {
00177         if ( ( x > -1.0 ) || ( x < 1.0 ) )
00178                 return BT_FALSE;
00179         else
00180                 return BT_TRUE;
00181 }
00182 
00183 
00184 
00185 int acadoRound (double x){
00186 
00187         if (x - floor(x) > 0.5)
00188                 return (int)ceil(x);
00189         else
00190                 return (int)floor(x);
00191 
00192 }
00193 
00194 
00195 int acadoFactorial( int n ){
00196   
00197         ASSERT( n>= 0 );
00198         int r = 1;
00199         for (int i = n; i > 1; --i)
00200                 r *= i;
00201         return r;
00202 }
00203 
00204 
00205 int acadoRoundAway (double x) {
00206 
00207         return x < 0 ? floor(x) : ceil(x);
00208 }
00209 
00210 CLOSE_NAMESPACE_ACADO
00211 
00212 /*
00213  *      end of file
00214  */


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Thu Aug 27 2015 11:57:48