17 #ifndef _IGNITION_MATH_FUNCTIONS_HH_
18 #define _IGNITION_MATH_FUNCTIONS_HH_
20 #ifndef _USE_MATH_DEFINES
21 # define _USE_MATH_DEFINES
32 #define IGN_DBL_MAX std::numeric_limits<double>::max()
35 #define IGN_DBL_MIN std::numeric_limits<double>::min()
38 #define IGN_DBL_LOW std::numeric_limits<double>::lowest()
41 #define IGN_DBL_INF std::numeric_limits<double>::infinity()
44 #define IGN_FLT_MAX std::numeric_limits<float>::max()
47 #define IGN_FLT_MIN std::numeric_limits<float>::min()
50 #define IGN_FLT_LOW std::numeric_limits<float>::lowest()
53 #define IGN_UINT32_MAX std::numeric_limits<uint32_t>::max()
56 #define IGN_UINT32_MIN std::numeric_limits<uint32_t>::min()
60 #define IGN_UINT32_LOW std::numeric_limits<uint32_t>::lowest()
63 #define IGN_INT32_MAX std::numeric_limits<int32_t>::max()
66 #define IGN_INT32_MIN std::numeric_limits<int32_t>::min()
70 #define IGN_INT32_LOW std::numeric_limits<int32_t>::lowest()
76 #define IGN_PI_2 M_PI_2
77 #define IGN_PI_4 M_PI_4
79 #define IGN_PI 3.14159265358979323846
80 #define IGN_PI_2 1.57079632679489661923
81 #define IGN_PI_4 0.78539816339744830962
87 #if defined __FLT_EVAL_METHOD__ && __FLT_EVAL_METHOD__ == 2
88 #define IGN_FP_VOLATILE volatile
90 #define IGN_FP_VOLATILE
95 #define IGN_SPHERE_VOLUME(_radius) (4.0*IGN_PI*std::pow(_radius, 3)/3.0)
100 #define IGN_CYLINDER_VOLUME(_r, _l) (_l * IGN_PI * std::pow(_r, 2))
106 #define IGN_BOX_VOLUME(_x, _y, _z) (_x *_y * _z)
110 #define IGN_BOX_VOLUME_V(_v) (_v.X() *_v.Y() * _v.Z())
118 static const double NAN_D = std::numeric_limits<double>::quiet_NaN();
121 static const float NAN_F = std::numeric_limits<float>::quiet_NaN();
124 static const int NAN_I = std::numeric_limits<int>::quiet_NaN();
131 inline T
clamp(T _v, T _min, T _max)
157 return isnan(_v) || std::isinf(_v) ? 0.0f : _v;
165 return isnan(_v) || std::isinf(_v) ? 0.0 : _v;
179 inline bool isEven(
const unsigned int _v)
189 return (_v % 2) != 0;
195 inline bool isOdd(
const unsigned int _v)
197 return (_v % 2) != 0;
204 inline T
mean(
const std::vector<T> &_values)
207 for (
unsigned int i = 0; i < _values.size(); ++i)
209 return sum / _values.size();
218 T avg = mean<T>(_values);
221 for (
unsigned int i = 0; i < _values.size(); ++i)
222 sum += (_values[i] - avg) * (_values[i] - avg);
223 return sum / _values.size();
230 inline T
max(
const std::vector<T> &_values)
233 for (
unsigned int i = 0; i < _values.size(); ++i)
234 if (_values[i] > max)
243 inline T
min(
const std::vector<T> &_values)
246 for (
unsigned int i = 0; i < _values.size(); ++i)
247 if (_values[i] < min)
257 inline bool equal(
const T &_a,
const T &_b,
258 const T &_epsilon = 1e-6)
261 return diff <= _epsilon;
269 inline T
precision(
const T &_a,
const unsigned int &_precision)
271 return std::round(_a * std::pow(10, _precision))
272 / std::pow(10, _precision);
291 inline void sort3(T &_a, T &_b, T &_c)
306 return ((_x != 0) && ((_x & (~_x + 1)) == _x));
322 while (_x & (_x - 1))
335 const char *p = _input.c_str();
336 if (!*p || *p ==
'?')
350 while (*p >=
'0' && *p <=
'9')
351 acc = acc * 10 + *p++ -
'0';
355 std::cerr <<
"Invalid int numeric format[" << _input <<
"]\n";
359 return static_cast<int>(s * acc);
368 const char *p = _input.c_str();
369 if (!*p || *p ==
'?')
382 while (*p >=
'0' && *p <=
'9')
383 acc = acc * 10 + *p++ -
'0';
389 while (*p >=
'0' && *p <=
'9')
391 acc += (*p++ -
'0') * k;
410 while (*p >=
'0' && *p <=
'9')
411 f = f * 10 + *p++ -
'0';
413 acc *= pow(10, f*es);
418 std::cerr <<
"Invalid double numeric format[" << _input <<
"]\n";
434 #if defined _WIN32 || defined __CYGWIN__
437 #define IGNITION_VISIBLE __attribute__ ((dllexport))
439 #define IGNITION_VISIBLE __declspec(dllexport)
443 #define IGNITION_VISIBLE __attribute__ ((dllimport))
445 #define IGNITION_VISIBLE __declspec(dllimport)
448 #define IGNITION_HIDDEN
451 #define IGNITION_VISIBLE __attribute__ ((visibility ("default")))
452 #define IGNITION_HIDDEN __attribute__ ((visibility ("hidden")))
454 #define IGNITION_VISIBLE
455 #define IGNITION_HIDDEN
static const float NAN_F
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:121
static const double NAN_D
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:118
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition: Helpers.hh:269
T mean(const std::vector< T > &_values)
get mean of vector of values
Definition: Helpers.hh:204
bool isnan(float _v)
check if a float is NaN
Definition: Helpers.hh:139
unsigned int roundUpPowerOfTwo(unsigned int _x)
Get the smallest power of two that is greater or equal to a given value.
Definition: Helpers.hh:314
T max(const std::vector< T > &_values)
get the maximum value of vector of values
Definition: Helpers.hh:230
bool isOdd(const int _v)
Check if parameter is odd.
Definition: Helpers.hh:187
T variance(const std::vector< T > &_values)
get variance of vector of values
Definition: Helpers.hh:216
#define IGN_FP_VOLATILE
Define IGN_FP_VOLATILE for FP equality comparisons Use volatile parameters when checking floating poi...
Definition: Helpers.hh:90
bool isPowerOfTwo(unsigned int _x)
Is this a power of 2?
Definition: Helpers.hh:304
bool isEven(const int _v)
Check if parameter is even.
Definition: Helpers.hh:171
static const int NAN_I
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:124
double parseFloat(const std::string &_input)
parse string into float
Definition: Helpers.hh:366
void sort2(T &_a, T &_b)
Sort two numbers, such that _a <= _b.
Definition: Helpers.hh:279
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:257
float fixnan(float _v)
Fix a nan value.
Definition: Helpers.hh:155
int parseInt(const std::string &_input)
parse string into an integer
Definition: Helpers.hh:333
T min(const std::vector< T > &_values)
get the minimum value of vector of values
Definition: Helpers.hh:243
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:131
bool isnan(double _v)
check if a double is NaN
Definition: Helpers.hh:147
void sort3(T &_a, T &_b, T &_c)
Sort three numbers, such that _a <= _b <= _c.
Definition: Helpers.hh:291