acado_utils.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ACADO Toolkit.
3  *
4  * ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
5  * Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
6  * Milan Vukov, Rien Quirynen, KU Leuven.
7  * Developed within the Optimization in Engineering Center (OPTEC)
8  * under supervision of Moritz Diehl. All rights reserved.
9  *
10  * ACADO Toolkit is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * ACADO Toolkit is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with ACADO Toolkit; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 
34 
36 
38 {
39  if ( fabs( x - floor( x + 0.5) ) < 1.0e-5 )
40  return BT_TRUE;
41  else
42  return BT_FALSE;
43 }
44 
45 
46 double acadoDiv( double nom, double den )
47 {
48  if ( acadoIsInteger( nom/den ) == BT_TRUE )
49  return floor( nom/den + 0.5 );
50  else
51  return floor( nom/den );
52 }
53 
54 
55 double acadoMod( double nom, double den )
56 {
57  if ( acadoIsInteger( nom/den ) == BT_TRUE )
58  return 0.0;
59  else
60  return ( nom/den ) - floor( nom/den );
61 }
62 
63 
64 int acadoMax( const int x, const int y ){
65 
66  return (y<x)?x:y;
67 }
68 
69 
70 double acadoMax( const double x, const double y ){
71 
72  return (y<x)?x:y;
73 }
74 
75 
76 int acadoMin( const int x, const int y ){
77 
78  return (y>x)?x:y;
79 }
80 
81 
82 double acadoMin( const double x, const double y ){
83 
84  return (y>x)?x:y;
85 }
86 
87 
88 BooleanType acadoIsEqual( double x, double y, double TOL ){
89 
90  double maxabs= acadoMax(fabs(x),fabs(y));
91  if(maxabs > 1)
92  {
93  // use relative error
94  if ( fabs( x-y )/maxabs >= 10.0*TOL ) return BT_FALSE;
95  else return BT_TRUE ;
96  }
97  else
98  {
99  // use absolute error
100  if ( fabs( x-y ) >= 10.0*TOL ) return BT_FALSE;
101  else return BT_TRUE ;
102  }
103 }
104 
105 
106 BooleanType acadoIsGreater( double x, double y, double TOL ){
107 
108  if ( x >= y - TOL ) return BT_TRUE ;
109  else return BT_FALSE;
110 }
111 
112 
113 BooleanType acadoIsSmaller( double x, double y, double TOL ){
114 
115  if ( x <= y + TOL ) return BT_TRUE ;
116  else return BT_FALSE;
117 }
118 
119 
120 BooleanType acadoIsStrictlyGreater( double x, double y, double TOL ){
121 
122  if ( x > y + TOL ) return BT_TRUE ;
123  else return BT_FALSE;
124 }
125 
126 
127 BooleanType acadoIsStrictlySmaller( double x, double y, double TOL ){
128 
129  if ( x < y - TOL ) return BT_TRUE ;
130  else return BT_FALSE;
131 }
132 
133 
134 BooleanType acadoIsPositive( double x, double TOL ){
135 
136  if ( x >= TOL ) return BT_TRUE ;
137  else return BT_FALSE;
138 }
139 
140 
141 BooleanType acadoIsNegative( double x, double TOL ){
142 
143  if ( x <= -TOL ) return BT_TRUE ;
144  else return BT_FALSE;
145 }
146 
147 
148 BooleanType acadoIsZero( double x, double TOL ){
149 
150  return acadoIsEqual( x, 0.0, TOL );
151 }
152 
153 
154 BooleanType acadoIsInfty( double x, double TOL )
155 {
156  if ( ( acadoIsGreater( x, INFTY, TOL ) == BT_TRUE ) ||
157  ( acadoIsSmaller( x,-INFTY, TOL ) == BT_TRUE ) )
158  return BT_TRUE;
159  else
160  return BT_FALSE;
161 }
162 
163 
164 BooleanType acadoIsFinite( double x, double TOL )
165 {
166  if ( ( acadoIsStrictlySmaller( x, INFTY, TOL ) == BT_TRUE ) &&
167  ( acadoIsStrictlyGreater( x,-INFTY, TOL ) == BT_TRUE ) )
168  return BT_TRUE;
169  else
170  return BT_FALSE;
171 }
172 
173 
175  )
176 {
177  if ( ( x > -1.0 ) || ( x < 1.0 ) )
178  return BT_FALSE;
179  else
180  return BT_TRUE;
181 }
182 
183 
184 
185 int acadoRound (double x){
186 
187  if (x - floor(x) > 0.5)
188  return (int)ceil(x);
189  else
190  return (int)floor(x);
191 
192 }
193 
194 
195 int acadoFactorial( int n ){
196 
197  ASSERT( n>= 0 );
198  int r = 1;
199  for (int i = n; i > 1; --i)
200  r *= i;
201  return r;
202 }
203 
204 
205 int acadoRoundAway (double x) {
206 
207  return x < 0 ? floor(x) : ceil(x);
208 }
209 
211 
212 /*
213  * end of file
214  */
int acadoMax(const int x, const int y)
Definition: acado_utils.cpp:64
BooleanType acadoIsEqual(double x, double y, double TOL)
Definition: acado_utils.cpp:88
const double INFTY
BooleanType acadoIsNegative(double x, double TOL)
BooleanType acadoIsGreater(double x, double y, double TOL)
#define CLOSE_NAMESPACE_ACADO
int acadoFactorial(int n)
BooleanType acadoIsStrictlySmaller(double x, double y, double TOL)
BooleanType acadoIsNaN(double x)
BEGIN_NAMESPACE_ACADO BooleanType acadoIsInteger(double x)
Definition: acado_utils.cpp:37
int acadoRoundAway(double x)
int acadoRound(double x)
BooleanType acadoIsSmaller(double x, double y, double TOL)
BooleanType acadoIsPositive(double x, double TOL)
double acadoDiv(double nom, double den)
Definition: acado_utils.cpp:46
int acadoMin(const int x, const int y)
Definition: acado_utils.cpp:76
#define ASSERT(x)
#define BT_TRUE
Definition: acado_types.hpp:47
BooleanType acadoIsZero(double x, double TOL)
BooleanType acadoIsInfty(double x, double TOL)
BooleanType acadoIsFinite(double x, double TOL)
double acadoMod(double nom, double den)
Definition: acado_utils.cpp:55
#define BEGIN_NAMESPACE_ACADO
#define BT_FALSE
Definition: acado_types.hpp:49
BooleanType acadoIsStrictlyGreater(double x, double y, double TOL)


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:27