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: // std_real_concept is an archetype for built-in Real types. Chris@16: Chris@16: // The main purpose in providing this type is to verify Chris@16: // that std lib functions are found via a using declaration Chris@16: // bringing those functions into the current scope, and not Chris@16: // just because they happen to be in global scope. Chris@16: // Chris@16: // If ::pow is found rather than std::pow say, then the code Chris@16: // will silently compile, but truncation of long doubles to Chris@16: // double will cause a significant loss of precision. Chris@16: // A template instantiated with std_real_concept will *only* Chris@16: // compile if it std::whatever is in scope. Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include // fmodl Chris@16: Chris@16: #ifndef BOOST_MATH_STD_REAL_CONCEPT_HPP Chris@16: #define BOOST_MATH_STD_REAL_CONCEPT_HPP 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 std_real_concept_base_type; Chris@16: #else Chris@16: typedef long double std_real_concept_base_type; Chris@16: #endif Chris@16: Chris@16: class std_real_concept Chris@16: { Chris@16: public: Chris@16: // Constructors: Chris@16: std_real_concept() : m_value(0){} Chris@16: std_real_concept(char c) : m_value(c){} Chris@16: #ifndef BOOST_NO_INTRINSIC_WCHAR_T Chris@16: std_real_concept(wchar_t c) : m_value(c){} Chris@16: #endif Chris@16: std_real_concept(unsigned char c) : m_value(c){} Chris@16: std_real_concept(signed char c) : m_value(c){} Chris@16: std_real_concept(unsigned short c) : m_value(c){} Chris@16: std_real_concept(short c) : m_value(c){} Chris@16: std_real_concept(unsigned int c) : m_value(c){} Chris@16: std_real_concept(int c) : m_value(c){} Chris@16: std_real_concept(unsigned long c) : m_value(c){} Chris@16: std_real_concept(long c) : m_value(c){} Chris@16: #if defined(__DECCXX) || defined(__SUNPRO_CC) Chris@16: std_real_concept(unsigned long long c) : m_value(static_cast(c)){} Chris@16: std_real_concept(long long c) : m_value(static_cast(c)){} Chris@16: #elif defined(BOOST_HAS_LONG_LONG) Chris@16: std_real_concept(boost::ulong_long_type c) : m_value(static_cast(c)){} Chris@16: std_real_concept(boost::long_long_type c) : m_value(static_cast(c)){} Chris@16: #endif Chris@16: std_real_concept(float c) : m_value(c){} Chris@16: std_real_concept(double c) : m_value(c){} Chris@16: std_real_concept(long double c) : m_value(c){} Chris@16: #ifdef BOOST_MATH_USE_FLOAT128 Chris@101: std_real_concept(BOOST_MATH_FLOAT128_TYPE c) : m_value(c){} Chris@16: #endif Chris@16: Chris@16: // Assignment: Chris@16: std_real_concept& operator=(char c) { m_value = c; return *this; } Chris@16: std_real_concept& operator=(unsigned char c) { m_value = c; return *this; } Chris@16: std_real_concept& operator=(signed char c) { m_value = c; return *this; } Chris@16: #ifndef BOOST_NO_INTRINSIC_WCHAR_T Chris@16: std_real_concept& operator=(wchar_t c) { m_value = c; return *this; } Chris@16: #endif Chris@16: std_real_concept& operator=(short c) { m_value = c; return *this; } Chris@16: std_real_concept& operator=(unsigned short c) { m_value = c; return *this; } Chris@16: std_real_concept& operator=(int c) { m_value = c; return *this; } Chris@16: std_real_concept& operator=(unsigned int c) { m_value = c; return *this; } Chris@16: std_real_concept& operator=(long c) { m_value = c; return *this; } Chris@16: std_real_concept& operator=(unsigned long c) { m_value = c; return *this; } Chris@16: #if defined(__DECCXX) || defined(__SUNPRO_CC) Chris@16: std_real_concept& operator=(unsigned long long c) { m_value = static_cast(c); return *this; } Chris@16: std_real_concept& operator=(long long c) { m_value = static_cast(c); return *this; } Chris@16: #elif defined(BOOST_HAS_LONG_LONG) Chris@16: std_real_concept& operator=(boost::long_long_type c) { m_value = static_cast(c); return *this; } Chris@16: std_real_concept& operator=(boost::ulong_long_type c) { m_value = static_cast(c); return *this; } Chris@16: #endif Chris@16: std_real_concept& operator=(float c) { m_value = c; return *this; } Chris@16: std_real_concept& operator=(double c) { m_value = c; return *this; } Chris@16: std_real_concept& operator=(long double c) { m_value = c; return *this; } Chris@16: Chris@16: // Access: Chris@16: std_real_concept_base_type value()const{ return m_value; } Chris@16: Chris@16: // Member arithmetic: Chris@16: std_real_concept& operator+=(const std_real_concept& other) Chris@16: { m_value += other.value(); return *this; } Chris@16: std_real_concept& operator-=(const std_real_concept& other) Chris@16: { m_value -= other.value(); return *this; } Chris@16: std_real_concept& operator*=(const std_real_concept& other) Chris@16: { m_value *= other.value(); return *this; } Chris@16: std_real_concept& operator/=(const std_real_concept& other) Chris@16: { m_value /= other.value(); return *this; } Chris@16: std_real_concept operator-()const Chris@16: { return -m_value; } Chris@16: std_real_concept const& operator+()const Chris@16: { return *this; } Chris@16: Chris@16: private: Chris@16: std_real_concept_base_type m_value; Chris@16: }; Chris@16: Chris@16: // Non-member arithmetic: Chris@16: inline std_real_concept operator+(const std_real_concept& a, const std_real_concept& b) Chris@16: { Chris@16: std_real_concept result(a); Chris@16: result += b; Chris@16: return result; Chris@16: } Chris@16: inline std_real_concept operator-(const std_real_concept& a, const std_real_concept& b) Chris@16: { Chris@16: std_real_concept result(a); Chris@16: result -= b; Chris@16: return result; Chris@16: } Chris@16: inline std_real_concept operator*(const std_real_concept& a, const std_real_concept& b) Chris@16: { Chris@16: std_real_concept result(a); Chris@16: result *= b; Chris@16: return result; Chris@16: } Chris@16: inline std_real_concept operator/(const std_real_concept& a, const std_real_concept& b) Chris@16: { Chris@16: std_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 std_real_concept& a, const std_real_concept& b) Chris@16: { return a.value() == b.value(); } Chris@16: inline bool operator != (const std_real_concept& a, const std_real_concept& b) Chris@16: { return a.value() != b.value();} Chris@16: inline bool operator < (const std_real_concept& a, const std_real_concept& b) Chris@16: { return a.value() < b.value(); } Chris@16: inline bool operator <= (const std_real_concept& a, const std_real_concept& b) Chris@16: { return a.value() <= b.value(); } Chris@16: inline bool operator > (const std_real_concept& a, const std_real_concept& b) Chris@16: { return a.value() > b.value(); } Chris@16: inline bool operator >= (const std_real_concept& a, const std_real_concept& b) Chris@16: { return a.value() >= b.value(); } Chris@16: Chris@16: } // namespace concepts Chris@16: } // namespace math Chris@16: } // namespace boost Chris@16: Chris@16: namespace std{ Chris@16: Chris@16: // Non-member functions: Chris@16: inline boost::math::concepts::std_real_concept acos(boost::math::concepts::std_real_concept a) Chris@16: { return std::acos(a.value()); } Chris@16: inline boost::math::concepts::std_real_concept cos(boost::math::concepts::std_real_concept a) Chris@16: { return std::cos(a.value()); } Chris@16: inline boost::math::concepts::std_real_concept asin(boost::math::concepts::std_real_concept a) Chris@16: { return std::asin(a.value()); } Chris@16: inline boost::math::concepts::std_real_concept atan(boost::math::concepts::std_real_concept a) Chris@16: { return std::atan(a.value()); } Chris@16: inline boost::math::concepts::std_real_concept atan2(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept b) Chris@16: { return std::atan2(a.value(), b.value()); } Chris@16: inline boost::math::concepts::std_real_concept ceil(boost::math::concepts::std_real_concept a) Chris@16: { return std::ceil(a.value()); } Chris@16: #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS Chris@16: inline boost::math::concepts::std_real_concept fmod(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept b) Chris@16: { return fmodl(a.value(), b.value()); } Chris@16: #else Chris@16: inline boost::math::concepts::std_real_concept fmod(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept b) Chris@16: { return std::fmod(a.value(), b.value()); } Chris@16: #endif Chris@16: inline boost::math::concepts::std_real_concept cosh(boost::math::concepts::std_real_concept a) Chris@16: { return std::cosh(a.value()); } Chris@16: inline boost::math::concepts::std_real_concept exp(boost::math::concepts::std_real_concept a) Chris@16: { return std::exp(a.value()); } Chris@16: inline boost::math::concepts::std_real_concept fabs(boost::math::concepts::std_real_concept a) Chris@16: { return std::fabs(a.value()); } Chris@16: inline boost::math::concepts::std_real_concept abs(boost::math::concepts::std_real_concept a) Chris@16: { return std::abs(a.value()); } Chris@16: inline boost::math::concepts::std_real_concept floor(boost::math::concepts::std_real_concept a) Chris@16: { return std::floor(a.value()); } Chris@16: inline boost::math::concepts::std_real_concept modf(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept* ipart) Chris@16: { Chris@16: boost::math::concepts::std_real_concept_base_type ip; Chris@16: boost::math::concepts::std_real_concept_base_type result = std::modf(a.value(), &ip); Chris@16: *ipart = ip; Chris@16: return result; Chris@16: } Chris@16: inline boost::math::concepts::std_real_concept frexp(boost::math::concepts::std_real_concept a, int* expon) Chris@16: { return std::frexp(a.value(), expon); } Chris@16: inline boost::math::concepts::std_real_concept ldexp(boost::math::concepts::std_real_concept a, int expon) Chris@16: { return std::ldexp(a.value(), expon); } Chris@16: inline boost::math::concepts::std_real_concept log(boost::math::concepts::std_real_concept a) Chris@16: { return std::log(a.value()); } Chris@16: inline boost::math::concepts::std_real_concept log10(boost::math::concepts::std_real_concept a) Chris@16: { return std::log10(a.value()); } Chris@16: inline boost::math::concepts::std_real_concept tan(boost::math::concepts::std_real_concept a) Chris@16: { return std::tan(a.value()); } Chris@16: inline boost::math::concepts::std_real_concept pow(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept b) Chris@16: { return std::pow(a.value(), b.value()); } Chris@16: #if !defined(__SUNPRO_CC) Chris@16: inline boost::math::concepts::std_real_concept pow(boost::math::concepts::std_real_concept a, int b) Chris@16: { return std::pow(a.value(), b); } Chris@16: #else Chris@16: inline boost::math::concepts::std_real_concept pow(boost::math::concepts::std_real_concept a, int b) Chris@16: { return std::pow(a.value(), static_cast(b)); } Chris@16: #endif Chris@16: inline boost::math::concepts::std_real_concept sin(boost::math::concepts::std_real_concept a) Chris@16: { return std::sin(a.value()); } Chris@16: inline boost::math::concepts::std_real_concept sinh(boost::math::concepts::std_real_concept a) Chris@16: { return std::sinh(a.value()); } Chris@16: inline boost::math::concepts::std_real_concept sqrt(boost::math::concepts::std_real_concept a) Chris@16: { return std::sqrt(a.value()); } Chris@16: inline boost::math::concepts::std_real_concept tanh(boost::math::concepts::std_real_concept a) Chris@16: { return std::tanh(a.value()); } Chris@16: Chris@16: } // namespace std Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost{ namespace math{ namespace concepts{ Chris@16: Chris@16: // Chris@16: // Conversion and truncation routines: Chris@16: // Chris@16: template Chris@16: inline int iround(const concepts::std_real_concept& v, const Policy& pol) Chris@16: { Chris@16: return boost::math::iround(v.value(), pol); Chris@16: } Chris@16: inline int iround(const concepts::std_real_concept& v) Chris@16: { Chris@16: return boost::math::iround(v.value(), policies::policy<>()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline long lround(const concepts::std_real_concept& v, const Policy& pol) Chris@16: { Chris@16: return boost::math::lround(v.value(), pol); Chris@16: } Chris@16: inline long lround(const concepts::std_real_concept& v) Chris@16: { Chris@16: return boost::math::lround(v.value(), policies::policy<>()); Chris@16: } Chris@16: Chris@16: #ifdef BOOST_HAS_LONG_LONG Chris@16: Chris@16: template Chris@16: inline boost::long_long_type llround(const concepts::std_real_concept& v, const Policy& pol) Chris@16: { Chris@16: return boost::math::llround(v.value(), pol); Chris@16: } Chris@16: inline boost::long_long_type llround(const concepts::std_real_concept& v) Chris@16: { Chris@16: return boost::math::llround(v.value(), policies::policy<>()); Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: template Chris@16: inline int itrunc(const concepts::std_real_concept& v, const Policy& pol) Chris@16: { Chris@16: return boost::math::itrunc(v.value(), pol); Chris@16: } Chris@16: inline int itrunc(const concepts::std_real_concept& v) Chris@16: { Chris@16: return boost::math::itrunc(v.value(), policies::policy<>()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline long ltrunc(const concepts::std_real_concept& v, const Policy& pol) Chris@16: { Chris@16: return boost::math::ltrunc(v.value(), pol); Chris@16: } Chris@16: inline long ltrunc(const concepts::std_real_concept& v) Chris@16: { Chris@16: return boost::math::ltrunc(v.value(), policies::policy<>()); Chris@16: } Chris@16: Chris@16: #ifdef BOOST_HAS_LONG_LONG Chris@16: Chris@16: template Chris@16: inline boost::long_long_type lltrunc(const concepts::std_real_concept& v, const Policy& pol) Chris@16: { Chris@16: return boost::math::lltrunc(v.value(), pol); Chris@16: } Chris@16: inline boost::long_long_type lltrunc(const concepts::std_real_concept& v) Chris@16: { Chris@16: return boost::math::lltrunc(v.value(), policies::policy<>()); Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: // Streaming: Chris@16: template Chris@16: inline std::basic_ostream& operator<<(std::basic_ostream& os, const std_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, std_real_concept& a) Chris@16: { Chris@101: #if defined(__SGI_STL_PORT) || defined(_RWSTD_VER) || defined(__LIBCOMO__) || defined(_LIBCPP_VERSION) Chris@101: std::string s; Chris@101: std_real_concept_base_type d; Chris@101: is >> s; Chris@101: std::sscanf(s.c_str(), "%Lf", &d); Chris@101: a = d; Chris@101: return is; Chris@101: #else Chris@16: std_real_concept_base_type v; Chris@16: is >> v; Chris@16: a = v; Chris@16: return is; Chris@101: #endif Chris@16: } Chris@16: Chris@16: } // namespace concepts Chris@16: }} Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost{ namespace math{ Chris@16: namespace tools Chris@16: { Chris@16: Chris@16: template <> Chris@101: inline concepts::std_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::std_real_concept max_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept)) Chris@16: { Chris@16: return max_value(); Chris@16: } Chris@16: Chris@16: template <> Chris@16: inline concepts::std_real_concept min_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept)) Chris@16: { Chris@16: return min_value(); Chris@16: } Chris@16: Chris@16: template <> Chris@16: inline concepts::std_real_concept log_max_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept)) Chris@16: { Chris@16: return log_max_value(); Chris@16: } Chris@16: Chris@16: template <> Chris@16: inline concepts::std_real_concept log_min_value(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept)) Chris@16: { Chris@16: return log_min_value(); Chris@16: } Chris@16: Chris@16: template <> Chris@16: inline concepts::std_real_concept epsilon(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept)) Chris@16: { Chris@16: return tools::epsilon(); Chris@16: } Chris@16: Chris@16: template <> Chris@16: inline int digits(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept)) Chris@16: { // Assume number of significand bits is same as std_real_concept_base_type, Chris@16: // unless std::numeric_limits::is_specialized to provide digits. Chris@16: return digits(); Chris@16: } Chris@16: Chris@16: } // namespace tools Chris@16: Chris@16: #if BOOST_WORKAROUND(BOOST_MSVC, <= 1310) Chris@16: using concepts::itrunc; Chris@16: using concepts::ltrunc; Chris@16: using concepts::lltrunc; Chris@16: using concepts::iround; Chris@16: using concepts::lround; Chris@16: using concepts::llround; Chris@16: #endif Chris@16: Chris@16: } // namespace math Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_MATH_STD_REAL_CONCEPT_HPP Chris@16: Chris@16: Chris@16: Chris@16: