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>
30 #define GZ_DBL_MAX std::numeric_limits<double>::max()
33 #define GZ_DBL_MIN std::numeric_limits<double>::min()
36 #define GZ_FLT_MAX std::numeric_limits<float>::max()
39 #define GZ_FLT_MIN std::numeric_limits<float>::min()
42 #define GZ_UINT32_MAX std::numeric_limits<uint32_t>::max()
45 #define GZ_UINT32_MIN std::numeric_limits<uint32_t>::min()
48 #define GZ_INT32_MAX std::numeric_limits<int32_t>::max()
51 #define GZ_INT32_MIN std::numeric_limits<int32_t>::min()
64 static const double NAN_D = std::numeric_limits<double>::quiet_NaN();
67 static const int NAN_I = std::numeric_limits<int>::quiet_NaN();
74 inline T
clamp(T _v, T _min, T _max)
100 return isnan(_v) || std::isinf(_v) ? 0.0f : _v;
108 return isnan(_v) || std::isinf(_v) ? 0.0 : _v;
115 inline T
mean(
const std::vector<T> &_values)
118 for (
unsigned int i = 0; i < _values.size(); ++i)
120 return sum / _values.size();
129 T avg = mean<T>(_values);
132 for (
unsigned int i = 0; i < _values.size(); ++i)
133 sum += (_values[i] - avg) * (_values[i] - avg);
134 return sum / _values.size();
141 inline T
max(
const std::vector<T> &_values)
144 for (
unsigned int i = 0; i < _values.size(); ++i)
145 if (_values[i] > max)
154 inline T
min(
const std::vector<T> &_values)
157 for (
unsigned int i = 0; i < _values.size(); ++i)
158 if (_values[i] < min)
168 inline bool equal(
const T &_a,
const T &_b,
169 const T &_epsilon = 1e-6)
171 return std::fabs(_a - _b) <= _epsilon;
179 inline T
precision(
const T &_a,
const unsigned int &_precision)
181 return boost::math::round(_a * pow(10, _precision)) / pow(10, _precision);
189 return ((_x != 0) && ((_x & (~_x + 1)) == _x));
205 while (_x & (_x - 1))
218 const char *p = _input.c_str();
219 if (!*p || *p ==
'?')
233 while (*p >=
'0' && *p <=
'9')
234 acc = acc * 10 + *p++ -
'0';
238 std::cerr <<
"Invalid int numeric format[" << _input <<
"]\n";
251 const char *p = _input.c_str();
252 if (!*p || *p ==
'?')
265 while (*p >=
'0' && *p <=
'9')
266 acc = acc * 10 + *p++ -
'0';
272 while (*p >=
'0' && *p <=
'9')
274 acc += (*p++ -
'0') * k;
293 while (*p >=
'0' && *p <=
'9')
294 f = f * 10 + *p++ -
'0';
296 acc *= pow(10, f*es);
301 std::cerr <<
"Invalid double numeric format[" << _input <<
"]\n";