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_DBL_INF std::numeric_limits<double>::infinity()
39 #define GZ_FLT_MAX std::numeric_limits<float>::max()
42 #define GZ_FLT_MIN std::numeric_limits<float>::min()
45 #define GZ_UINT32_MAX std::numeric_limits<uint32_t>::max()
48 #define GZ_UINT32_MIN std::numeric_limits<uint32_t>::min()
51 #define GZ_INT32_MAX std::numeric_limits<int32_t>::max()
54 #define GZ_INT32_MIN std::numeric_limits<int32_t>::min()
67 static const double NAN_D = std::numeric_limits<double>::quiet_NaN();
70 static const int NAN_I = std::numeric_limits<int>::quiet_NaN();
77 inline T
clamp(T _v, T _min, T _max)
103 return isnan(_v) || std::isinf(_v) ? 0.0f : _v;
111 return isnan(_v) || std::isinf(_v) ? 0.0 : _v;
118 inline T
mean(
const std::vector<T> &_values)
121 for (
unsigned int i = 0; i < _values.size(); ++i)
123 return sum / _values.size();
132 T avg = mean<T>(_values);
135 for (
unsigned int i = 0; i < _values.size(); ++i)
136 sum += (_values[i] - avg) * (_values[i] - avg);
137 return sum / _values.size();
144 inline T
max(
const std::vector<T> &_values)
147 for (
unsigned int i = 0; i < _values.size(); ++i)
148 if (_values[i] > max)
157 inline T
min(
const std::vector<T> &_values)
160 for (
unsigned int i = 0; i < _values.size(); ++i)
161 if (_values[i] < min)
171 inline bool equal(
const T &_a,
const T &_b,
172 const T &_epsilon = 1e-6)
174 return std::fabs(_a - _b) <= _epsilon;
182 inline T
precision(
const T &_a,
const unsigned int &_precision)
186 return boost::math::round(
187 _a * pow(10, _precision)) / pow(10, _precision);
200 return ((_x != 0) && ((_x & (~_x + 1)) == _x));
216 while (_x & (_x - 1))
229 const char *p = _input.c_str();
230 if (!*p || *p ==
'?')
244 while (*p >=
'0' && *p <=
'9')
245 acc = acc * 10 + *p++ -
'0';
249 std::cerr <<
"Invalid int numeric format[" << _input <<
"]\n";
262 const char *p = _input.c_str();
263 if (!*p || *p ==
'?')
276 while (*p >=
'0' && *p <=
'9')
277 acc = acc * 10 + *p++ -
'0';
283 while (*p >=
'0' && *p <=
'9')
285 acc += (*p++ -
'0') * k;
304 while (*p >=
'0' && *p <=
'9')
305 f = f * 10 + *p++ -
'0';
307 acc *= pow(10, f*es);
312 std::cerr <<
"Invalid double numeric format[" << _input <<
"]\n";
unsigned int roundUpPowerOfTwo(unsigned int _x)
Get the smallest power of two that is greater or equal to a given value.
Definition: Helpers.hh:208
int parseInt(const std::string &_input)
parse string into an integer
Definition: Helpers.hh:227
double parseFloat(const std::string &_input)
parse string into float
Definition: Helpers.hh:260
bool isPowerOfTwo(unsigned int _x)
is this a power of 2?
Definition: Helpers.hh:198
T min(const std::vector< T > &_values)
get the minimum value of vector of values
Definition: Helpers.hh:157
bool isnan(float _v)
check if a float is NaN
Definition: Helpers.hh:85
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:77
T variance(const std::vector< T > &_values)
get variance of vector of values
Definition: Helpers.hh:130
bool equal(const T &_a, const T &_b, const T &_epsilon=1e-6)
check if two values are equal, within a tolerance
Definition: Helpers.hh:171
T mean(const std::vector< T > &_values)
get mean of vector of values
Definition: Helpers.hh:118
static const int NAN_I
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:70
float fixnan(float _v)
Fix a nan value.
Definition: Helpers.hh:101
static const double NAN_D
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:67
bool isnan(double _v)
check if a double is NaN
Definition: Helpers.hh:93
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition: Helpers.hh:182
T max(const std::vector< T > &_values)
get the maximum value of vector of values
Definition: Helpers.hh:144