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";
unsigned int roundUpPowerOfTwo(unsigned int _x)
Get the smallest power of two that is greater or equal to a given value.
Definition: Helpers.hh:197
int parseInt(const std::string &_input)
parse string into an integer
Definition: Helpers.hh:216
double parseFloat(const std::string &_input)
parse string into float
Definition: Helpers.hh:249
bool isPowerOfTwo(unsigned int _x)
is this a power of 2?
Definition: Helpers.hh:187
T min(const std::vector< T > &_values)
get the minimum value of vector of values
Definition: Helpers.hh:154
bool isnan(float _v)
check if a float is NaN
Definition: Helpers.hh:82
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:74
T variance(const std::vector< T > &_values)
get variance of vector of values
Definition: Helpers.hh:127
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:168
T mean(const std::vector< T > &_values)
get mean of vector of values
Definition: Helpers.hh:115
static const int NAN_I
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:67
float fixnan(float _v)
Fix a nan value.
Definition: Helpers.hh:98
static const double NAN_D
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:64
bool isnan(double _v)
check if a double is NaN
Definition: Helpers.hh:90
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition: Helpers.hh:179
T max(const std::vector< T > &_values)
get the maximum value of vector of values
Definition: Helpers.hh:141