17#ifndef GZ_MATH_FUNCTIONS_HH_
18#define GZ_MATH_FUNCTIONS_HH_
31#include <gz/math/config.hh>
32#include "gz/math/Export.hh"
49#define GZ_SQRT2 M_SQRT2
51#define GZ_PI 3.14159265358979323846
52#define GZ_PI_2 1.57079632679489661923
53#define GZ_PI_4 0.78539816339744830962
54#define GZ_SQRT2 1.41421356237309504880
60#if defined __FLT_EVAL_METHOD__ && __FLT_EVAL_METHOD__ == 2
61#define GZ_FP_VOLATILE volatile
68#define GZ_SPHERE_VOLUME(_radius) (4.0*GZ_PI*std::pow(_radius, 3)/3.0)
73#define GZ_CYLINDER_VOLUME(_r, _l) (_l * GZ_PI * std::pow(_r, 2))
79#define GZ_BOX_VOLUME(_x, _y, _z) (_x *_y * _z)
83#define GZ_BOX_VOLUME_V(_v) (_v.X() *_v.Y() * _v.Z())
91 inline namespace GZ_MATH_VERSION_NAMESPACE {
124 constexpr auto GZ_DEPRECATED(7) IGN_ZERO_SIZE_T = &GZ_ZERO_SIZE_T;
125 constexpr auto GZ_DEPRECATED(7) IGN_ONE_SIZE_T = &GZ_ONE_SIZE_T;
126 constexpr auto GZ_DEPRECATED(7) IGN_TWO_SIZE_T = &GZ_TWO_SIZE_T;
127 constexpr auto GZ_DEPRECATED(7) IGN_THREE_SIZE_T = &GZ_THREE_SIZE_T;
128 constexpr auto GZ_DEPRECATED(7) IGN_FOUR_SIZE_T = &GZ_FOUR_SIZE_T;
129 constexpr auto GZ_DEPRECATED(7) IGN_FIVE_SIZE_T = &GZ_FIVE_SIZE_T;
130 constexpr auto GZ_DEPRECATED(7) IGN_SIX_SIZE_T = &GZ_SIX_SIZE_T;
131 constexpr auto GZ_DEPRECATED(7) IGN_SEVEN_SIZE_T = &GZ_SEVEN_SIZE_T;
132 constexpr auto GZ_DEPRECATED(7) IGN_EIGHT_SIZE_T = &GZ_EIGHT_SIZE_T;
133 constexpr auto GZ_DEPRECATED(7) IGN_NINE_SIZE_T = &GZ_NINE_SIZE_T;
136 static const
double MAX_D =
std::numeric_limits<
double>::max();
139 static const
double MIN_D =
std::numeric_limits<
double>::min();
142 static const
double LOW_D =
std::numeric_limits<
double>::lowest();
145 static const
double INF_D =
std::numeric_limits<
double>::infinity();
148 static const
double NAN_D =
std::numeric_limits<
double>::quiet_NaN();
151 static const
float MAX_F =
std::numeric_limits<
float>::max();
154 static const
float MIN_F =
std::numeric_limits<
float>::min();
157 static const
float LOW_F =
std::numeric_limits<
float>::lowest();
160 static const
float INF_F =
std::numeric_limits<
float>::infinity();
163 static const
float NAN_F =
std::numeric_limits<
float>::quiet_NaN();
166 static const uint16_t MAX_UI16 =
std::numeric_limits<uint16_t>::max();
169 static const uint16_t MIN_UI16 =
std::numeric_limits<uint16_t>::min();
173 static const uint16_t LOW_UI16 =
std::numeric_limits<uint16_t>::lowest();
176 static const uint16_t INF_UI16 =
std::numeric_limits<uint16_t>::infinity();
179 static const int16_t MAX_I16 =
std::numeric_limits<int16_t>::max();
182 static const int16_t MIN_I16 =
std::numeric_limits<int16_t>::min();
186 static const int16_t LOW_I16 =
std::numeric_limits<int16_t>::lowest();
189 static const int16_t INF_I16 =
std::numeric_limits<int16_t>::infinity();
192 static const uint32_t MAX_UI32 =
std::numeric_limits<uint32_t>::max();
195 static const uint32_t MIN_UI32 =
std::numeric_limits<uint32_t>::min();
199 static const uint32_t LOW_UI32 =
std::numeric_limits<uint32_t>::lowest();
202 static const uint32_t INF_UI32 =
std::numeric_limits<uint32_t>::infinity();
205 static const int32_t MAX_I32 =
std::numeric_limits<int32_t>::max();
208 static const int32_t MIN_I32 =
std::numeric_limits<int32_t>::min();
212 static const int32_t LOW_I32 =
std::numeric_limits<int32_t>::lowest();
215 static const int32_t INF_I32 =
std::numeric_limits<int32_t>::infinity();
218 static const uint64_t MAX_UI64 =
std::numeric_limits<uint64_t>::max();
221 static const uint64_t MIN_UI64 =
std::numeric_limits<uint64_t>::min();
225 static const uint64_t LOW_UI64 =
std::numeric_limits<uint64_t>::lowest();
228 static const uint64_t INF_UI64 =
std::numeric_limits<uint64_t>::infinity();
231 static const int64_t MAX_I64 =
std::numeric_limits<int64_t>::max();
234 static const int64_t MIN_I64 =
std::numeric_limits<int64_t>::min();
238 static const int64_t LOW_I64 =
std::numeric_limits<int64_t>::lowest();
241 static const int64_t INF_I64 =
std::numeric_limits<int64_t>::infinity();
244 static const
int NAN_I =
std::numeric_limits<
int>::quiet_NaN();
254 inline T clamp(T _v, T _min, T _max)
262 inline bool isnan(
float _v)
270 inline bool isnan(
double _v)
280 return isnan(_v) ||
std::isinf(_v) ? 0.0f : _v;
288 return isnan(_v) ||
std::isinf(_v) ? 0.0 : _v;
302 inline bool isEven(
const unsigned int _v)
312 return (_v % 2) != 0;
318 inline bool isOdd(
const unsigned int _v)
320 return (_v % 2) != 0;
332 return (T(0) < _value) - (_value < T(0));
354 for (
unsigned int i = 0; i < _values.
size(); ++i)
356 return sum /
static_cast<T
>(_values.
size());
365 T avg = mean<T>(_values);
368 for (
unsigned int i = 0; i < _values.
size(); ++i)
369 sum += (_values[i] - avg) * (_values[i] - avg);
370 return sum /
static_cast<T
>(_values.
size());
397 inline bool equal(
const T &_a,
const T &_b,
398 const T &_epsilon = T(1e-6))
401 return diff <= _epsilon;
411 const T &_epsilon = 1e-6)
413 return _a < _b + _epsilon;
423 const T &_epsilon = 1e-6)
425 return _a > _b - _epsilon;
433 inline T
precision(
const T &_a,
const unsigned int &_precision)
436 return static_cast<T
>(
std::round(_a * p) / p);
459 inline void sort3(T &_a, T &_b, T &_c)
499 return ((_x != 0) && ((_x & (~_x + 1)) == _x));
515 while (_x & (_x - 1))
538 int remainder = std::abs(_num) % _multiple;
543 return -(std::abs(_num) - remainder);
545 return _num + _multiple - remainder;
565 const std::chrono::steady_clock::time_point &_time);
573 std::chrono::steady_clock::time_point GZ_MATH_VISIBLE
575 const uint64_t &_sec,
const uint64_t &_nanosec);
584 const uint64_t &_sec,
const uint64_t &_nanosec);
592 const std::chrono::steady_clock::duration &_dur);
605 template<
class...Durations,
class DurationIn>
608 using discard =
int[];
609 (void)discard{0, (void((
610 (std::get<Durations>(retval) =
611 std::chrono::duration_cast<Durations>(d)),
612 (d -= std::chrono::duration_cast<DurationIn>(
613 std::get<Durations>(retval))))), 0)...};
621 const std::chrono::steady_clock::time_point &_point);
627 const std::chrono::steady_clock::duration &_duration);
639 uint64_t & numberDays, uint64_t & numberHours,
640 uint64_t & numberMinutes, uint64_t & numberSeconds,
641 uint64_t & numberMilliseconds);
650 uint64_t d, h, m, s, ms;
669 std::chrono::steady_clock::time_point
676#if defined _MSC_VER || defined __arm__