17#ifndef IGNITION_MATH_FUNCTIONS_HH_
18#define IGNITION_MATH_FUNCTIONS_HH_
29#include <ignition/math/config.hh>
30#include "ignition/math/Export.hh"
34#define IGN_DBL_MAX ignition::math::DPRCT_MAX_D
38#define IGN_DBL_MIN ignition::math::DPRCT_MIN_D
42#define IGN_DBL_LOW ignition::math::DPRCT_LOW_D
46#define IGN_DBL_INF ignition::math::DPRCT_INF_D
50#define IGN_FLT_MAX ignition::math::DPRCT_MAX_F
54#define IGN_FLT_MIN ignition::math::DPRCT_MIN_F
58#define IGN_FLT_LOW ignition::math::DPRCT_LOW_F
62#define IGN_FLT_INF ignition::math::DPRCT_INF_F
66#define IGN_UINT16_MAX ignition::math::DPRCT_MAX_UI16
70#define IGN_UINT16_MIN ignition::math::DPRCT_MIN_UI16
75#define IGN_UINT16_LOW ignition::math::DPRCT_LOW_UI16
79#define IGN_UINT16_INF ignition::math::DPRCT_INF_UI16
83#define IGN_INT16_MAX ignition::math::DPRCT_MAX_I16
87#define IGN_INT16_MIN ignition::math::DPRCT_MIN_I16
92#define IGN_INT16_LOW ignition::math::DPRCT_LOW_I16
96#define IGN_INT16_INF ignition::math::DPRCT_INF_I16
100#define IGN_UINT32_MAX ignition::math::DPRCT_MAX_UI32
104#define IGN_UINT32_MIN ignition::math::DPRCT_MIN_UI32
109#define IGN_UINT32_LOW ignition::math::DPRCT_LOW_UI32
113#define IGN_UINT32_INF ignition::math::DPRCT_INF_UI32
117#define IGN_INT32_MAX ignition::math::DPRCT_MAX_I32
121#define IGN_INT32_MIN ignition::math::DPRCT_MIN_I32
126#define IGN_INT32_LOW ignition::math::DPRCT_LOW_I32
130#define IGN_INT32_INF ignition::math::DPRCT_INF_I32
134#define IGN_UINT64_MAX ignition::math::DPRCT_MAX_UI64
138#define IGN_UINT64_MIN ignition::math::DPRCT_MIN_UI64
143#define IGN_UINT64_LOW ignition::math::DPRCT_LOW_UI64
147#define IGN_UINT64_INF ignition::math::DPRCT_INF_UI64
151#define IGN_INT64_MAX ignition::math::DPRCT_MAX_I64
155#define IGN_INT64_MIN ignition::math::DPRCT_MIN_I64
160#define IGN_INT64_LOW ignition::math::DPRCT_LOW_I64
164#define IGN_INT64_INF ignition::math::DPRCT_INF_I64
170#define IGN_PI_2 M_PI_2
171#define IGN_PI_4 M_PI_4
172#define IGN_SQRT2 M_SQRT2
174#define IGN_PI 3.14159265358979323846
175#define IGN_PI_2 1.57079632679489661923
176#define IGN_PI_4 0.78539816339744830962
177#define IGN_SQRT2 1.41421356237309504880
183#if defined __FLT_EVAL_METHOD__ && __FLT_EVAL_METHOD__ == 2
184#define IGN_FP_VOLATILE volatile
186#define IGN_FP_VOLATILE
191#define IGN_SPHERE_VOLUME(_radius) (4.0*IGN_PI*std::pow(_radius, 3)/3.0)
196#define IGN_CYLINDER_VOLUME(_r, _l) (_l * IGN_PI * std::pow(_r, 2))
202#define IGN_BOX_VOLUME(_x, _y, _z) (_x *_y * _z)
206#define IGN_BOX_VOLUME_V(_v) (_v.X() *_v.Y() * _v.Z())
213 inline namespace IGNITION_MATH_VERSION_NAMESPACE
246 static const double MAX_D = std::numeric_limits<double>::max();
249 static const double MIN_D = std::numeric_limits<double>::min();
252 static const double LOW_D = std::numeric_limits<double>::lowest();
255 static const double INF_D = std::numeric_limits<double>::infinity();
258 static const double NAN_D = std::numeric_limits<double>::quiet_NaN();
261 static const float MAX_F = std::numeric_limits<float>::max();
264 static const float MIN_F = std::numeric_limits<float>::min();
267 static const float LOW_F = std::numeric_limits<float>::lowest();
270 static const float INF_F = std::numeric_limits<float>::infinity();
273 static const float NAN_F = std::numeric_limits<float>::quiet_NaN();
276 static const uint16_t
MAX_UI16 = std::numeric_limits<uint16_t>::max();
279 static const uint16_t
MIN_UI16 = std::numeric_limits<uint16_t>::min();
283 static const uint16_t
LOW_UI16 = std::numeric_limits<uint16_t>::lowest();
286 static const uint16_t
INF_UI16 = std::numeric_limits<uint16_t>::infinity();
289 static const int16_t
MAX_I16 = std::numeric_limits<int16_t>::max();
292 static const int16_t
MIN_I16 = std::numeric_limits<int16_t>::min();
296 static const int16_t
LOW_I16 = std::numeric_limits<int16_t>::lowest();
299 static const int16_t
INF_I16 = std::numeric_limits<int16_t>::infinity();
302 static const uint32_t
MAX_UI32 = std::numeric_limits<uint32_t>::max();
305 static const uint32_t
MIN_UI32 = std::numeric_limits<uint32_t>::min();
309 static const uint32_t
LOW_UI32 = std::numeric_limits<uint32_t>::lowest();
312 static const uint32_t
INF_UI32 = std::numeric_limits<uint32_t>::infinity();
315 static const int32_t
MAX_I32 = std::numeric_limits<int32_t>::max();
318 static const int32_t
MIN_I32 = std::numeric_limits<int32_t>::min();
322 static const int32_t
LOW_I32 = std::numeric_limits<int32_t>::lowest();
325 static const int32_t
INF_I32 = std::numeric_limits<int32_t>::infinity();
328 static const uint64_t
MAX_UI64 = std::numeric_limits<uint64_t>::max();
331 static const uint64_t
MIN_UI64 = std::numeric_limits<uint64_t>::min();
335 static const uint64_t
LOW_UI64 = std::numeric_limits<uint64_t>::lowest();
338 static const uint64_t
INF_UI64 = std::numeric_limits<uint64_t>::infinity();
341 static const int64_t
MAX_I64 = std::numeric_limits<int64_t>::max();
344 static const int64_t
MIN_I64 = std::numeric_limits<int64_t>::min();
348 static const int64_t
LOW_I64 = std::numeric_limits<int64_t>::lowest();
351 static const int64_t
INF_I64 = std::numeric_limits<int64_t>::infinity();
354 static const int NAN_I = std::numeric_limits<int>::quiet_NaN();
395 inline T
clamp(T _v, T _min, T _max)
397 return std::max(std::min(_v, _max), _min);
405 return (std::isnan)(_v);
413 return (std::isnan)(_v);
421 return isnan(_v) || std::isinf(_v) ? 0.0f : _v;
429 return isnan(_v) || std::isinf(_v) ? 0.0 : _v;
443 inline bool isEven(
const unsigned int _v)
453 return (_v % 2) != 0;
459 inline bool isOdd(
const unsigned int _v)
461 return (_v % 2) != 0;
473 return (T(0) < _value) - (_value < T(0));
492 inline T
mean(
const std::vector<T> &_values)
495 for (
unsigned int i = 0; i < _values.size(); ++i)
497 return sum / _values.size();
509 for (
unsigned int i = 0; i < _values.size(); ++i)
510 sum += (_values[i] - avg) * (_values[i] - avg);
511 return sum / _values.size();
518 inline T
max(
const std::vector<T> &_values)
520 T
max = std::numeric_limits<T>::min();
521 for (
unsigned int i = 0; i < _values.size(); ++i)
522 if (_values[i] >
max)
531 inline T
min(
const std::vector<T> &_values)
533 T
min = std::numeric_limits<T>::max();
534 for (
unsigned int i = 0; i < _values.size(); ++i)
535 if (_values[i] <
min)
545 inline bool equal(
const T &_a,
const T &_b,
546 const T &_epsilon = T(1e-6))
549 return diff <= _epsilon;
558 const T &_epsilon = 1e-6)
560 return _a < _b + _epsilon;
569 const T &_epsilon = 1e-6)
571 return _a > _b - _epsilon;
579 inline T
precision(
const T &_a,
const unsigned int &_precision)
581 auto p = std::pow(10, _precision);
582 return static_cast<T
>(std::round(_a * p) / p);
601 inline void sort3(T &_a, T &_b, T &_c)
616 return ((_x != 0) && ((_x & (~_x + 1)) == _x));
632 while (_x & (_x - 1))
651 else if (_input.find_first_not_of(
' ') == std::string::npos)
659 return std::stoi(_input);
680 else if (_input.find_first_not_of(
' ') == std::string::npos)
688 return std::stod(_input);
702#if defined _MSC_VER || defined __arm__
733 std::tuple<PairInput, PairInput> IGNITION_MATH_VISIBLE
Unpair(
#define IGN_FP_VOLATILE
Define IGN_FP_VOLATILE for FP equality comparisons Use volatile parameters when checking floating poi...
Definition Helpers.hh:186
static const double NAN_D
Returns the representation of a quiet not a number (NAN)
Definition Helpers.hh:258
void sort3(T &_a, T &_b, T &_c)
Sort three numbers, such that _a <= _b <= _c.
Definition Helpers.hh:601
static const size_t IGN_SIX_SIZE_T
size_t type with a value of 6
Definition Helpers.hh:234
static const size_t IGN_EIGHT_SIZE_T
size_t type with a value of 8
Definition Helpers.hh:240
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition Helpers.hh:579
bool isnan(float _v)
check if a float is NaN
Definition Helpers.hh:403
static const double MAX_D
Double maximum value. This value will be similar to 1.79769e+308.
Definition Helpers.hh:246
static const uint32_t LOW_UI32
32bit unsigned integer lowest value.
Definition Helpers.hh:309
static const uint16_t MIN_UI16
16bit unsigned integer minimum value
Definition Helpers.hh:279
static const int16_t MAX_I16
16bit unsigned integer maximum value
Definition Helpers.hh:289
static const size_t IGN_NINE_SIZE_T
size_t type with a value of 9
Definition Helpers.hh:243
T variance(const std::vector< T > &_values)
get variance of vector of values
Definition Helpers.hh:504
int parseInt(const std::string &_input)
parse string into an integer
Definition Helpers.hh:643
static const double LOW_D
Double low value, equivalent to -MAX_D.
Definition Helpers.hh:252
int signum(T _value)
The signum function.
Definition Helpers.hh:483
static const uint16_t LOW_UI16
16bit unsigned integer lowest value.
Definition Helpers.hh:283
static const size_t IGN_THREE_SIZE_T
size_t type with a value of 3
Definition Helpers.hh:225
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition Helpers.hh:395
static const float INF_F
float positive infinite value
Definition Helpers.hh:270
static const uint64_t LOW_UI64
64bit unsigned integer lowest value.
Definition Helpers.hh:335
static const uint64_t MAX_UI64
64bit unsigned integer maximum value
Definition Helpers.hh:328
static const size_t IGN_TWO_SIZE_T
size_t type with a value of 2
Definition Helpers.hh:222
static const int NAN_I
Returns the representation of a quiet not a number (NAN)
Definition Helpers.hh:354
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:545
static const double MIN_D
Double min value. This value will be similar to 2.22507e-308.
Definition Helpers.hh:249
static const uint16_t INF_UI16
16-bit unsigned integer positive infinite value
Definition Helpers.hh:286
float fixnan(float _v)
Fix a nan value.
Definition Helpers.hh:419
uint64_t PairOutput
Definition Helpers.hh:707
static const uint64_t MIN_UI64
64bit unsigned integer minimum value
Definition Helpers.hh:331
T min(const std::vector< T > &_values)
get the minimum value of vector of values
Definition Helpers.hh:531
static const float MAX_F
Float maximum value. This value will be similar to 3.40282e+38.
Definition Helpers.hh:261
static const double IGN_DEPRECATED(3) DPRCT_MAX_D
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition Helpers.hh:216
T mean(const std::vector< T > &_values)
get mean of vector of values
Definition Helpers.hh:492
static const int16_t LOW_I16
16bit unsigned integer lowest value.
Definition Helpers.hh:296
static const int64_t MAX_I64
64bit unsigned integer maximum value
Definition Helpers.hh:341
static const float NAN_F
Returns the representation of a quiet not a number (NAN)
Definition Helpers.hh:273
static const int16_t INF_I16
16-bit unsigned integer positive infinite value
Definition Helpers.hh:299
static const int32_t MAX_I32
32bit unsigned integer maximum value
Definition Helpers.hh:315
bool lessOrNearEqual(const T &_a, const T &_b, const T &_epsilon=1e-6)
inequality test, within a tolerance
Definition Helpers.hh:557
static const uint16_t MAX_UI16
16bit unsigned integer maximum value
Definition Helpers.hh:276
unsigned int roundUpPowerOfTwo(unsigned int _x)
Get the smallest power of two that is greater or equal to a given value.
Definition Helpers.hh:624
bool isPowerOfTwo(unsigned int _x)
Is this a power of 2?
Definition Helpers.hh:614
static const int64_t MIN_I64
64bit unsigned integer minimum value
Definition Helpers.hh:344
static const int32_t MIN_I32
32bit unsigned integer minimum value
Definition Helpers.hh:318
static const uint32_t MIN_UI32
32bit unsigned integer minimum value
Definition Helpers.hh:305
static const double INF_D
Double positive infinite value.
Definition Helpers.hh:255
static const size_t IGN_SEVEN_SIZE_T
size_t type with a value of 7
Definition Helpers.hh:237
static const int32_t INF_I32
32-bit unsigned integer positive infinite value
Definition Helpers.hh:325
static const int16_t MIN_I16
16bit unsigned integer minimum value
Definition Helpers.hh:292
double parseFloat(const std::string &_input)
parse string into float
Definition Helpers.hh:672
std::tuple< PairInput, PairInput > IGNITION_MATH_VISIBLE Unpair(const PairOutput _key)
The reverse of the Pair function.
static const int64_t LOW_I64
64bit unsigned integer lowest value.
Definition Helpers.hh:348
static const int32_t LOW_I32
32bit unsigned integer lowest value.
Definition Helpers.hh:322
static const size_t IGN_FIVE_SIZE_T
size_t type with a value of 5
Definition Helpers.hh:231
static const uint32_t INF_UI32
32-bit unsigned integer positive infinite value
Definition Helpers.hh:312
bool isOdd(const int _v)
Check if parameter is odd.
Definition Helpers.hh:451
PairOutput IGNITION_MATH_VISIBLE Pair(const PairInput _a, const PairInput _b)
A pairing function that maps two values to a unique third value.
static const uint64_t INF_UI64
64-bit unsigned integer positive infinite value
Definition Helpers.hh:338
static const uint32_t MAX_UI32
32bit unsigned integer maximum value
Definition Helpers.hh:302
static const size_t IGN_ONE_SIZE_T
size_t type with a value of 1
Definition Helpers.hh:219
static const float MIN_F
Float minimum value. This value will be similar to 1.17549e-38.
Definition Helpers.hh:264
bool isEven(const int _v)
Check if parameter is even.
Definition Helpers.hh:435
bool greaterOrNearEqual(const T &_a, const T &_b, const T &_epsilon=1e-6)
inequality test, within a tolerance
Definition Helpers.hh:568
T max(const std::vector< T > &_values)
get the maximum value of vector of values
Definition Helpers.hh:518
int sgn(T _value)
The signum function.
Definition Helpers.hh:471
void sort2(T &_a, T &_b)
Sort two numbers, such that _a <= _b.
Definition Helpers.hh:589
static const float LOW_F
Float low value, equivalent to -MAX_F.
Definition Helpers.hh:267
uint32_t PairInput
Definition Helpers.hh:706
static const int64_t INF_I64
64-bit unsigned integer positive infinite value
Definition Helpers.hh:351
static const size_t IGN_FOUR_SIZE_T
size_t type with a value of 4
Definition Helpers.hh:228