17 #ifndef _IGNITION_MATH_FUNCTIONS_HH_
18 #define _IGNITION_MATH_FUNCTIONS_HH_
20 #ifndef _USE_MATH_DEFINES
21 # define _USE_MATH_DEFINES
32 #define IGN_DBL_MAX std::numeric_limits<double>::max()
35 #define IGN_DBL_MIN std::numeric_limits<double>::min()
38 #define IGN_DBL_LOW std::numeric_limits<double>::lowest()
41 #define IGN_DBL_INF std::numeric_limits<double>::infinity()
44 #define IGN_FLT_MAX std::numeric_limits<float>::max()
47 #define IGN_FLT_MIN std::numeric_limits<float>::min()
50 #define IGN_FLT_LOW std::numeric_limits<float>::lowest()
53 #define IGN_UINT32_MAX std::numeric_limits<uint32_t>::max()
56 #define IGN_UINT32_MIN std::numeric_limits<uint32_t>::min()
60 #define IGN_UINT32_LOW std::numeric_limits<uint32_t>::lowest()
63 #define IGN_INT32_MAX std::numeric_limits<int32_t>::max()
66 #define IGN_INT32_MIN std::numeric_limits<int32_t>::min()
70 #define IGN_INT32_LOW std::numeric_limits<int32_t>::lowest()
76 #define IGN_PI_2 M_PI_2
77 #define IGN_PI_4 M_PI_4
79 #define IGN_PI 3.14159265358979323846
80 #define IGN_PI_2 1.57079632679489661923
81 #define IGN_PI_4 0.78539816339744830962
87 #if defined __FLT_EVAL_METHOD__ && __FLT_EVAL_METHOD__ == 2
88 #define IGN_FP_VOLATILE volatile
90 #define IGN_FP_VOLATILE
99 static const double NAN_D = std::numeric_limits<double>::quiet_NaN();
102 static const float NAN_F = std::numeric_limits<float>::quiet_NaN();
105 static const int NAN_I = std::numeric_limits<int>::quiet_NaN();
112 inline T
clamp(T _v, T _min, T _max)
138 return isnan(_v) || std::isinf(_v) ? 0.0f : _v;
146 return isnan(_v) || std::isinf(_v) ? 0.0 : _v;
160 inline bool isEven(
const unsigned int _v)
170 return (_v % 2) != 0;
176 inline bool isOdd(
const unsigned int _v)
178 return (_v % 2) != 0;
185 inline T
mean(
const std::vector<T> &_values)
188 for (
unsigned int i = 0; i < _values.size(); ++i)
190 return sum / _values.size();
199 T avg = mean<T>(_values);
202 for (
unsigned int i = 0; i < _values.size(); ++i)
203 sum += (_values[i] - avg) * (_values[i] - avg);
204 return sum / _values.size();
211 inline T
max(
const std::vector<T> &_values)
214 for (
unsigned int i = 0; i < _values.size(); ++i)
215 if (_values[i] > max)
224 inline T
min(
const std::vector<T> &_values)
227 for (
unsigned int i = 0; i < _values.size(); ++i)
228 if (_values[i] < min)
238 inline bool equal(
const T &_a,
const T &_b,
239 const T &_epsilon = 1e-6)
242 return diff <= _epsilon;
250 inline T
precision(
const T &_a,
const unsigned int &_precision)
252 return std::round(_a * pow(10, _precision)) / pow(10, _precision);
260 return ((_x != 0) && ((_x & (~_x + 1)) == _x));
276 while (_x & (_x - 1))
289 const char *p = _input.c_str();
290 if (!*p || *p ==
'?')
304 while (*p >=
'0' && *p <=
'9')
305 acc = acc * 10 + *p++ -
'0';
309 std::cerr <<
"Invalid int numeric format[" << _input <<
"]\n";
313 return static_cast<int>(s * acc);
322 const char *p = _input.c_str();
323 if (!*p || *p ==
'?')
336 while (*p >=
'0' && *p <=
'9')
337 acc = acc * 10 + *p++ -
'0';
343 while (*p >=
'0' && *p <=
'9')
345 acc += (*p++ -
'0') * k;
364 while (*p >=
'0' && *p <=
'9')
365 f = f * 10 + *p++ -
'0';
367 acc *= pow(10, f*es);
372 std::cerr <<
"Invalid double numeric format[" << _input <<
"]\n";
388 #if defined _WIN32 || defined __CYGWIN__
391 #define IGNITION_VISIBLE __attribute__ ((dllexport))
393 #define IGNITION_VISIBLE __declspec(dllexport)
397 #define IGNITION_VISIBLE __attribute__ ((dllimport))
399 #define IGNITION_VISIBLE __declspec(dllimport)
402 #define IGNITION_HIDDEN
405 #define IGNITION_VISIBLE __attribute__ ((visibility ("default")))
406 #define IGNITION_HIDDEN __attribute__ ((visibility ("hidden")))
408 #define IGNITION_VISIBLE
409 #define IGNITION_HIDDEN
static const float NAN_F
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:102
static const double NAN_D
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:99
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition: Helpers.hh:250
T mean(const std::vector< T > &_values)
get mean of vector of values
Definition: Helpers.hh:185
bool isnan(float _v)
check if a float is NaN
Definition: Helpers.hh:120
unsigned int roundUpPowerOfTwo(unsigned int _x)
Get the smallest power of two that is greater or equal to a given value.
Definition: Helpers.hh:268
T max(const std::vector< T > &_values)
get the maximum value of vector of values
Definition: Helpers.hh:211
bool isOdd(const int _v)
Check if parameter is odd.
Definition: Helpers.hh:168
T variance(const std::vector< T > &_values)
get variance of vector of values
Definition: Helpers.hh:197
#define IGN_FP_VOLATILE
Define IGN_FP_VOLATILE for FP equality comparisons Use volatile parameters when checking floating poi...
Definition: Helpers.hh:90
bool isPowerOfTwo(unsigned int _x)
Is this a power of 2?
Definition: Helpers.hh:258
bool isEven(const int _v)
Check if parameter is even.
Definition: Helpers.hh:152
static const int NAN_I
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:105
double parseFloat(const std::string &_input)
parse string into float
Definition: Helpers.hh:320
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:238
float fixnan(float _v)
Fix a nan value.
Definition: Helpers.hh:136
int parseInt(const std::string &_input)
parse string into an integer
Definition: Helpers.hh:287
T min(const std::vector< T > &_values)
get the minimum value of vector of values
Definition: Helpers.hh:224
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:112