17 #ifndef _GAZEBO_MATH_FUNCTIONS_HH_
18 #define _GAZEBO_MATH_FUNCTIONS_HH_
20 #include <boost/math/special_functions/fpclassify.hpp>
21 #include <boost/math/special_functions/round.hpp>
31 #define GZ_DBL_MAX gazebo::math::MAX_D
34 #define GZ_DBL_MIN gazebo::math::MIN_D
37 #define GZ_DBL_INF gazebo::math::INF_D
40 #define GZ_FLT_MAX gazebo::math::MAX_F
43 #define GZ_FLT_MIN gazebo::math::MIN_F
46 #define GZ_UINT32_MAX gazebo::math::MAX_UI32
49 #define GZ_UINT32_MIN gazebo::math::MIN_UI32
52 #define GZ_INT32_MAX gazebo::math::MAX_I32
55 #define GZ_INT32_MIN gazebo::math::MIN_I32
70 MAX_D = std::numeric_limits<double>::max();
75 MIN_D = std::numeric_limits<double>::min();
80 INF_D = std::numeric_limits<double>::infinity();
85 NAN_D = std::numeric_limits<double>::quiet_NaN();
90 MAX_F = std::numeric_limits<float>::max();
95 MIN_F = std::numeric_limits<float>::min();
100 MAX_UI32 = std::numeric_limits<uint32_t>::max();
103 static const uint32_t
105 MIN_UI32 = std::numeric_limits<uint32_t>::min();
110 MAX_I32 = std::numeric_limits<int32_t>::max();
115 MIN_I32 = std::numeric_limits<int32_t>::min();
120 NAN_I = std::numeric_limits<int>::quiet_NaN();
132 return std::max(std::min(_v, _max), _min);
143 return (boost::math::isnan)(_v);
154 return (boost::math::isnan)(_v);
158 #pragma GCC diagnostic push
159 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
169 return isnan(_v) || std::isinf(_v) ? 0.0f : _v;
180 return isnan(_v) || std::isinf(_v) ? 0.0 : _v;
183 #pragma GCC diagnostic pop
193 mean(const std::vector<T> &_values)
196 for (
unsigned int i = 0; i < _values.size(); ++i)
198 return sum / _values.size();
208 variance(const std::vector<T> &_values)
210 T avg = mean<T>(_values);
213 for (
unsigned int i = 0; i < _values.size(); ++i)
214 sum += (_values[i] - avg) * (_values[i] - avg);
215 return sum / _values.size();
225 max(const std::vector<T> &_values)
227 T max = std::numeric_limits<T>::min();
228 for (
unsigned int i = 0; i < _values.size(); ++i)
229 if (_values[i] > max)
241 min(const std::vector<T> &_values)
243 T min = std::numeric_limits<T>::max();
244 for (
unsigned int i = 0; i < _values.size(); ++i)
245 if (_values[i] < min)
258 equal(const T &_a, const T &
_b, const T &_epsilon = 1e-6)
260 return std::fabs(_a - _b) <= _epsilon;
271 precision(const T &_a, const
unsigned int &_precision)
275 return boost::math::round(
276 _a * pow(10, _precision)) / pow(10, _precision);
290 isPowerOfTwo(
unsigned int _x)
292 return ((_x != 0) && ((_x & (~_x + 1)) == _x));
303 roundUpPowerOfTwo(
unsigned int _x)
309 #pragma GCC diagnostic push
310 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
312 if (isPowerOfTwo(_x))
315 #pragma GCC diagnostic pop
318 while (_x & (_x - 1))
332 parseInt(const std::
string& _input)
334 const char *p = _input.c_str();
336 #pragma GCC diagnostic push
337 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
339 if (!*p || *p ==
'?')
342 #pragma GCC diagnostic pop
356 while (*p >=
'0' && *p <=
'9')
357 acc = acc * 10 + *p++ -
'0';
361 std::cerr <<
"Invalid int numeric format[" << _input <<
"]\n";
375 parseFloat(const std::
string& _input)
377 const char *p = _input.c_str();
379 #pragma GCC diagnostic push
380 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
382 if (!*p || *p ==
'?')
385 #pragma GCC diagnostic pop
398 while (*p >=
'0' && *p <=
'9')
399 acc = acc * 10 + *p++ -
'0';
405 while (*p >=
'0' && *p <=
'9')
407 acc += (*p++ -
'0') * k;
426 while (*p >=
'0' && *p <=
'9')
427 f = f * 10 + *p++ -
'0';
429 acc *= pow(10, f*es);
434 std::cerr <<
"Invalid double numeric format[" << _input <<
"]\n";
T T _min
Definition: Helpers.hh:130
static const double GAZEBO_DEPRECATED(8.0) MAX_D
Double maximum value. This value will be similar to 1.79769e+308.
Definition: Helpers.hh:140
bool const T & _b
Definition: Helpers.hh:258