casadi_limits.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_CASADI_LIMITS_HPP
27 #define CASADI_CASADI_LIMITS_HPP
28 
29 #include <cmath>
30 #include <limits>
31 
35 namespace casadi {
36 
47  template<class T>
48  class CASADI_EXPORT casadi_limits {
49  public:
50  static bool is_zero(const T& val) {
51  return val==0;
52  }
53  static bool is_equal(const T& x, const T& y, casadi_int depth) {
54  return x==y;
55  }
56  static bool is_almost_zero(const T& val, double tol) {
57  return val<=tol && val>=-tol;
58  }
59  static bool is_one(const T& val) {
60  return val==1;
61  }
62  static bool is_minus_one(const T& val) {
63  return val==-1;
64  }
65  static bool is_constant(const T& val) {
66  return true;
67  }
68  static bool is_integer(const T& val) {
69  return val==static_cast<casadi_int>(val);
70  }
71  static bool is_inf(const T& val) {
72  return std::numeric_limits<T>::has_infinity && val==std::numeric_limits<T>::infinity();
73  }
74  static bool is_minus_inf(const T& val) {
75  return std::numeric_limits<T>::has_infinity && val==-std::numeric_limits<T>::infinity();
76  }
77  static bool is_nan(const T& val) {
78  return std::numeric_limits<T>::has_quiet_NaN && val!=val;
79  }
80  static const T zero;
81  static const T one;
82  static const T two;
83  static const T minus_one;
84  };
85 
86  template<class T>
87  inline bool is_zero(const T& x) {
88  return casadi_limits<T>::is_zero(x);
89  }
90 
91  template<class T>
92  const T casadi_limits<T>::zero = T(0);
93 
94  template<class T>
95  const T casadi_limits<T>::one = 1;
96 
97  template<class T>
98  const T casadi_limits<T>::two = 2;
99 
100  template<class T>
101  const T casadi_limits<T>::minus_one = -1;
102 
103 } // namespace casadi
104 #endif // CASADI_CASADI_LIMITS_HPP
casadi_limits class
static bool is_inf(const T &val)
static const T minus_one
static bool is_almost_zero(const T &val, double tol)
static bool is_constant(const T &val)
static bool is_equal(const T &x, const T &y, casadi_int depth)
static bool is_one(const T &val)
static bool is_minus_inf(const T &val)
static bool is_integer(const T &val)
static bool is_minus_one(const T &val)
static bool is_nan(const T &val)
static bool is_zero(const T &val)
The casadi namespace.
bool is_zero(const T &x)