Loading...
Searching...
No Matches
MATHS/common.h
Go to the documentation of this file.
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: common.h,v 1.29.16.1 2007/03/25 21:23:45 oliver Exp $
//
#ifndef BALL_MATHS_COMMON_H
#define BALL_MATHS_COMMON_H
#ifndef BALL_CONFIG_CONFIG_H
# include <BALL/CONFIG/config.h>
#endif
#include <cmath>
#ifdef BALL_HAS_IEEEFP_H
# include <ieeefp.h>
#endif
#ifndef BALL_COMMON_CONSTANTS_H
# include <BALL/COMMON/constants.h>
#endif
#ifndef BALL_COMMON_GLOBAL_H
# include <BALL/COMMON/global.h>
#endif
#ifndef BALL_COMMON_MACROS_H
# include <BALL/COMMON/macros.h>
#endif
namespace BALL
{
namespace Maths
{
template <typename T>
inline
T abs(const T& t)
{
return t >= 0 ? t : -t;
}
template <typename T>
inline
T frac(const T& t)
{
long tmp = (long)t;
return (t - (T)tmp);
}
#ifndef max
template <typename T>
inline
T max(const T& a, const T& b)
{
return a < b ? b : a;
}
template <typename T>
inline
T max(const T& a, const T& b, const T &ct)
{
return a > b ? max(a, ct) : max(b, ct);
}
#endif
#ifndef min
template <typename T>
inline
T min(const T& a, const T& b)
{
return a > b ? b : a;
}
template <typename T>
inline
T min(const T& a, const T& b, const T &ct)
{
return a < b ? min(a, ct) : min(b, ct);
}
#endif
template <typename T>
inline
T round(const T& t)
{
return (T)(t > 0 ? long(t + 0.5) : long(t - 0.5));
}
template <typename T>
inline
T sgn(const T& t)
{
return ((t < 0) ? -1 : (t == 0) ? 0 : 1);
}
template <typename T>
inline
bool isFinite(const T& t)
{
return std::isfinite(t);
}
template <typename T>
inline
bool isNan(const T& t)
{
#ifdef BALL_COMPILER_MSVC
return (_isnan(t) != 0);
#elif defined(BALL_OS_DARWIN)
return ( __inline_isnand(t) != 0);
#else
using std::isnan;
return (isnan(t) != 0);
#endif
}
template <typename T>
inline
bool isInfinite(const T& t)
{
return (!Maths::isFinite(t) && !Maths::isNan(t));
}
template <typename T>
inline
bool isZero(const T& t)
{
return (abs(t) < Constants::EPSILON);
}
template <typename T>
inline
bool isNotZero(const T& t)
{
return (abs(t) >= Constants::EPSILON);
}
template <typename T1, typename T2>
inline
bool isEqual(const T1& a, const T2& b)
{
return (abs(a - b) < Constants::EPSILON);
}
template <typename T1, typename T2>
inline
bool isNotEqual(const T1& a, const T2& b)
{
return (abs(a - b) >= Constants::EPSILON);
}
template <typename T1, typename T2>
inline
bool isLess(const T1& a, const T2& b)
{
return ((a - b) <= -Constants::EPSILON);
}
template <typename T1, typename T2>
inline
bool isLessOrEqual(const T1& a, const T2& b)
{
return ((a - b) < Constants::EPSILON);
}
template <typename T1, typename T2>
inline
bool isGreaterOrEqual(const T1& a, const T2& b)
{
return ((a - b) > -Constants::EPSILON);
}
template <typename T1, typename T2>
inline
bool isGreater(const T1& a, const T2& b)
{
return (a - b >= Constants::EPSILON);
}
template <typename T>
inline
long floor(const T& t)
{
return (long)(Maths::isGreater(t, 0) ? t: (Maths::isEqual(t, (T)(long)t) ? t : t - 1));
}
template <typename T>
inline
long ceiling(const T& t)
{
return (long)(Maths::isLess(t, 0) ? t: (Maths::isEqual(t, (T)(long)t) ? t : t + 1));
}
template <typename T1, typename T2>
inline
Index compare(const T1& a, const T2& b)
{
return (Maths::isLess(a, b) ? -1 : Maths::isEqual(a, b) ? 0 : 1);
}
template <typename T>
inline
bool isNear(const T& a, const T& b, const T& max_diff)
{
return (abs((double)a - (double)b) < abs((double)max_diff));
}
inline double rint(double x)
{
if (x < 0.0) return (double)(int)(x - 0.5);
else return (double)(int)(x + 0.5);
}
} // namespace Maths
} // namespace BALL
#endif // BALL_MATHS_COMMON_H
global.h
constants.h
macros.h
BALL::Constants::EPSILON
BALL_EXTERN_VARIABLE double EPSILON
Definition constants.h:43
BALL::Maths
Definition MATHS/common.h:36
BALL::Maths::isFinite
bool isFinite(const T &t)
Definition MATHS/common.h:149
BALL::Maths::isNear
bool isNear(const T &a, const T &b, const T &max_diff)
Definition MATHS/common.h:320
BALL::Maths::round
T round(const T &t)
Definition MATHS/common.h:127
BALL::Maths::isNan
bool isNan(const T &t)
Definition MATHS/common.h:160
BALL::Maths::max
T max(const T &a, const T &b)
Definition MATHS/common.h:75
BALL::Maths::frac
T frac(const T &t)
Definition MATHS/common.h:61
BALL::Maths::isNotZero
bool isNotZero(const T &t)
Definition MATHS/common.h:200
BALL::Maths::isNotEqual
bool isNotEqual(const T1 &a, const T2 &b)
Definition MATHS/common.h:224
BALL::Maths::isGreater
bool isGreater(const T1 &a, const T2 &b)
Definition MATHS/common.h:273
BALL::Maths::isGreaterOrEqual
bool isGreaterOrEqual(const T1 &a, const T2 &b)
Definition MATHS/common.h:261
BALL::Maths::isLess
bool isLess(const T1 &a, const T2 &b)
Definition MATHS/common.h:236
BALL::Maths::rint
double rint(double x)
round to integral value in floating-point format
Definition MATHS/common.h:327
BALL::Maths::ceiling
long ceiling(const T &t)
Definition MATHS/common.h:295
BALL::Maths::min
T min(const T &a, const T &b)
Definition MATHS/common.h:102
BALL::Maths::isZero
bool isZero(const T &t)
Definition MATHS/common.h:189
BALL::Maths::sgn
T sgn(const T &t)
Definition MATHS/common.h:138
BALL::Maths::floor
long floor(const T &t)
Definition MATHS/common.h:284
BALL::Maths::abs
T abs(const T &t)
Definition MATHS/common.h:50
BALL::Maths::isInfinite
bool isInfinite(const T &t)
Definition MATHS/common.h:178
BALL::Maths::isLessOrEqual
bool isLessOrEqual(const T1 &a, const T2 &b)
Definition MATHS/common.h:249
BALL::Maths::isEqual
bool isEqual(const T1 &a, const T2 &b)
Definition MATHS/common.h:212
BALL::Maths::compare
Index compare(const T1 &a, const T2 &b)
Definition MATHS/common.h:307
BALL
Definition constants.h:13
BALL::Index
BALL_INDEX_TYPE Index
Definition COMMON/global.h:105