17 #ifndef IGNITION_MATH_FUNCTIONS_HH_ 18 #define IGNITION_MATH_FUNCTIONS_HH_ 30 #define IGN_DBL_MAX std::numeric_limits<double>::max() 33 #define IGN_DBL_MIN std::numeric_limits<double>::min() 36 #define IGN_DBL_LOW std::numeric_limits<double>::lowest() 39 #define IGN_DBL_INF std::numeric_limits<double>::infinity() 42 #define IGN_FLT_MAX std::numeric_limits<float>::max() 45 #define IGN_FLT_MIN std::numeric_limits<float>::min() 48 #define IGN_FLT_LOW std::numeric_limits<float>::lowest() 51 #define IGN_FLT_INF std::numeric_limits<float>::infinity() 54 #define IGN_UINT16_MAX std::numeric_limits<uint16_t>::max() 57 #define IGN_UINT16_MIN std::numeric_limits<uint16_t>::min() 61 #define IGN_UINT16_LOW std::numeric_limits<uint16_t>::lowest() 64 #define IGN_UINT16_INF std::numeric_limits<uint16_t>::infinity() 67 #define IGN_INT16_MAX std::numeric_limits<int16_t>::max() 70 #define IGN_INT16_MIN std::numeric_limits<int16_t>::min() 74 #define IGN_INT16_LOW std::numeric_limits<int16_t>::lowest() 77 #define IGN_INT16_INF std::numeric_limits<int16_t>::infinity() 80 #define IGN_UINT32_MAX std::numeric_limits<uint32_t>::max() 83 #define IGN_UINT32_MIN std::numeric_limits<uint32_t>::min() 87 #define IGN_UINT32_LOW std::numeric_limits<uint32_t>::lowest() 90 #define IGN_UINT32_INF std::numeric_limits<uint32_t>::infinity() 93 #define IGN_INT32_MAX std::numeric_limits<int32_t>::max() 96 #define IGN_INT32_MIN std::numeric_limits<int32_t>::min() 100 #define IGN_INT32_LOW std::numeric_limits<int32_t>::lowest() 103 #define IGN_INT32_INF std::numeric_limits<int32_t>::infinity() 106 #define IGN_UINT64_MAX std::numeric_limits<uint64_t>::max() 109 #define IGN_UINT64_MIN std::numeric_limits<uint64_t>::min() 113 #define IGN_UINT64_LOW std::numeric_limits<uint64_t>::lowest() 116 #define IGN_UINT64_INF std::numeric_limits<uint64_t>::infinity() 119 #define IGN_INT64_MAX std::numeric_limits<int64_t>::max() 122 #define IGN_INT64_MIN std::numeric_limits<int64_t>::min() 126 #define IGN_INT64_LOW std::numeric_limits<int64_t>::lowest() 129 #define IGN_INT64_INF std::numeric_limits<int64_t>::infinity() 135 #define IGN_PI_2 M_PI_2 136 #define IGN_PI_4 M_PI_4 137 #define IGN_SQRT2 M_SQRT2 139 #define IGN_PI 3.14159265358979323846 140 #define IGN_PI_2 1.57079632679489661923 141 #define IGN_PI_4 0.78539816339744830962 142 #define IGN_SQRT2 1.41421356237309504880 148 #if defined __FLT_EVAL_METHOD__ && __FLT_EVAL_METHOD__ == 2 149 #define IGN_FP_VOLATILE volatile 151 #define IGN_FP_VOLATILE 156 #define IGN_SPHERE_VOLUME(_radius) (4.0*IGN_PI*std::pow(_radius, 3)/3.0) 161 #define IGN_CYLINDER_VOLUME(_r, _l) (_l * IGN_PI * std::pow(_r, 2)) 167 #define IGN_BOX_VOLUME(_x, _y, _z) (_x *_y * _z) 171 #define IGN_BOX_VOLUME_V(_v) (_v.X() *_v.Y() * _v.Z()) 181 #if defined _WIN32 || defined __CYGWIN__ 184 #define IGNITION_VISIBLE __attribute__ ((dllexport)) 186 #define IGNITION_VISIBLE __declspec(dllexport) 190 #define IGNITION_VISIBLE __attribute__ ((dllimport)) 192 #define IGNITION_VISIBLE __declspec(dllimport) 195 #define IGNITION_HIDDEN 198 #define IGNITION_VISIBLE __attribute__ ((visibility ("default"))) 199 #define IGNITION_HIDDEN __attribute__ ((visibility ("hidden"))) 201 #define IGNITION_VISIBLE 202 #define IGNITION_HIDDEN 212 static const double NAN_D = std::numeric_limits<double>::quiet_NaN();
215 static const float NAN_F = std::numeric_limits<float>::quiet_NaN();
218 static const int NAN_I = std::numeric_limits<int>::quiet_NaN();
225 inline T
clamp(T _v, T _min, T _max)
251 return isnan(_v) || std::isinf(_v) ? 0.0f : _v;
259 return isnan(_v) || std::isinf(_v) ? 0.0 : _v;
273 inline bool isEven(
const unsigned int _v)
283 return (_v % 2) != 0;
289 inline bool isOdd(
const unsigned int _v)
291 return (_v % 2) != 0;
298 inline T
mean(
const std::vector<T> &_values)
301 for (
unsigned int i = 0; i < _values.size(); ++i)
303 return sum / _values.size();
312 T avg = mean<T>(_values);
315 for (
unsigned int i = 0; i < _values.size(); ++i)
316 sum += (_values[i] - avg) * (_values[i] - avg);
317 return sum / _values.size();
324 inline T
max(
const std::vector<T> &_values)
327 for (
unsigned int i = 0; i < _values.size(); ++i)
328 if (_values[i] > max)
337 inline T
min(
const std::vector<T> &_values)
340 for (
unsigned int i = 0; i < _values.size(); ++i)
341 if (_values[i] < min)
351 inline bool equal(
const T &_a,
const T &_b,
352 const T &_epsilon = 1e-6)
355 return diff <= _epsilon;
363 inline T
precision(
const T &_a,
const unsigned int &_precision)
365 auto p = std::pow(10, _precision);
366 return static_cast<T
>(std::round(_a * p) / p);
385 inline void sort3(T &_a, T &_b, T &_c)
400 return ((_x != 0) && ((_x & (~_x + 1)) == _x));
416 while (_x & (_x - 1))
429 const char *p = _input.c_str();
430 if (!*p || *p ==
'?')
444 while (*p >=
'0' && *p <=
'9')
445 acc = acc * 10 + *p++ -
'0';
449 std::cerr <<
"Invalid int numeric format[" << _input <<
"]\n";
462 const char *p = _input.c_str();
463 if (!*p || *p ==
'?')
476 while (*p >=
'0' && *p <=
'9')
477 acc = acc * 10 + *p++ -
'0';
483 while (*p >=
'0' && *p <=
'9')
485 acc += (*p++ -
'0') * k;
504 while (*p >=
'0' && *p <=
'9')
505 f = f * 10 + *p++ -
'0';
507 acc *= pow(10, f*es);
512 std::cerr <<
"Invalid double numeric format[" << _input <<
"]\n";
523 #if defined _MSC_VER || defined __arm__ static const float NAN_F
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:215
std::tuple< PairInput, PairInput > Unpair(const PairOutput _key)
The reverse of the Pair function.
#define IGNITION_VISIBLE
Use to represent "symbol visible" if supported.
Definition: Helpers.hh:201
static const double NAN_D
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:212
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition: Helpers.hh:363
T mean(const std::vector< T > &_values)
get mean of vector of values
Definition: Helpers.hh:298
bool isnan(float _v)
check if a float is NaN
Definition: Helpers.hh:233
unsigned int roundUpPowerOfTwo(unsigned int _x)
Get the smallest power of two that is greater or equal to a given value.
Definition: Helpers.hh:408
T max(const std::vector< T > &_values)
get the maximum value of vector of values
Definition: Helpers.hh:324
uint64_t PairOutput
Definition: Helpers.hh:528
bool isOdd(const int _v)
Check if parameter is odd.
Definition: Helpers.hh:281
T variance(const std::vector< T > &_values)
get variance of vector of values
Definition: Helpers.hh:310
#define IGN_FP_VOLATILE
Define IGN_FP_VOLATILE for FP equality comparisons Use volatile parameters when checking floating poi...
Definition: Helpers.hh:151
bool isPowerOfTwo(unsigned int _x)
Is this a power of 2?
Definition: Helpers.hh:398
PairOutput Pair(const PairInput _a, const PairInput _b)
A pairing function that maps two values to a unique third value.
uint32_t PairInput
Definition: Helpers.hh:527
bool isEven(const int _v)
Check if parameter is even.
Definition: Helpers.hh:265
static const int NAN_I
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:218
double parseFloat(const std::string &_input)
parse string into float
Definition: Helpers.hh:460
void sort2(T &_a, T &_b)
Sort two numbers, such that _a <= _b.
Definition: Helpers.hh:373
Definition: AffineException.hh:30
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:351
float fixnan(float _v)
Fix a nan value.
Definition: Helpers.hh:249
int parseInt(const std::string &_input)
parse string into an integer
Definition: Helpers.hh:427
T min(const std::vector< T > &_values)
get the minimum value of vector of values
Definition: Helpers.hh:337
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:225
bool isnan(double _v)
check if a double is NaN
Definition: Helpers.hh:241
void sort3(T &_a, T &_b, T &_c)
Sort three numbers, such that _a <= _b <= _c.
Definition: Helpers.hh:385