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>
29 #define GZ_DBL_MAX std::numeric_limits<double>::max()
30 #define GZ_DBL_MIN std::numeric_limits<double>::min()
32 #define GZ_FLT_MAX std::numeric_limits<float>::max()
33 #define GZ_FLT_MIN std::numeric_limits<float>::min()
45 static const double NAN_D = std::numeric_limits<double>::quiet_NaN();
48 static const int NAN_I = std::numeric_limits<int>::quiet_NaN();
55 inline T
clamp(T _v, T _min, T _max)
80 inline T
mean(
const std::vector<T> &_values)
83 for (
unsigned int i = 0; i < _values.size(); ++i)
85 return sum / _values.size();
92 inline T
variance(
const std::vector<T> &_values)
94 T avg = mean<T>(_values);
97 for (
unsigned int i = 0; i < _values.size(); ++i)
98 sum += (_values[i] - avg) * (_values[i] - avg);
99 return sum / _values.size();
106 inline T
max(
const std::vector<T> &_values)
109 for (
unsigned int i = 0; i < _values.size(); ++i)
110 if (_values[i] > max)
119 inline T
min(
const std::vector<T> &_values)
122 for (
unsigned int i = 0; i < _values.size(); ++i)
123 if (_values[i] < min)
133 inline bool equal(
const T &_a,
const T &_b,
134 const T &_epsilon = 1e-6)
136 return std::fabs(_a - _b) <= _epsilon;
144 inline T
precision(
const T &_a,
const unsigned int &_precision)
146 return boost::math::round(_a * pow(10, _precision)) / pow(10, _precision);
154 return ((_x != 0) && ((_x & (~_x + 1)) == _x));
162 const char *p = _input.c_str();
163 if (!*p || *p ==
'?')
177 while (*p >=
'0' && *p <=
'9')
178 acc = acc * 10 + *p++ -
'0';
182 std::cerr <<
"Invalid int numeric format[" << _input <<
"]\n";
195 const char *p = _input.c_str();
196 if (!*p || *p ==
'?')
209 while (*p >=
'0' && *p <=
'9')
210 acc = acc * 10 + *p++ -
'0';
216 while (*p >=
'0' && *p <=
'9')
218 acc += (*p++ -
'0') * k;
237 while (*p >=
'0' && *p <=
'9')
238 f = f * 10 + *p++ -
'0';
240 acc *= pow(10, f*es);
245 std::cerr <<
"Invalid double numeric format[" << _input <<
"]\n";