123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- #ifndef _NUMERICTOOLS_FBASICS_H
- #define _NUMERICTOOLS_FBASICS_H
- #define _USE_MATH_DEFINES
- #include <cmath>
- #ifdef WIN32
- #include <math.h>
- #endif
- #include "CrossplatformDefines.h"
- #ifdef NICE_BOOST_FOUND
- #include <boost/math/special_functions/fpclassify.hpp> // isnan
- #endif
- #include <stdlib.h>
- #include <limits>
- #include <string>
- #ifdef LIMUN_AIBO_MODE
-
- inline float roundf(float x) {
- return floor(x + 0.5);
- }
- inline double round(double x) {
- return floor(x + 0.5);
- }
- inline long double roundl(long double x) {
- return floor(x + 0.5);
- }
- inline long lroundf(float x) {
- return (long) floor(x + 0.5);
- }
- inline long lround(double x) {
- return (long) floor(x + 0.5);
- }
- inline long lroundl(long double x) {
- return (long) floor(x + 0.5);
- }
- #endif
- namespace NICE {
- template<class T>
- inline bool isZero(const T& x) {
- return x == (T)0;
- }
- inline bool isZero(float x, float tolerance) {
- return fabs(x) < tolerance;
- }
- inline bool isZero(double x, double tolerance) {
- return fabs(x) < tolerance;
- }
- template<>
- inline bool isZero(const float& x) {
- return isZero(x, std::numeric_limits<float>::epsilon());
- }
- template<>
- inline bool isZero(const double& x) {
- return isZero(x, std::numeric_limits<double>::epsilon());
- }
- inline bool almostZero(const double a) {
- return (fabs(a)<1e-15);
- }
- inline bool isEqual(float x, float y, float tolerance) {
- return fabs(x - y) < tolerance;
- }
- inline bool isEqual(double x, double y, double tolerance) {
- return fabs(x - y) < tolerance;
- }
- inline bool isEqual(float x, float y) {
- return isEqual(x, y, std::numeric_limits<float>::epsilon());
- }
- inline bool isEqual(double x, double y) {
- return isEqual(x, y, std::numeric_limits<double>::epsilon());
- }
- template<class T>
- inline T sign(const T& x) {
- if (isZero(x)) {
- return (T)0;
- } else if (x > (T)0) {
- return (T)1;
- } else {
- return (T)(-1);
- }
- }
- template<class T>
- inline T absolute(const T& x) {
-
- if (x >= T(0)) {
- return x;
- } else {
- return -x;
- }
- }
- template<>
- inline float absolute(const float& x) {
- return (float) fabs(x);
- }
- template<>
- inline double absolute(const double& x) {
- return (double) fabs(x);
- }
- template<class T>
- inline T imposeSign(const T& x, const T& s) {
- return absolute(x) * sign(s);
- }
- template<class T>
- inline T square(const T& t) {
- return t * t;
- }
- template<class T>
- inline T cube(const T& t) {
- return t * t * t;
- }
- inline double cubeRoot(const double& t) {
- return sign(t) * pow(fabs(t), 1.0 / 3.0);
- }
- inline bool isNaN(double x) {
-
- #ifdef NICE_BOOST_FOUND
- return boost::math::isnan(x);
- #else
- #if (__GNUC__ > 3)
- return isnan(x);
- #else
- return x != x;
- #endif
- #endif
- }
- inline bool isNaN(float x) {
- #ifdef NICE_BOOST_FOUND
- return boost::math::isnan(x);
- #else
- #if (__GNUC__ > 3)
- return isnan(x);
- #else
- return x != x;
- #endif
- #endif
- }
- inline bool isFinite(double x)
- {
- #ifdef WIN32
- #ifdef NICE_BOOST_FOUND
- return boost::math::isfinite(x);
- #else
- NICE_ERROR("isFinite() not defined (and neither is boost found for compensation...)")
- #endif
- #else
- return finite(x);
- #endif
- }
- inline double doubleNaN() {
- double zero = 1.0;
- zero -= zero;
- return 0.0 / zero;
- }
- inline float floatNaN() {
- float zero = 1.0f;
- zero -= zero;
- return 0.0f / zero;
- }
- template<class T>
- inline const T& limitValue(const T& value, const T& min, const T& max) {
- if (value < min) {
- return min;
- } else if (value > max) {
- return max;
- } else {
- return value;
- }
- }
- inline double degreeToRadian(double a) {
- return a * M_PI / 180.0;
- }
- inline double radianToDegree(double a) {
- return a / M_PI * 180.0;
- }
- inline double normalizeAngle(double a) {
- const double TWO_PI = 2.0 * M_PI;
- return a - floor(a / TWO_PI) * TWO_PI;
- }
- void initRand( bool fixedSeed = false, unsigned int seed = 0 );
- inline int randInt(const int limit) {
- if (limit == 0) {
- return 0;
- } else {
- return rand() % limit;
- }
- }
- inline double randDouble() {
- return ((double)rand() / ((double)(RAND_MAX)+1.0));
- }
- inline double randDouble(const double limit) {
- return ((double)rand() / ((double)(RAND_MAX)+1.0)) * limit;
- }
- inline float randFloat() {
- return ((float)rand() / ((float)(RAND_MAX)+1.0f));
- }
- inline float randFloat(const float limit) {
- return ((float)rand() / ((float)(RAND_MAX)+1.0f)) * limit;
- }
- inline double randGaussDouble ( const double stddev ) {
-
- double r1, r2, d;
- do {
- r1 = 2.0 * randDouble() - 1.0;
- r2 = 2.0 * randDouble() - 1.0;
- d = r1*r1 + r2*r2;
- } while ((d >= 1.0)||(isZero(d)));
- d = sqrt((-2.0 * log(d))/d);
- double y1 = r1*d;
-
- return y1*stddev;
- }
- inline double nanToInf(double x) {
- if (isNaN(x)) {
- return std::numeric_limits<double>::infinity();
- } else {
- return x;
- }
- }
- double stringToDouble(const char* s);
- inline double stringToDouble(std::string s) {
- return stringToDouble(s.c_str());
- }
- inline double readDouble(std::istream& str) {
- std::string s;
- str >> s;
- return stringToDouble(s);
- }
- long stringToInt(const char* s);
- inline long stringToInt(std::string s) {
- return stringToInt(s.c_str());
- }
- std::string intToString(const int i);
- std::string doubleToString(const double d, const unsigned int digits = 6,
- const bool fixedPoint = false);
- inline int roundInt(double d) {
- return int(round(d));
- }
- inline int roundInt(float d) {
- return int(roundf(d));
- }
- }
- #endif
|