Chris@16: // Copyright John Maddock 2006. Chris@16: // Use, modification and distribution are subject to the Chris@16: // Boost Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // Test real concept. Chris@16: Chris@16: // real_concept is an archetype for User defined Real types. Chris@16: Chris@16: // This file defines the features, constructors, operators, functions... Chris@16: // that are essential to use mathematical and statistical functions. Chris@16: // The template typename "RealType" is used where this type Chris@16: // (as well as the normal built-in types, float, double & long double) Chris@16: // can be used. Chris@16: // That this is the minimum set is confirmed by use as a type Chris@16: // in tests of all functions & distributions, for example: Chris@16: // test_spots(0.F); & test_spots(0.); for float and double, but also Chris@16: // test_spots(boost::math::concepts::real_concept(0.)); Chris@16: // NTL quad_float type is an example of a type meeting the requirements, Chris@16: // but note minor additions are needed - see ntl.diff and documentation Chris@16: // "Using With NTL - a High-Precision Floating-Point Library". Chris@16: Chris@16: #ifndef BOOST_MATH_REAL_CONCEPT_HPP Chris@16: #define BOOST_MATH_REAL_CONCEPT_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #if defined(__SGI_STL_PORT) Chris@16: # include Chris@16: #endif Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include // fmodl Chris@16: Chris@16: #if defined(__SGI_STL_PORT) || defined(_RWSTD_VER) || defined(__LIBCOMO__) Chris@16: # include Chris@16: #endif Chris@16: Chris@16: namespace boost{ namespace math{ Chris@16: Chris@16: namespace concepts Chris@16: { Chris@16: Chris@16: #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS Chris@16: typedef double real_concept_base_type; Chris@16: #else Chris@16: typedef long double real_concept_base_type; Chris@16: #endif Chris@16: Chris@16: class real_concept Chris@16: { Chris@16: public: Chris@16: // Constructors: Chris@16: real_concept() : m_value(0){} Chris@16: real_concept(char c) : m_value(c){} Chris@16: #ifndef BOOST_NO_INTRINSIC_WCHAR_T Chris@16: real_concept(wchar_t c) : m_value(c){} Chris@16: #endif Chris@16: real_concept(unsigned char c) : m_value(c){} Chris@16: real_concept(signed char c) : m_value(c){} Chris@16: real_concept(unsigned short c) : m_value(c){} Chris@16: real_concept(short c) : m_value(c){} Chris@16: real_concept(unsigned int c) : m_value(c){} Chris@16: real_concept(int c) : m_value(c){} Chris@16: real_concept(unsigned long c) : m_value(c){} Chris@16: real_concept(long c) : m_value(c){} Chris@16: #if defined(__DECCXX) || defined(__SUNPRO_CC) Chris@16: real_concept(unsigned long long c) : m_value(static_cast(c)){} Chris@16: real_concept(long long c) : m_value(static_cast(c)){} Chris@16: #elif defined(BOOST_HAS_LONG_LONG) Chris@16: real_concept(boost::ulong_long_type c) : m_value(static_cast(c)){} Chris@16: real_concept(boost::long_long_type c) : m_value(static_cast(c)){} Chris@16: #elif defined(BOOST_HAS_MS_INT64) Chris@16: real_concept(unsigned __int64 c) : m_value(static_cast(c)){} Chris@16: real_concept(__int64 c) : m_value(static_cast(c)){} Chris@16: #endif Chris@16: real_concept(float c) : m_value(c){} Chris@16: real_concept(double c) : m_value(c){} Chris@16: real_concept(long double c) : m_value(c){} Chris@16: #ifdef BOOST_MATH_USE_FLOAT128 Chris@101: real_concept(BOOST_MATH_FLOAT128_TYPE c) : m_value(c){} Chris@16: #endif Chris@16: Chris@16: // Assignment: Chris@16: real_concept& operator=(char c) { m_value = c; return *this; } Chris@16: real_concept& operator=(unsigned char c) { m_value = c; return *this; } Chris@16: real_concept& operator=(signed char c) { m_value = c; return *this; } Chris@16: #ifndef BOOST_NO_INTRINSIC_WCHAR_T Chris@16: real_concept& operator=(wchar_t c) { m_value = c; return *this; } Chris@16: #endif Chris@16: real_concept& operator=(short c) { m_value = c; return *this; } Chris@16: real_concept& operator=(unsigned short c) { m_value = c; return *this; } Chris@16: real_concept& operator=(int c) { m_value = c; return *this; } Chris@16: real_concept& operator=(unsigned int c) { m_value = c; return *this; } Chris@16: real_concept& operator=(long c) { m_value = c; return *this; } Chris@16: real_concept& operator=(unsigned long c) { m_value = c; return *this; } Chris@16: #ifdef BOOST_HAS_LONG_LONG Chris@16: real_concept& operator=(boost::long_long_type c) { m_value = static_cast(c); return *this; } Chris@16: real_concept& operator=(boost::ulong_long_type c) { m_value = static_cast(c); return *this; } Chris@16: #endif Chris@16: real_concept& operator=(float c) { m_value = c; return *this; } Chris@16: real_concept& operator=(double c) { m_value = c; return *this; } Chris@16: real_concept& operator=(long double c) { m_value = c; return *this; } Chris@16: Chris@16: // Access: Chris@16: real_concept_base_type value()const{ return m_value; } Chris@16: Chris@16: // Member arithmetic: Chris@16: real_concept& operator+=(const real_concept& other) Chris@16: { m_value += other.value(); return *this; } Chris@16: real_concept& operator-=(const real_concept& other) Chris@16: { m_value -= other.value(); return *this; } Chris@16: real_concept& operator*=(const real_concept& other) Chris@16: { m_value *= other.value(); return *this; } Chris@16: real_concept& operator/=(const real_concept& other) Chris@16: { m_value /= other.value(); return *this; } Chris@16: real_concept operator-()const Chris@16: { return -m_value; } Chris@16: real_concept const& operator+()const Chris@16: { return *this; } Chris@16: real_concept& operator++() Chris@16: { ++m_value; return *this; } Chris@16: real_concept& operator--() Chris@16: { --m_value; return *this; } Chris@16: Chris@16: private: Chris@16: real_concept_base_type m_value; Chris@16: }; Chris@16: Chris@16: // Non-member arithmetic: Chris@16: inline real_concept operator+(const real_concept& a, const real_concept& b) Chris@16: { Chris@16: real_concept result(a); Chris@16: result += b; Chris@16: return result; Chris@16: } Chris@16: inline real_concept operator-(const real_concept& a, const real_concept& b) Chris@16: { Chris@16: real_concept result(a); Chris@16: result -= b; Chris@16: return result; Chris@16: } Chris@16: inline real_concept operator*(const real_concept& a, const real_concept& b) Chris@16: { Chris@16: real_concept result(a); Chris@16: result *= b; Chris@16: return result; Chris@16: } Chris@16: inline real_concept operator/(const real_concept& a, const real_concept& b) Chris@16: { Chris@16: real_concept result(a); Chris@16: result /= b; Chris@16: return result; Chris@16: } Chris@16: Chris@16: // Comparison: Chris@16: inline bool operator == (const real_concept& a, const real_concept& b) Chris@16: { return a.value() == b.value(); } Chris@16: inline bool operator != (const real_concept& a, const real_concept& b) Chris@16: { return a.value() != b.value();} Chris@16: inline bool operator < (const real_concept& a, const real_concept& b) Chris@16: { return a.value() < b.value(); } Chris@16: inline bool operator <= (const real_concept& a, const real_concept& b) Chris@16: { return a.value() <= b.value(); } Chris@16: inline bool operator > (const real_concept& a, const real_concept& b) Chris@16: { return a.value() > b.value(); } Chris@16: inline bool operator >= (const real_concept& a, const real_concept& b) Chris@16: { return a.value() >= b.value(); } Chris@16: Chris@16: // Non-member functions: Chris@16: inline real_concept acos(real_concept a) Chris@16: { return std::acos(a.value()); } Chris@16: inline real_concept cos(real_concept a) Chris@16: { return std::cos(a.value()); } Chris@16: inline real_concept asin(real_concept a) Chris@16: { return std::asin(a.value()); } Chris@16: inline real_concept atan(real_concept a) Chris@16: { return std::atan(a.value()); } Chris@16: inline real_concept atan2(real_concept a, real_concept b) Chris@16: { return std::atan2(a.value(), b.value()); } Chris@16: inline real_concept ceil(real_concept a) Chris@16: { return std::ceil(a.value()); } Chris@16: #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS Chris@16: // I've seen std::fmod(long double) crash on some platforms Chris@16: // so use fmodl instead: Chris@16: #ifdef _WIN32_WCE Chris@16: // Chris@16: // Ugly workaround for macro fmodl: Chris@16: // Chris@16: inline long double call_fmodl(long double a, long double b) Chris@16: { return fmodl(a, b); } Chris@16: inline real_concept fmod(real_concept a, real_concept b) Chris@16: { return call_fmodl(a.value(), b.value()); } Chris@16: #else Chris@16: inline real_concept fmod(real_concept a, real_concept b) Chris@16: { return fmodl(a.value(), b.value()); } Chris@16: #endif Chris@16: #endif Chris@16: inline real_concept cosh(real_concept a) Chris@16: { return std::cosh(a.value()); } Chris@16: inline real_concept exp(real_concept a) Chris@16: { return std::exp(a.value()); } Chris@16: inline real_concept fabs(real_concept a) Chris@16: { return std::fabs(a.value()); } Chris@16: inline real_concept abs(real_concept a) Chris@16: { return std::abs(a.value()); } Chris@16: inline real_concept floor(real_concept a) Chris@16: { return std::floor(a.value()); } Chris@16: inline real_concept modf(real_concept a, real_concept* ipart) Chris@16: { Chris@16: real_concept_base_type ip; Chris@16: real_concept_base_type result = std::modf(a.value(), &ip); Chris@16: *ipart = ip; Chris@16: return result; Chris@16: } Chris@16: inline real_concept frexp(real_concept a, int* expon) Chris@16: { return std::frexp(a.value(), expon); } Chris@16: inline real_concept ldexp(real_concept a, int expon) Chris@16: { return std::ldexp(a.value(), expon); } Chris@16: inline real_concept log(real_concept a) Chris@16: { return std::log(a.value()); } Chris@16: inline real_concept log10(real_concept a) Chris@16: { return std::log10(a.value()); } Chris@16: inline real_concept tan(real_concept a) Chris@16: { return std::tan(a.value()); } Chris@16: inline real_concept pow(real_concept a, real_concept b) Chris@16: { return std::pow(a.value(), b.value()); } Chris@16: #if !defined(__SUNPRO_CC) Chris@16: inline real_concept pow(real_concept a, int b) Chris@16: { return std::pow(a.value(), b); } Chris@16: #else Chris@16: inline real_concept pow(real_concept a, int b) Chris@16: { return std::pow(a.value(), static_cast(b)); } Chris@16: #endif Chris@16: inline real_concept sin(real_concept a) Chris@16: { return std::sin(a.value()); } Chris@16: inline real_concept sinh(real_concept a) Chris@16: { return std::sinh(a.value()); } Chris@16: inline real_concept sqrt(real_concept a) Chris@16: { return std::sqrt(a.value()); } Chris@16: inline real_concept tanh(real_concept a) Chris@16: { return std::tanh(a.value()); } Chris@16: Chris@16: // Chris@16: // Conversion and truncation routines: Chris@16: // Chris@16: template Chris@16: inline int iround(const concepts::real_concept& v, const Policy& pol) Chris@16: { return boost::math::iround(v.value(), pol); } Chris@16: inline int iround(const concepts::real_concept& v) Chris@16: { return boost::math::iround(v.value(), policies::policy<>()); } Chris@16: template Chris@16: inline long lround(const concepts::real_concept& v, const Policy& pol) Chris@16: { return boost::math::lround(v.value(), pol); } Chris@16: inline long lround(const concepts::real_concept& v) Chris@16: { return boost::math::lround(v.value(), policies::policy<>()); } Chris@16: Chris@16: #ifdef BOOST_HAS_LONG_LONG Chris@16: template Chris@16: inline boost::long_long_type llround(const concepts::real_concept& v, const Policy& pol) Chris@16: { return boost::math::llround(v.value(), pol); } Chris@16: inline boost::long_long_type llround(const concepts::real_concept& v) Chris@16: { return boost::math::llround(v.value(), policies::policy<>()); } Chris@16: #endif Chris@16: Chris@16: template Chris@16: inline int itrunc(const concepts::real_concept& v, const Policy& pol) Chris@16: { return boost::math::itrunc(v.value(), pol); } Chris@16: inline int itrunc(const concepts::real_concept& v) Chris@16: { return boost::math::itrunc(v.value(), policies::policy<>()); } Chris@16: template Chris@16: inline long ltrunc(const concepts::real_concept& v, const Policy& pol) Chris@16: { return boost::math::ltrunc(v.value(), pol); } Chris@16: inline long ltrunc(const concepts::real_concept& v) Chris@16: { return boost::math::ltrunc(v.value(), policies::policy<>()); } Chris@16: Chris@16: #ifdef BOOST_HAS_LONG_LONG Chris@16: template Chris@16: inline boost::long_long_type lltrunc(const concepts::real_concept& v, const Policy& pol) Chris@16: { return boost::math::lltrunc(v.value(), pol); } Chris@16: inline boost::long_long_type lltrunc(const concepts::real_concept& v) Chris@16: { return boost::math::lltrunc(v.value(), policies::policy<>()); } Chris@16: #endif Chris@16: Chris@16: // Streaming: Chris@16: template Chris@16: inline std::basic_ostream& operator<<(std::basic_ostream& os, const real_concept& a) Chris@16: { Chris@16: return os << a.value(); Chris@16: } Chris@16: template Chris@16: inline std::basic_istream& operator>>(std::basic_istream& is, real_concept& a) Chris@16: { Chris@16: #if defined(BOOST_MSVC) && defined(__SGI_STL_PORT) Chris@16: // Chris@16: // STLPort 5.1.4 has a problem reading long doubles from strings, Chris@16: // see http://sourceforge.net/tracker/index.php?func=detail&aid=1811043&group_id=146814&atid=766244 Chris@16: // Chris@16: double v; Chris@16: is >> v; Chris@16: a = v; Chris@16: return is; Chris@101: #elif defined(__SGI_STL_PORT) || defined(_RWSTD_VER) || defined(__LIBCOMO__) || defined(_LIBCPP_VERSION) Chris@16: std::string s; Chris@16: real_concept_base_type d; Chris@16: is >> s; Chris@16: std::sscanf(s.c_str(), "%Lf", &d); Chris@16: a = d; Chris@16: return is; Chris@16: #else Chris@16: real_concept_base_type v; Chris@16: is >> v; Chris@16: a = v; Chris@16: return is; Chris@16: #endif Chris@16: } Chris@16: Chris@16: } // namespace concepts Chris@16: Chris@16: namespace tools Chris@16: { Chris@16: Chris@16: template <> Chris@101: inline concepts::real_concept make_big_value(boost::math::tools::largest_float val, const char* , mpl::false_ const&, mpl::false_ const&) Chris@16: { Chris@16: return val; // Can't use lexical_cast here, sometimes it fails.... Chris@16: } Chris@16: Chris@16: template <> Chris@16: inline concepts::real_concept max_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept)) Chris@16: { Chris@16: return max_value(); Chris@16: } Chris@16: Chris@16: template <> Chris@16: inline concepts::real_concept min_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept)) Chris@16: { Chris@16: return min_value(); Chris@16: } Chris@16: Chris@16: template <> Chris@16: inline concepts::real_concept log_max_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept)) Chris@16: { Chris@16: return log_max_value(); Chris@16: } Chris@16: Chris@16: template <> Chris@16: inline concepts::real_concept log_min_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept)) Chris@16: { Chris@16: return log_min_value(); Chris@16: } Chris@16: Chris@16: template <> Chris@16: inline concepts::real_concept epsilon(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept)) Chris@16: { Chris@16: #ifdef __SUNPRO_CC Chris@16: return std::numeric_limits::epsilon(); Chris@16: #else Chris@16: return tools::epsilon(); Chris@16: #endif Chris@16: } Chris@16: Chris@16: template <> Chris@16: inline int digits(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept)) Chris@16: { Chris@16: // Assume number of significand bits is same as real_concept_base_type, Chris@16: // unless std::numeric_limits::is_specialized to provide digits. Chris@16: return tools::digits(); Chris@16: // Note that if numeric_limits real concept is NOT specialized to provide digits10 Chris@16: // (or max_digits10) then the default precision of 6 decimal digits will be used Chris@16: // by Boost test (giving misleading error messages like Chris@16: // "difference between {9.79796} and {9.79796} exceeds 5.42101e-19%" Chris@16: // and by Boost lexical cast and serialization causing loss of accuracy. Chris@16: } Chris@16: Chris@16: } // namespace tools Chris@16: Chris@16: #if defined(__SGI_STL_PORT) || defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) Chris@16: // Chris@16: // We shouldn't really need these type casts any more, but there are some Chris@16: // STLport iostream bugs we work around by using them.... Chris@16: // Chris@16: namespace tools Chris@16: { Chris@16: // real_cast converts from T to integer and narrower floating-point types. Chris@16: Chris@16: // Convert from T to integer types. Chris@16: Chris@16: template <> Chris@16: inline unsigned int real_cast(concepts::real_concept r) Chris@16: { Chris@16: return static_cast(r.value()); Chris@16: } Chris@16: Chris@16: template <> Chris@16: inline int real_cast(concepts::real_concept r) Chris@16: { Chris@16: return static_cast(r.value()); Chris@16: } Chris@16: Chris@16: template <> Chris@16: inline long real_cast(concepts::real_concept r) Chris@16: { Chris@16: return static_cast(r.value()); Chris@16: } Chris@16: Chris@16: // Converts from T to narrower floating-point types, float, double & long double. Chris@16: Chris@16: template <> Chris@16: inline float real_cast(concepts::real_concept r) Chris@16: { Chris@16: return static_cast(r.value()); Chris@16: } Chris@16: template <> Chris@16: inline double real_cast(concepts::real_concept r) Chris@16: { Chris@16: return static_cast(r.value()); Chris@16: } Chris@16: template <> Chris@16: inline long double real_cast(concepts::real_concept r) Chris@16: { Chris@16: return r.value(); Chris@16: } Chris@16: Chris@16: } // STLPort Chris@16: Chris@16: #endif Chris@16: Chris@16: #if BOOST_WORKAROUND(BOOST_MSVC, <= 1310) Chris@16: // Chris@16: // For some strange reason ADL sometimes fails to find the Chris@16: // correct overloads, unless we bring these declarations into scope: Chris@16: // Chris@16: using concepts::itrunc; Chris@16: using concepts::iround; Chris@16: Chris@16: #endif Chris@16: Chris@16: } // namespace math Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_MATH_REAL_CONCEPT_HPP Chris@16: Chris@16: