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()
58 static const double NAN_D = std::numeric_limits<double>::quiet_NaN();
61 static const int NAN_I = std::numeric_limits<int>::quiet_NaN();
68 inline T
clamp(T _v, T _min, T _max)
93 inline T
mean(
const std::vector<T> &_values)
96 for (
unsigned int i = 0; i < _values.size(); ++i)
98 return sum / _values.size();
107 T avg = mean<T>(_values);
110 for (
unsigned int i = 0; i < _values.size(); ++i)
111 sum += (_values[i] - avg) * (_values[i] - avg);
112 return sum / _values.size();
119 inline T
max(
const std::vector<T> &_values)
122 for (
unsigned int i = 0; i < _values.size(); ++i)
123 if (_values[i] > max)
132 inline T
min(
const std::vector<T> &_values)
135 for (
unsigned int i = 0; i < _values.size(); ++i)
136 if (_values[i] < min)
146 inline bool equal(
const T &_a,
const T &_b,
147 const T &_epsilon = 1e-6)
149 return std::fabs(_a - _b) <= _epsilon;
157 inline T
precision(
const T &_a,
const unsigned int &_precision)
159 return boost::math::round(_a * pow(10, _precision)) / pow(10, _precision);
167 return ((_x != 0) && ((_x & (~_x + 1)) == _x));
175 const char *p = _input.c_str();
176 if (!*p || *p ==
'?')
190 while (*p >=
'0' && *p <=
'9')
191 acc = acc * 10 + *p++ -
'0';
195 std::cerr <<
"Invalid int numeric format[" << _input <<
"]\n";
208 const char *p = _input.c_str();
209 if (!*p || *p ==
'?')
222 while (*p >=
'0' && *p <=
'9')
223 acc = acc * 10 + *p++ -
'0';
229 while (*p >=
'0' && *p <=
'9')
231 acc += (*p++ -
'0') * k;
250 while (*p >=
'0' && *p <=
'9')
251 f = f * 10 + *p++ -
'0';
253 acc *= pow(10, f*es);
258 std::cerr <<
"Invalid double numeric format[" << _input <<
"]\n";