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/math/special_functions/round.hpp>
27 #include <boost/math/special_functions/trunc.hpp>
28 #include <boost/math/special_functions/modf.hpp>
29 #include <boost/math/tools/big_constant.hpp>
30 #include <boost/math/tools/precision.hpp>
31 #include <boost/math/tools/config.hpp>
32 #include <boost/math/policies/policy.hpp>
33 #include <boost/math/special_functions/asinh.hpp>
34 #include <boost/math/special_functions/atanh.hpp>
35 #if defined(__SGI_STL_PORT)
36 # include <boost/math/tools/real_cast.hpp>
44 #if defined(__SGI_STL_PORT) || defined(_RWSTD_VER) || defined(__LIBCOMO__)
48 namespace boost{ namespace math{
53 #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
54 typedef double real_concept_base_type;
56 typedef long double real_concept_base_type;
63 real_concept() : m_value(0){}
64 real_concept(char c) : m_value(c){}
65 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 real_concept(unsigned long long c) : m_value(static_cast<real_concept_base_type>(c)){}
75 real_concept(long long c) : m_value(static_cast<real_concept_base_type>(c)){}
76 real_concept(float c) : m_value(c){}
77 real_concept(double c) : m_value(c){}
78 real_concept(long double c) : m_value(c){}
79 #ifdef BOOST_MATH_USE_FLOAT128
80 real_concept(BOOST_MATH_FLOAT128_TYPE c) : m_value(c){}
84 real_concept& operator=(char c) { m_value = c; return *this; }
85 real_concept& operator=(unsigned char c) { m_value = c; return *this; }
86 real_concept& operator=(signed char c) { m_value = c; return *this; }
87 real_concept& operator=(wchar_t c) { m_value = c; return *this; }
88 real_concept& operator=(short c) { m_value = c; return *this; }
89 real_concept& operator=(unsigned short c) { m_value = c; return *this; }
90 real_concept& operator=(int c) { m_value = c; return *this; }
91 real_concept& operator=(unsigned int c) { m_value = c; return *this; }
92 real_concept& operator=(long c) { m_value = c; return *this; }
93 real_concept& operator=(unsigned long c) { m_value = c; return *this; }
94 real_concept& operator=(long long c) { m_value = static_cast<real_concept_base_type>(c); return *this; }
95 real_concept& operator=(unsigned long long c) { m_value = static_cast<real_concept_base_type>(c); return *this; }
96 real_concept& operator=(float c) { m_value = c; return *this; }
97 real_concept& operator=(double c) { m_value = c; return *this; }
98 real_concept& operator=(long double c) { m_value = c; return *this; }
101 real_concept_base_type value()const{ return m_value; }
103 // Member arithmetic:
104 real_concept& operator+=(const real_concept& other)
105 { m_value += other.value(); return *this; }
106 real_concept& operator-=(const real_concept& other)
107 { m_value -= other.value(); return *this; }
108 real_concept& operator*=(const real_concept& other)
109 { m_value *= other.value(); return *this; }
110 real_concept& operator/=(const real_concept& other)
111 { m_value /= other.value(); return *this; }
112 real_concept operator-()const
114 real_concept const& operator+()const
116 real_concept& operator++()
117 { ++m_value; return *this; }
118 real_concept& operator--()
119 { --m_value; return *this; }
122 real_concept_base_type m_value;
125 // Non-member arithmetic:
126 inline real_concept operator+(const real_concept& a, const real_concept& b)
128 real_concept result(a);
132 inline real_concept operator-(const real_concept& a, const real_concept& b)
134 real_concept result(a);
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);
152 inline bool operator == (const real_concept& a, const real_concept& b)
153 { return a.value() == b.value(); }
154 inline bool operator != (const real_concept& a, const real_concept& b)
155 { return a.value() != b.value();}
156 inline bool operator < (const real_concept& a, const real_concept& b)
157 { return a.value() < b.value(); }
158 inline bool operator <= (const real_concept& a, const real_concept& b)
159 { return a.value() <= b.value(); }
160 inline bool operator > (const real_concept& a, const real_concept& b)
161 { return a.value() > b.value(); }
162 inline bool operator >= (const real_concept& a, const real_concept& b)
163 { return a.value() >= b.value(); }
165 // Non-member functions:
166 inline real_concept acos(real_concept a)
167 { return std::acos(a.value()); }
168 inline real_concept cos(real_concept a)
169 { return std::cos(a.value()); }
170 inline real_concept asin(real_concept a)
171 { return std::asin(a.value()); }
172 inline real_concept atan(real_concept a)
173 { return std::atan(a.value()); }
174 inline real_concept atan2(real_concept a, real_concept b)
175 { return std::atan2(a.value(), b.value()); }
176 inline real_concept ceil(real_concept a)
177 { return std::ceil(a.value()); }
178 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
179 // I've seen std::fmod(long double) crash on some platforms
180 // so use fmodl instead:
183 // Ugly workaround for macro fmodl:
185 inline long double call_fmodl(long double a, long double b)
186 { return fmodl(a, b); }
187 inline real_concept fmod(real_concept a, real_concept b)
188 { return call_fmodl(a.value(), b.value()); }
190 inline real_concept fmod(real_concept a, real_concept b)
191 { return fmodl(a.value(), b.value()); }
194 inline real_concept cosh(real_concept a)
195 { return std::cosh(a.value()); }
196 inline real_concept exp(real_concept a)
197 { return std::exp(a.value()); }
198 inline real_concept fabs(real_concept a)
199 { return std::fabs(a.value()); }
200 inline real_concept abs(real_concept a)
201 { return std::abs(a.value()); }
202 inline real_concept floor(real_concept a)
203 { return std::floor(a.value()); }
204 inline real_concept modf(real_concept a, real_concept* ipart)
207 real_concept_base_type ip;
208 real_concept_base_type result = boost::math::modf(a.value(), &ip);
212 real_concept_base_type ip;
213 real_concept_base_type result = std::modf(a.value(), &ip);
218 inline real_concept frexp(real_concept a, int* expon)
219 { return std::frexp(a.value(), expon); }
220 inline real_concept ldexp(real_concept a, int expon)
221 { return std::ldexp(a.value(), expon); }
222 inline real_concept log(real_concept a)
223 { return std::log(a.value()); }
224 inline real_concept log10(real_concept a)
225 { return std::log10(a.value()); }
226 inline real_concept tan(real_concept a)
227 { return std::tan(a.value()); }
228 inline real_concept pow(real_concept a, real_concept b)
229 { return std::pow(a.value(), b.value()); }
230 #if !defined(__SUNPRO_CC)
231 inline real_concept pow(real_concept a, int b)
232 { return std::pow(a.value(), b); }
234 inline real_concept pow(real_concept a, int b)
235 { return std::pow(a.value(), static_cast<real_concept_base_type>(b)); }
237 inline real_concept sin(real_concept a)
238 { return std::sin(a.value()); }
239 inline real_concept sinh(real_concept a)
240 { return std::sinh(a.value()); }
241 inline real_concept sqrt(real_concept a)
242 { return std::sqrt(a.value()); }
243 inline real_concept tanh(real_concept a)
244 { return std::tanh(a.value()); }
248 // Note that these must not actually call the std:: versions as that precludes using this
249 // header to test in C++03 mode, call the Boost versions instead:
251 inline boost::math::concepts::real_concept asinh(boost::math::concepts::real_concept a)
253 return boost::math::asinh(a.value(), boost::math::policies::make_policy(boost::math::policies::overflow_error<boost::math::policies::ignore_error>()));
255 inline boost::math::concepts::real_concept acosh(boost::math::concepts::real_concept a)
257 return boost::math::acosh(a.value(), boost::math::policies::make_policy(boost::math::policies::overflow_error<boost::math::policies::ignore_error>()));
259 inline boost::math::concepts::real_concept atanh(boost::math::concepts::real_concept a)
261 return boost::math::atanh(a.value(), boost::math::policies::make_policy(boost::math::policies::overflow_error<boost::math::policies::ignore_error>()));
265 // Conversion and truncation routines:
267 template <class Policy>
268 inline int iround(const concepts::real_concept& v, const Policy& pol)
269 { return boost::math::iround(v.value(), pol); }
270 inline int iround(const concepts::real_concept& v)
271 { return boost::math::iround(v.value(), policies::policy<>()); }
272 template <class Policy>
273 inline long lround(const concepts::real_concept& v, const Policy& pol)
274 { return boost::math::lround(v.value(), pol); }
275 inline long lround(const concepts::real_concept& v)
276 { return boost::math::lround(v.value(), policies::policy<>()); }
278 template <class Policy>
279 inline long long llround(const concepts::real_concept& v, const Policy& pol)
280 { return boost::math::llround(v.value(), pol); }
281 inline long long llround(const concepts::real_concept& v)
282 { return boost::math::llround(v.value(), policies::policy<>()); }
284 template <class Policy>
285 inline int itrunc(const concepts::real_concept& v, const Policy& pol)
286 { return boost::math::itrunc(v.value(), pol); }
287 inline int itrunc(const concepts::real_concept& v)
288 { return boost::math::itrunc(v.value(), policies::policy<>()); }
289 template <class Policy>
290 inline long ltrunc(const concepts::real_concept& v, const Policy& pol)
291 { return boost::math::ltrunc(v.value(), pol); }
292 inline long ltrunc(const concepts::real_concept& v)
293 { return boost::math::ltrunc(v.value(), policies::policy<>()); }
295 template <class Policy>
296 inline long long lltrunc(const concepts::real_concept& v, const Policy& pol)
297 { return boost::math::lltrunc(v.value(), pol); }
298 inline long long lltrunc(const concepts::real_concept& v)
299 { return boost::math::lltrunc(v.value(), policies::policy<>()); }
302 template <class charT, class traits>
303 inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const real_concept& a)
305 return os << a.value();
307 template <class charT, class traits>
308 inline std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& is, real_concept& a)
310 real_concept_base_type v;
316 } // namespace concepts
322 inline concepts::real_concept make_big_value<concepts::real_concept>(boost::math::tools::largest_float val, const char* , std::false_type const&, std::false_type const&)
324 return val; // Can't use lexical_cast here, sometimes it fails....
328 inline concepts::real_concept max_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
330 return max_value<concepts::real_concept_base_type>();
334 inline concepts::real_concept min_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
336 return min_value<concepts::real_concept_base_type>();
340 inline concepts::real_concept log_max_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
342 return log_max_value<concepts::real_concept_base_type>();
346 inline concepts::real_concept log_min_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
348 return log_min_value<concepts::real_concept_base_type>();
352 inline concepts::real_concept epsilon<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
355 return std::numeric_limits<concepts::real_concept_base_type>::epsilon();
357 return tools::epsilon<concepts::real_concept_base_type>();
362 inline constexpr int digits<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept)) noexcept
364 // Assume number of significand bits is same as real_concept_base_type,
365 // unless std::numeric_limits<T>::is_specialized to provide digits.
366 return tools::digits<concepts::real_concept_base_type>();
367 // Note that if numeric_limits real concept is NOT specialized to provide digits10
368 // (or max_digits10) then the default precision of 6 decimal digits will be used
369 // by Boost test (giving misleading error messages like
370 // "difference between {9.79796} and {9.79796} exceeds 5.42101e-19%"
371 // and by Boost lexical cast and serialization causing loss of accuracy.
378 #endif // BOOST_MATH_REAL_CONCEPT_HPP