17 #ifndef _IGNITION_MATH_FUNCTIONS_HH_
18 #define _IGNITION_MATH_FUNCTIONS_HH_
20 #define _USE_MATH_DEFINES
29 #define IGN_DBL_MAX std::numeric_limits<double>::max()
32 #define IGN_DBL_MIN std::numeric_limits<double>::min()
35 #define IGN_FLT_MAX std::numeric_limits<float>::max()
38 #define IGN_FLT_MIN std::numeric_limits<float>::min()
41 #define IGN_UINT32_MAX std::numeric_limits<uint32_t>::max()
44 #define IGN_UINT32_MIN std::numeric_limits<uint32_t>::min()
47 #define IGN_INT32_MAX std::numeric_limits<int32_t>::max()
50 #define IGN_INT32_MIN std::numeric_limits<int32_t>::min()
56 #define IGN_PI_2 M_PI_2
57 #define IGN_PI_4 M_PI_4
59 #define IGN_PI 3.14159265358979323846
60 #define IGN_PI_2 1.57079632679489661923
61 #define IGN_PI_4 0.78539816339744830962
70 static const double NAN_D = std::numeric_limits<double>::quiet_NaN();
73 static const float NAN_F = std::numeric_limits<float>::quiet_NaN();
76 static const int NAN_I = std::numeric_limits<int>::quiet_NaN();
83 inline T
clamp(T _v, T _min, T _max)
109 return isnan(_v) || std::isinf(_v) ? 0.0f : _v;
117 return isnan(_v) || std::isinf(_v) ? 0.0 : _v;
124 inline T
mean(
const std::vector<T> &_values)
127 for (
unsigned int i = 0; i < _values.size(); ++i)
129 return sum / _values.size();
138 T avg = mean<T>(_values);
141 for (
unsigned int i = 0; i < _values.size(); ++i)
142 sum += (_values[i] - avg) * (_values[i] - avg);
143 return sum / _values.size();
150 inline T
max(
const std::vector<T> &_values)
153 for (
unsigned int i = 0; i < _values.size(); ++i)
154 if (_values[i] > max)
163 inline T
min(
const std::vector<T> &_values)
166 for (
unsigned int i = 0; i < _values.size(); ++i)
167 if (_values[i] < min)
177 inline bool equal(
const T &_a,
const T &_b,
178 const T &_epsilon = 1e-6)
180 return std::abs(_a - _b) <= _epsilon;
188 inline T
precision(
const T &_a,
const unsigned int &_precision)
190 return std::round(_a * pow(10, _precision)) / pow(10, _precision);
198 return ((_x != 0) && ((_x & (~_x + 1)) == _x));
214 while (_x & (_x - 1))
227 const char *p = _input.c_str();
228 if (!*p || *p ==
'?')
242 while (*p >=
'0' && *p <=
'9')
243 acc = acc * 10 + *p++ -
'0';
247 std::cerr <<
"Invalid int numeric format[" << _input <<
"]\n";
251 return static_cast<int>(s * acc);
260 const char *p = _input.c_str();
261 if (!*p || *p ==
'?')
274 while (*p >=
'0' && *p <=
'9')
275 acc = acc * 10 + *p++ -
'0';
281 while (*p >=
'0' && *p <=
'9')
283 acc += (*p++ -
'0') * k;
302 while (*p >=
'0' && *p <=
'9')
303 f = f * 10 + *p++ -
'0';
305 acc *= pow(10, f*es);
310 std::cerr <<
"Invalid double numeric format[" << _input <<
"]\n";
326 #if defined _WIN32 || defined __CYGWIN__
329 #define IGNITION_VISIBLE __attribute__ ((dllexport))
331 #define IGNITION_VISIBLE __declspec(dllexport)
335 #define IGNITION_VISIBLE __attribute__ ((dllimport))
337 #define IGNITION_VISIBLE __declspec(dllimport)
340 #define IGNITION_HIDDEN
343 #define IGNITION_VISIBLE __attribute__ ((visibility ("default")))
344 #define IGNITION_HIDDEN __attribute__ ((visibility ("hidden")))
346 #define IGNITION_VISIBLE
347 #define IGNITION_HIDDEN
static const float NAN_F
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:73
static const double NAN_D
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:70
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition: Helpers.hh:188
T mean(const std::vector< T > &_values)
get mean of vector of values
Definition: Helpers.hh:124
bool isnan(float _v)
check if a float is NaN
Definition: Helpers.hh:91
unsigned int roundUpPowerOfTwo(unsigned int _x)
Get the smallest power of two that is greater or equal to a given value.
Definition: Helpers.hh:206
T max(const std::vector< T > &_values)
get the maximum value of vector of values
Definition: Helpers.hh:150
T variance(const std::vector< T > &_values)
get variance of vector of values
Definition: Helpers.hh:136
bool isPowerOfTwo(unsigned int _x)
Is this a power of 2?
Definition: Helpers.hh:196
static const int NAN_I
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:76
double parseFloat(const std::string &_input)
parse string into float
Definition: Helpers.hh:258
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:177
float fixnan(float _v)
Fix a nan value.
Definition: Helpers.hh:107
int parseInt(const std::string &_input)
parse string into an integer
Definition: Helpers.hh:225
T min(const std::vector< T > &_values)
get the minimum value of vector of values
Definition: Helpers.hh:163
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:83
bool isnan(double _v)
check if a double is NaN
Definition: Helpers.hh:99