17 #ifndef IGNITION_MATH_FUNCTIONS_HH_ 18 #define IGNITION_MATH_FUNCTIONS_HH_ 33 #define IGN_DBL_MAX ignition::math::DPRCT_MAX_D 37 #define IGN_DBL_MIN ignition::math::DPRCT_MIN_D 41 #define IGN_DBL_LOW ignition::math::DPRCT_LOW_D 45 #define IGN_DBL_INF ignition::math::DPRCT_INF_D 49 #define IGN_FLT_MAX ignition::math::DPRCT_MAX_F 53 #define IGN_FLT_MIN ignition::math::DPRCT_MIN_F 57 #define IGN_FLT_LOW ignition::math::DPRCT_LOW_F 61 #define IGN_FLT_INF ignition::math::DPRCT_INF_F 65 #define IGN_UINT16_MAX ignition::math::DPRCT_MAX_UI16 69 #define IGN_UINT16_MIN ignition::math::DPRCT_MIN_UI16 74 #define IGN_UINT16_LOW ignition::math::DPRCT_LOW_UI16 78 #define IGN_UINT16_INF ignition::math::DPRCT_INF_UI16 82 #define IGN_INT16_MAX ignition::math::DPRCT_MAX_I16 86 #define IGN_INT16_MIN ignition::math::DPRCT_MIN_I16 91 #define IGN_INT16_LOW ignition::math::DPRCT_LOW_I16 95 #define IGN_INT16_INF ignition::math::DPRCT_INF_I16 99 #define IGN_UINT32_MAX ignition::math::DPRCT_MAX_UI32 103 #define IGN_UINT32_MIN ignition::math::DPRCT_MIN_UI32 108 #define IGN_UINT32_LOW ignition::math::DPRCT_LOW_UI32 112 #define IGN_UINT32_INF ignition::math::DPRCT_INF_UI32 116 #define IGN_INT32_MAX ignition::math::DPRCT_MAX_I32 120 #define IGN_INT32_MIN ignition::math::DPRCT_MIN_I32 125 #define IGN_INT32_LOW ignition::math::DPRCT_LOW_I32 129 #define IGN_INT32_INF ignition::math::DPRCT_INF_I32 133 #define IGN_UINT64_MAX ignition::math::DPRCT_MAX_UI64 137 #define IGN_UINT64_MIN ignition::math::DPRCT_MIN_UI64 142 #define IGN_UINT64_LOW ignition::math::DPRCT_LOW_UI64 146 #define IGN_UINT64_INF ignition::math::DPRCT_INF_UI64 150 #define IGN_INT64_MAX ignition::math::DPRCT_MAX_I64 154 #define IGN_INT64_MIN ignition::math::DPRCT_MIN_I64 159 #define IGN_INT64_LOW ignition::math::DPRCT_LOW_I64 163 #define IGN_INT64_INF ignition::math::DPRCT_INF_I64 169 #define IGN_PI_2 M_PI_2 170 #define IGN_PI_4 M_PI_4 171 #define IGN_SQRT2 M_SQRT2 173 #define IGN_PI 3.14159265358979323846 174 #define IGN_PI_2 1.57079632679489661923 175 #define IGN_PI_4 0.78539816339744830962 176 #define IGN_SQRT2 1.41421356237309504880 182 #if defined __FLT_EVAL_METHOD__ && __FLT_EVAL_METHOD__ == 2 183 #define IGN_FP_VOLATILE volatile 185 #define IGN_FP_VOLATILE 190 #define IGN_SPHERE_VOLUME(_radius) (4.0*IGN_PI*std::pow(_radius, 3)/3.0) 195 #define IGN_CYLINDER_VOLUME(_r, _l) (_l * IGN_PI * std::pow(_r, 2)) 201 #define IGN_BOX_VOLUME(_x, _y, _z) (_x *_y * _z) 205 #define IGN_BOX_VOLUME_V(_v) (_v.X() *_v.Y() * _v.Z()) 249 static const double LOW_D = std::numeric_limits<double>::lowest();
252 static const double INF_D = std::numeric_limits<double>::infinity();
255 static const double NAN_D = std::numeric_limits<double>::quiet_NaN();
264 static const float LOW_F = std::numeric_limits<float>::lowest();
267 static const float INF_F = std::numeric_limits<float>::infinity();
270 static const float NAN_F = std::numeric_limits<float>::quiet_NaN();
280 static const uint16_t
LOW_UI16 = std::numeric_limits<uint16_t>::lowest();
283 static const uint16_t
INF_UI16 = std::numeric_limits<uint16_t>::infinity();
293 static const int16_t
LOW_I16 = std::numeric_limits<int16_t>::lowest();
296 static const int16_t
INF_I16 = std::numeric_limits<int16_t>::infinity();
306 static const uint32_t
LOW_UI32 = std::numeric_limits<uint32_t>::lowest();
309 static const uint32_t
INF_UI32 = std::numeric_limits<uint32_t>::infinity();
319 static const int32_t
LOW_I32 = std::numeric_limits<int32_t>::lowest();
322 static const int32_t
INF_I32 = std::numeric_limits<int32_t>::infinity();
332 static const uint64_t
LOW_UI64 = std::numeric_limits<uint64_t>::lowest();
335 static const uint64_t
INF_UI64 = std::numeric_limits<uint64_t>::infinity();
345 static const int64_t
LOW_I64 = std::numeric_limits<int64_t>::lowest();
348 static const int64_t
INF_I64 = std::numeric_limits<int64_t>::infinity();
351 static const int NAN_I = std::numeric_limits<int>::quiet_NaN();
362 static const uint16_t
IGN_DEPRECATED(2) DPRCT_MAX_UI16 = MAX_UI16;
363 static const uint16_t
IGN_DEPRECATED(2) DPRCT_MIN_UI16 = MIN_UI16;
364 static const uint16_t
IGN_DEPRECATED(2) DPRCT_LOW_UI16 = LOW_UI16;
365 static const uint16_t
IGN_DEPRECATED(2) DPRCT_INF_UI16 = INF_UI16;
370 static const uint32_t
IGN_DEPRECATED(2) DPRCT_MAX_UI32 = MAX_UI32;
371 static const uint32_t
IGN_DEPRECATED(2) DPRCT_MIN_UI32 = MIN_UI32;
372 static const uint32_t
IGN_DEPRECATED(2) DPRCT_LOW_UI32 = LOW_UI32;
373 static const uint32_t
IGN_DEPRECATED(2) DPRCT_INF_UI32 = INF_UI32;
378 static const uint64_t
IGN_DEPRECATED(2) DPRCT_MAX_UI64 = MAX_UI64;
379 static const uint64_t
IGN_DEPRECATED(2) DPRCT_MIN_UI64 = MIN_UI64;
380 static const uint64_t
IGN_DEPRECATED(2) DPRCT_LOW_UI64 = LOW_UI64;
381 static const uint64_t
IGN_DEPRECATED(2) DPRCT_INF_UI64 = INF_UI64;
392 inline T
clamp(T _v, T _min, T _max)
418 return isnan(_v) || std::isinf(_v) ? 0.0f : _v;
426 return isnan(_v) || std::isinf(_v) ? 0.0 : _v;
440 inline bool isEven(
const unsigned int _v)
450 return (_v % 2) != 0;
456 inline bool isOdd(
const unsigned int _v)
458 return (_v % 2) != 0;
470 return (T(0) < _value) - (_value < T(0));
489 inline T
mean(
const std::vector<T> &_values)
492 for (
unsigned int i = 0; i < _values.size(); ++i)
494 return sum / _values.size();
503 T avg = mean<T>(_values);
506 for (
unsigned int i = 0; i < _values.size(); ++i)
507 sum += (_values[i] - avg) * (_values[i] - avg);
508 return sum / _values.size();
515 inline T
max(
const std::vector<T> &_values)
518 for (
unsigned int i = 0; i < _values.size(); ++i)
519 if (_values[i] > max)
528 inline T
min(
const std::vector<T> &_values)
531 for (
unsigned int i = 0; i < _values.size(); ++i)
532 if (_values[i] < min)
542 inline bool equal(
const T &_a,
const T &_b,
543 const T &_epsilon = T(1e-6))
546 return diff <= _epsilon;
555 const T &_epsilon = 1e-6)
557 return _a < _b + _epsilon;
566 const T &_epsilon = 1e-6)
568 return _a > _b - _epsilon;
576 inline T
precision(
const T &_a,
const unsigned int &_precision)
578 auto p = std::pow(10, _precision);
579 return static_cast<T
>(std::round(_a * p) / p);
598 inline void sort3(T &_a, T &_b, T &_c)
613 return ((_x != 0) && ((_x & (~_x + 1)) == _x));
629 while (_x & (_x - 1))
642 const char *p = _input.c_str();
643 if (!*p || *p ==
'?')
657 while (*p >=
'0' && *p <=
'9')
658 acc = acc * 10 + *p++ -
'0';
662 std::cerr <<
"Invalid int numeric format[" << _input <<
"]\n";
675 const char *p = _input.c_str();
676 if (!*p || *p ==
'?')
689 while (*p >=
'0' && *p <=
'9')
690 acc = acc * 10 + *p++ -
'0';
696 while (*p >=
'0' && *p <=
'9')
698 acc += (*p++ -
'0') * k;
717 while (*p >=
'0' && *p <=
'9')
718 f = f * 10 + *p++ -
'0';
720 acc *= pow(10, f*es);
725 std::cerr <<
"Invalid double numeric format[" << _input <<
"]\n";
736 #if defined _MSC_VER || defined __arm__ static const float NAN_F
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:270
static const size_t IGN_ONE_SIZE_T
size_t type with a value of 1
Definition: Helpers.hh:216
std::tuple< PairInput, PairInput > IGNITION_VISIBLE Unpair(const PairOutput _key)
The reverse of the Pair function.
static const uint16_t MAX_UI16
16bit unsigned integer maximum value
Definition: Helpers.hh:273
static const size_t IGN_SIX_SIZE_T
size_t type with a value of 6
Definition: Helpers.hh:231
static const uint16_t MIN_UI16
16bit unsigned integer minimum value
Definition: Helpers.hh:276
static const double NAN_D
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:255
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition: Helpers.hh:576
static const uint16_t INF_UI16
16-bit unsigned integer positive infinite value
Definition: Helpers.hh:283
T mean(const std::vector< T > &_values)
get mean of vector of values
Definition: Helpers.hh:489
static const size_t IGN_THREE_SIZE_T
size_t type with a value of 3
Definition: Helpers.hh:222
static const int32_t INF_I32
32-bit unsigned integer positive infinite value
Definition: Helpers.hh:322
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition: Helpers.hh:213
bool isnan(float _v)
check if a float is NaN
Definition: Helpers.hh:400
static const uint32_t MIN_UI32
32bit unsigned integer minimum value
Definition: Helpers.hh:302
unsigned int roundUpPowerOfTwo(unsigned int _x)
Get the smallest power of two that is greater or equal to a given value.
Definition: Helpers.hh:621
static const size_t IGN_EIGHT_SIZE_T
size_t type with a value of 8
Definition: Helpers.hh:237
T max(const std::vector< T > &_values)
get the maximum value of vector of values
Definition: Helpers.hh:515
static const double MAX_D
Double maximum value. This value will be similar to 1.79769e+308.
Definition: Helpers.hh:243
uint64_t PairOutput
Definition: Helpers.hh:741
bool equal(const T &_a, const T &_b, const T &_epsilon=T(1e-6))
check if two values are equal, within a tolerance
Definition: Helpers.hh:542
static const int32_t MIN_I32
32bit unsigned integer minimum value
Definition: Helpers.hh:315
static const double INF_D
Double positive infinite value.
Definition: Helpers.hh:252
static const int16_t INF_I16
16-bit unsigned integer positive infinite value
Definition: Helpers.hh:296
static const int32_t LOW_I32
32bit unsigned integer lowest value.
Definition: Helpers.hh:319
bool isOdd(const int _v)
Check if parameter is odd.
Definition: Helpers.hh:448
static const uint32_t INF_UI32
32-bit unsigned integer positive infinite value
Definition: Helpers.hh:309
static const uint16_t LOW_UI16
16bit unsigned integer lowest value.
Definition: Helpers.hh:280
static const size_t IGN_NINE_SIZE_T
size_t type with a value of 9
Definition: Helpers.hh:240
bool greaterOrNearEqual(const T &_a, const T &_b, const T &_epsilon=1e-6)
inequality test, within a tolerance
Definition: Helpers.hh:565
T variance(const std::vector< T > &_values)
get variance of vector of values
Definition: Helpers.hh:501
static const int16_t MIN_I16
16bit unsigned integer minimum value
Definition: Helpers.hh:289
static const double MIN_D
Double min value. This value will be similar to 2.22507e-308.
Definition: Helpers.hh:246
static const float MIN_F
Float minimum value. This value will be similar to 1.17549e-38.
Definition: Helpers.hh:261
static const int32_t MAX_I32
32bit unsigned integer maximum value
Definition: Helpers.hh:312
#define IGN_FP_VOLATILE
Define IGN_FP_VOLATILE for FP equality comparisons Use volatile parameters when checking floating poi...
Definition: Helpers.hh:185
static const uint64_t LOW_UI64
64bit unsigned integer lowest value.
Definition: Helpers.hh:332
#define IGNITION_VISIBLE
Use to represent "symbol visible" if supported.
Definition: System.hh:59
bool isPowerOfTwo(unsigned int _x)
Is this a power of 2?
Definition: Helpers.hh:611
static const float LOW_F
Float low value, equivalent to -MAX_F.
Definition: Helpers.hh:264
static const uint32_t MAX_UI32
32bit unsigned integer maximum value
Definition: Helpers.hh:299
uint32_t PairInput
Definition: Helpers.hh:740
static const float MAX_F
Float maximum value. This value will be similar to 3.40282e+38.
Definition: Helpers.hh:258
static const size_t IGN_TWO_SIZE_T
size_t type with a value of 2
Definition: Helpers.hh:219
int sgn(T _value)
The signum function.
Definition: Helpers.hh:468
static const int16_t MAX_I16
16bit unsigned integer maximum value
Definition: Helpers.hh:286
int signum(T _value)
The signum function.
Definition: Helpers.hh:480
static const double IGN_DEPRECATED(2) DPRCT_MAX_D
PairOutput IGNITION_VISIBLE Pair(const PairInput _a, const PairInput _b)
A pairing function that maps two values to a unique third value.
bool isEven(const int _v)
Check if parameter is even.
Definition: Helpers.hh:432
static const int NAN_I
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:351
static const float INF_F
float positive infinite value
Definition: Helpers.hh:267
static const uint64_t INF_UI64
64-bit unsigned integer positive infinite value
Definition: Helpers.hh:335
static const size_t IGN_FOUR_SIZE_T
size_t type with a value of 4
Definition: Helpers.hh:225
static const int64_t INF_I64
64-bit unsigned integer positive infinite value
Definition: Helpers.hh:348
static const int64_t MAX_I64
64bit unsigned integer maximum value
Definition: Helpers.hh:338
double parseFloat(const std::string &_input)
parse string into float
Definition: Helpers.hh:673
void sort2(T &_a, T &_b)
Sort two numbers, such that _a <= _b.
Definition: Helpers.hh:586
static const uint64_t MIN_UI64
64bit unsigned integer minimum value
Definition: Helpers.hh:328
static const size_t IGN_FIVE_SIZE_T
size_t type with a value of 5
Definition: Helpers.hh:228
float fixnan(float _v)
Fix a nan value.
Definition: Helpers.hh:416
static const double LOW_D
Double low value, equivalent to -MAX_D.
Definition: Helpers.hh:249
static const uint64_t MAX_UI64
64bit unsigned integer maximum value
Definition: Helpers.hh:325
static const int64_t LOW_I64
64bit unsigned integer lowest value.
Definition: Helpers.hh:345
static const int64_t MIN_I64
64bit unsigned integer minimum value
Definition: Helpers.hh:341
int parseInt(const std::string &_input)
parse string into an integer
Definition: Helpers.hh:640
bool lessOrNearEqual(const T &_a, const T &_b, const T &_epsilon=1e-6)
inequality test, within a tolerance
Definition: Helpers.hh:554
T min(const std::vector< T > &_values)
get the minimum value of vector of values
Definition: Helpers.hh:528
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:392
bool isnan(double _v)
check if a double is NaN
Definition: Helpers.hh:408
static const uint32_t LOW_UI32
32bit unsigned integer lowest value.
Definition: Helpers.hh:306
static const size_t IGN_SEVEN_SIZE_T
size_t type with a value of 7
Definition: Helpers.hh:234
void sort3(T &_a, T &_b, T &_c)
Sort three numbers, such that _a <= _b <= _c.
Definition: Helpers.hh:598
static const int16_t LOW_I16
16bit unsigned integer lowest value.
Definition: Helpers.hh:293