1 // Copyright John Maddock 2006.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 // real_concept is an archetype for User defined Real types.
10 // This file defines the features, constructors, operators, functions...
11 // that are essential to use mathematical and statistical functions.
12 // The template typename "RealType" is used where this type
13 // (as well as the normal built-in types, float, double & long double)
15 // That this is the minimum set is confirmed by use as a type
16 // in tests of all functions & distributions, for example:
17 // test_spots(0.F); & test_spots(0.); for float and double, but also
18 // test_spots(boost::math::concepts::real_concept(0.));
19 // NTL quad_float type is an example of a type meeting the requirements,
20 // but note minor additions are needed - see ntl.diff and documentation
21 // "Using With NTL - a High-Precision Floating-Point Library".
23 #ifndef BOOST_MATH_REAL_CONCEPT_HPP
24 #define BOOST_MATH_REAL_CONCEPT_HPP
26 #include <boost/config.hpp>
27 #include <boost/limits.hpp>
28 #include <boost/math/special_functions/round.hpp>
29 #include <boost/math/special_functions/trunc.hpp>
30 #include <boost/math/special_functions/modf.hpp>
31 #include <boost/math/tools/big_constant.hpp>
32 #include <boost/math/tools/precision.hpp>
33 #include <boost/math/policies/policy.hpp>
34 #if defined(__SGI_STL_PORT)
35 # include <boost/math/tools/real_cast.hpp>
39 #include <boost/config/no_tr1/cmath.hpp>
40 #include <math.h> // fmodl
42 #if defined(__SGI_STL_PORT) || defined(_RWSTD_VER) || defined(__LIBCOMO__)
46 namespace boost{ namespace math{
51 #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
52 typedef double real_concept_base_type;
54 typedef long double real_concept_base_type;
61 real_concept() : m_value(0){}
62 real_concept(char c) : m_value(c){}
63 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
64 real_concept(wchar_t c) : m_value(c){}
66 real_concept(unsigned char c) : m_value(c){}
67 real_concept(signed char c) : m_value(c){}
68 real_concept(unsigned short c) : m_value(c){}
69 real_concept(short c) : m_value(c){}
70 real_concept(unsigned int c) : m_value(c){}
71 real_concept(int c) : m_value(c){}
72 real_concept(unsigned long c) : m_value(c){}
73 real_concept(long c) : m_value(c){}
74 #if defined(__DECCXX) || defined(__SUNPRO_CC)
75 real_concept(unsigned long long c) : m_value(static_cast<real_concept_base_type>(c)){}
76 real_concept(long long c) : m_value(static_cast<real_concept_base_type>(c)){}
77 #elif defined(BOOST_HAS_LONG_LONG)
78 real_concept(boost::ulong_long_type c) : m_value(static_cast<real_concept_base_type>(c)){}
79 real_concept(boost::long_long_type c) : m_value(static_cast<real_concept_base_type>(c)){}
80 #elif defined(BOOST_HAS_MS_INT64)
81 real_concept(unsigned __int64 c) : m_value(static_cast<real_concept_base_type>(c)){}
82 real_concept(__int64 c) : m_value(static_cast<real_concept_base_type>(c)){}
84 real_concept(float c) : m_value(c){}
85 real_concept(double c) : m_value(c){}
86 real_concept(long double c) : m_value(c){}
87 #ifdef BOOST_MATH_USE_FLOAT128
88 real_concept(BOOST_MATH_FLOAT128_TYPE c) : m_value(c){}
92 real_concept& operator=(char c) { m_value = c; return *this; }
93 real_concept& operator=(unsigned char c) { m_value = c; return *this; }
94 real_concept& operator=(signed char c) { m_value = c; return *this; }
95 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
96 real_concept& operator=(wchar_t c) { m_value = c; return *this; }
98 real_concept& operator=(short c) { m_value = c; return *this; }
99 real_concept& operator=(unsigned short c) { m_value = c; return *this; }
100 real_concept& operator=(int c) { m_value = c; return *this; }
101 real_concept& operator=(unsigned int c) { m_value = c; return *this; }
102 real_concept& operator=(long c) { m_value = c; return *this; }
103 real_concept& operator=(unsigned long c) { m_value = c; return *this; }
104 #ifdef BOOST_HAS_LONG_LONG
105 real_concept& operator=(boost::long_long_type c) { m_value = static_cast<real_concept_base_type>(c); return *this; }
106 real_concept& operator=(boost::ulong_long_type c) { m_value = static_cast<real_concept_base_type>(c); return *this; }
108 real_concept& operator=(float c) { m_value = c; return *this; }
109 real_concept& operator=(double c) { m_value = c; return *this; }
110 real_concept& operator=(long double c) { m_value = c; return *this; }
113 real_concept_base_type value()const{ return m_value; }
115 // Member arithmetic:
116 real_concept& operator+=(const real_concept& other)
117 { m_value += other.value(); return *this; }
118 real_concept& operator-=(const real_concept& other)
119 { m_value -= other.value(); return *this; }
120 real_concept& operator*=(const real_concept& other)
121 { m_value *= other.value(); return *this; }
122 real_concept& operator/=(const real_concept& other)
123 { m_value /= other.value(); return *this; }
124 real_concept operator-()const
126 real_concept const& operator+()const
128 real_concept& operator++()
129 { ++m_value; return *this; }
130 real_concept& operator--()
131 { --m_value; return *this; }
134 real_concept_base_type m_value;
137 // Non-member arithmetic:
138 inline real_concept operator+(const real_concept& a, const real_concept& b)
140 real_concept result(a);
144 inline real_concept operator-(const real_concept& a, const real_concept& b)
146 real_concept result(a);
150 inline real_concept operator*(const real_concept& a, const real_concept& b)
152 real_concept result(a);
156 inline real_concept operator/(const real_concept& a, const real_concept& b)
158 real_concept result(a);
164 inline bool operator == (const real_concept& a, const real_concept& b)
165 { return a.value() == b.value(); }
166 inline bool operator != (const real_concept& a, const real_concept& b)
167 { return a.value() != b.value();}
168 inline bool operator < (const real_concept& a, const real_concept& b)
169 { return a.value() < b.value(); }
170 inline bool operator <= (const real_concept& a, const real_concept& b)
171 { return a.value() <= b.value(); }
172 inline bool operator > (const real_concept& a, const real_concept& b)
173 { return a.value() > b.value(); }
174 inline bool operator >= (const real_concept& a, const real_concept& b)
175 { return a.value() >= b.value(); }
177 // Non-member functions:
178 inline real_concept acos(real_concept a)
179 { return std::acos(a.value()); }
180 inline real_concept cos(real_concept a)
181 { return std::cos(a.value()); }
182 inline real_concept asin(real_concept a)
183 { return std::asin(a.value()); }
184 inline real_concept atan(real_concept a)
185 { return std::atan(a.value()); }
186 inline real_concept atan2(real_concept a, real_concept b)
187 { return std::atan2(a.value(), b.value()); }
188 inline real_concept ceil(real_concept a)
189 { return std::ceil(a.value()); }
190 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
191 // I've seen std::fmod(long double) crash on some platforms
192 // so use fmodl instead:
195 // Ugly workaround for macro fmodl:
197 inline long double call_fmodl(long double a, long double b)
198 { return fmodl(a, b); }
199 inline real_concept fmod(real_concept a, real_concept b)
200 { return call_fmodl(a.value(), b.value()); }
202 inline real_concept fmod(real_concept a, real_concept b)
203 { return fmodl(a.value(), b.value()); }
206 inline real_concept cosh(real_concept a)
207 { return std::cosh(a.value()); }
208 inline real_concept exp(real_concept a)
209 { return std::exp(a.value()); }
210 inline real_concept fabs(real_concept a)
211 { return std::fabs(a.value()); }
212 inline real_concept abs(real_concept a)
213 { return std::abs(a.value()); }
214 inline real_concept floor(real_concept a)
215 { return std::floor(a.value()); }
216 inline real_concept modf(real_concept a, real_concept* ipart)
219 real_concept_base_type ip;
220 real_concept_base_type result = boost::math::modf(a.value(), &ip);
224 real_concept_base_type ip;
225 real_concept_base_type result = std::modf(a.value(), &ip);
230 inline real_concept frexp(real_concept a, int* expon)
231 { return std::frexp(a.value(), expon); }
232 inline real_concept ldexp(real_concept a, int expon)
233 { return std::ldexp(a.value(), expon); }
234 inline real_concept log(real_concept a)
235 { return std::log(a.value()); }
236 inline real_concept log10(real_concept a)
237 { return std::log10(a.value()); }
238 inline real_concept tan(real_concept a)
239 { return std::tan(a.value()); }
240 inline real_concept pow(real_concept a, real_concept b)
241 { return std::pow(a.value(), b.value()); }
242 #if !defined(__SUNPRO_CC)
243 inline real_concept pow(real_concept a, int b)
244 { return std::pow(a.value(), b); }
246 inline real_concept pow(real_concept a, int b)
247 { return std::pow(a.value(), static_cast<real_concept_base_type>(b)); }
249 inline real_concept sin(real_concept a)
250 { return std::sin(a.value()); }
251 inline real_concept sinh(real_concept a)
252 { return std::sinh(a.value()); }
253 inline real_concept sqrt(real_concept a)
254 { return std::sqrt(a.value()); }
255 inline real_concept tanh(real_concept a)
256 { return std::tanh(a.value()); }
259 // Conversion and truncation routines:
261 template <class Policy>
262 inline int iround(const concepts::real_concept& v, const Policy& pol)
263 { return boost::math::iround(v.value(), pol); }
264 inline int iround(const concepts::real_concept& v)
265 { return boost::math::iround(v.value(), policies::policy<>()); }
266 template <class Policy>
267 inline long lround(const concepts::real_concept& v, const Policy& pol)
268 { return boost::math::lround(v.value(), pol); }
269 inline long lround(const concepts::real_concept& v)
270 { return boost::math::lround(v.value(), policies::policy<>()); }
272 #ifdef BOOST_HAS_LONG_LONG
273 template <class Policy>
274 inline boost::long_long_type llround(const concepts::real_concept& v, const Policy& pol)
275 { return boost::math::llround(v.value(), pol); }
276 inline boost::long_long_type llround(const concepts::real_concept& v)
277 { return boost::math::llround(v.value(), policies::policy<>()); }
280 template <class Policy>
281 inline int itrunc(const concepts::real_concept& v, const Policy& pol)
282 { return boost::math::itrunc(v.value(), pol); }
283 inline int itrunc(const concepts::real_concept& v)
284 { return boost::math::itrunc(v.value(), policies::policy<>()); }
285 template <class Policy>
286 inline long ltrunc(const concepts::real_concept& v, const Policy& pol)
287 { return boost::math::ltrunc(v.value(), pol); }
288 inline long ltrunc(const concepts::real_concept& v)
289 { return boost::math::ltrunc(v.value(), policies::policy<>()); }
291 #ifdef BOOST_HAS_LONG_LONG
292 template <class Policy>
293 inline boost::long_long_type lltrunc(const concepts::real_concept& v, const Policy& pol)
294 { return boost::math::lltrunc(v.value(), pol); }
295 inline boost::long_long_type lltrunc(const concepts::real_concept& v)
296 { return boost::math::lltrunc(v.value(), policies::policy<>()); }
300 template <class charT, class traits>
301 inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const real_concept& a)
303 return os << a.value();
305 template <class charT, class traits>
306 inline std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& is, real_concept& a)
308 #if defined(BOOST_MSVC) && defined(__SGI_STL_PORT)
310 // STLPort 5.1.4 has a problem reading long doubles from strings,
311 // see http://sourceforge.net/tracker/index.php?func=detail&aid=1811043&group_id=146814&atid=766244
317 #elif defined(__SGI_STL_PORT) || defined(_RWSTD_VER) || defined(__LIBCOMO__) || defined(_LIBCPP_VERSION)
319 real_concept_base_type d;
321 std::sscanf(s.c_str(), "%Lf", &d);
325 real_concept_base_type v;
332 } // namespace concepts
338 inline concepts::real_concept make_big_value<concepts::real_concept>(boost::math::tools::largest_float val, const char* , mpl::false_ const&, mpl::false_ const&)
340 return val; // Can't use lexical_cast here, sometimes it fails....
344 inline concepts::real_concept max_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
346 return max_value<concepts::real_concept_base_type>();
350 inline concepts::real_concept min_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
352 return min_value<concepts::real_concept_base_type>();
356 inline concepts::real_concept log_max_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
358 return log_max_value<concepts::real_concept_base_type>();
362 inline concepts::real_concept log_min_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
364 return log_min_value<concepts::real_concept_base_type>();
368 inline concepts::real_concept epsilon<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
371 return std::numeric_limits<concepts::real_concept_base_type>::epsilon();
373 return tools::epsilon<concepts::real_concept_base_type>();
378 inline BOOST_MATH_CONSTEXPR int digits<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept)) BOOST_NOEXCEPT
380 // Assume number of significand bits is same as real_concept_base_type,
381 // unless std::numeric_limits<T>::is_specialized to provide digits.
382 return tools::digits<concepts::real_concept_base_type>();
383 // Note that if numeric_limits real concept is NOT specialized to provide digits10
384 // (or max_digits10) then the default precision of 6 decimal digits will be used
385 // by Boost test (giving misleading error messages like
386 // "difference between {9.79796} and {9.79796} exceeds 5.42101e-19%"
387 // and by Boost lexical cast and serialization causing loss of accuracy.
396 inline concepts::real_concept raise_rounding_error(
400 const concepts::real_concept&,
401 const ::boost::math::policies::rounding_error< ::boost::math::policies::errno_on_error>&) BOOST_MATH_NOEXCEPT(T)
404 // This may or may not do the right thing, but the user asked for the error
405 // to be silent so here we go anyway:
406 return val > 0 ? boost::math::tools::max_value<concepts::real_concept>() : -boost::math::tools::max_value<concepts::real_concept>();
412 #if defined(__SGI_STL_PORT) || defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS)
414 // We shouldn't really need these type casts any more, but there are some
415 // STLport iostream bugs we work around by using them....
419 // real_cast converts from T to integer and narrower floating-point types.
421 // Convert from T to integer types.
424 inline unsigned int real_cast<unsigned int, concepts::real_concept>(concepts::real_concept r)
426 return static_cast<unsigned int>(r.value());
430 inline int real_cast<int, concepts::real_concept>(concepts::real_concept r)
432 return static_cast<int>(r.value());
436 inline long real_cast<long, concepts::real_concept>(concepts::real_concept r)
438 return static_cast<long>(r.value());
441 // Converts from T to narrower floating-point types, float, double & long double.
444 inline float real_cast<float, concepts::real_concept>(concepts::real_concept r)
446 return static_cast<float>(r.value());
449 inline double real_cast<double, concepts::real_concept>(concepts::real_concept r)
451 return static_cast<double>(r.value());
454 inline long double real_cast<long double, concepts::real_concept>(concepts::real_concept r)
463 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
465 // For some strange reason ADL sometimes fails to find the
466 // correct overloads, unless we bring these declarations into scope:
468 using concepts::itrunc;
469 using concepts::iround;
476 #endif // BOOST_MATH_REAL_CONCEPT_HPP