]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/math/concepts/real_concept.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / math / concepts / real_concept.hpp
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)
5
6 // Test real concept.
7
8 // real_concept is an archetype for User defined Real types.
9
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)
14 // can be used.
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".
22
23 #ifndef BOOST_MATH_REAL_CONCEPT_HPP
24 #define BOOST_MATH_REAL_CONCEPT_HPP
25
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>
37 #endif
38 #include <ostream>
39 #include <istream>
40 #include <limits>
41 #include <cmath>
42 #include <cstdint>
43
44 #if defined(__SGI_STL_PORT) || defined(_RWSTD_VER) || defined(__LIBCOMO__)
45 # include <cstdio>
46 #endif
47
48 namespace boost{ namespace math{
49
50 namespace concepts
51 {
52
53 #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
54 typedef double real_concept_base_type;
55 #else
56 typedef long double real_concept_base_type;
57 #endif
58
59 class real_concept
60 {
61 public:
62 // Constructors:
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){}
81 #endif
82
83 // Assignment:
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; }
99
100 // Access:
101 real_concept_base_type value()const{ return m_value; }
102
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
113 { return -m_value; }
114 real_concept const& operator+()const
115 { return *this; }
116 real_concept& operator++()
117 { ++m_value; return *this; }
118 real_concept& operator--()
119 { --m_value; return *this; }
120
121 private:
122 real_concept_base_type m_value;
123 };
124
125 // Non-member arithmetic:
126 inline real_concept operator+(const real_concept& a, const real_concept& b)
127 {
128 real_concept result(a);
129 result += b;
130 return result;
131 }
132 inline real_concept operator-(const real_concept& a, const real_concept& b)
133 {
134 real_concept result(a);
135 result -= b;
136 return result;
137 }
138 inline real_concept operator*(const real_concept& a, const real_concept& b)
139 {
140 real_concept result(a);
141 result *= b;
142 return result;
143 }
144 inline real_concept operator/(const real_concept& a, const real_concept& b)
145 {
146 real_concept result(a);
147 result /= b;
148 return result;
149 }
150
151 // Comparison:
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(); }
164
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:
181 #ifdef _WIN32_WCE
182 //
183 // Ugly workaround for macro fmodl:
184 //
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()); }
189 #else
190 inline real_concept fmod(real_concept a, real_concept b)
191 { return fmodl(a.value(), b.value()); }
192 #endif
193 #endif
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)
205 {
206 #ifdef __MINGW32__
207 real_concept_base_type ip;
208 real_concept_base_type result = boost::math::modf(a.value(), &ip);
209 *ipart = ip;
210 return result;
211 #else
212 real_concept_base_type ip;
213 real_concept_base_type result = std::modf(a.value(), &ip);
214 *ipart = ip;
215 return result;
216 #endif
217 }
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); }
233 #else
234 inline real_concept pow(real_concept a, int b)
235 { return std::pow(a.value(), static_cast<real_concept_base_type>(b)); }
236 #endif
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()); }
245
246 //
247 // C++11 ism's
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:
250 //
251 inline boost::math::concepts::real_concept asinh(boost::math::concepts::real_concept a)
252 {
253 return boost::math::asinh(a.value(), boost::math::policies::make_policy(boost::math::policies::overflow_error<boost::math::policies::ignore_error>()));
254 }
255 inline boost::math::concepts::real_concept acosh(boost::math::concepts::real_concept a)
256 {
257 return boost::math::acosh(a.value(), boost::math::policies::make_policy(boost::math::policies::overflow_error<boost::math::policies::ignore_error>()));
258 }
259 inline boost::math::concepts::real_concept atanh(boost::math::concepts::real_concept a)
260 {
261 return boost::math::atanh(a.value(), boost::math::policies::make_policy(boost::math::policies::overflow_error<boost::math::policies::ignore_error>()));
262 }
263
264 //
265 // Conversion and truncation routines:
266 //
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<>()); }
277
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<>()); }
283
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<>()); }
294
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<>()); }
300
301 // Streaming:
302 template <class charT, class traits>
303 inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const real_concept& a)
304 {
305 return os << a.value();
306 }
307 template <class charT, class traits>
308 inline std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& is, real_concept& a)
309 {
310 real_concept_base_type v;
311 is >> v;
312 a = v;
313 return is;
314 }
315
316 } // namespace concepts
317
318 namespace tools
319 {
320
321 template <>
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&)
323 {
324 return val; // Can't use lexical_cast here, sometimes it fails....
325 }
326
327 template <>
328 inline concepts::real_concept max_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
329 {
330 return max_value<concepts::real_concept_base_type>();
331 }
332
333 template <>
334 inline concepts::real_concept min_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
335 {
336 return min_value<concepts::real_concept_base_type>();
337 }
338
339 template <>
340 inline concepts::real_concept log_max_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
341 {
342 return log_max_value<concepts::real_concept_base_type>();
343 }
344
345 template <>
346 inline concepts::real_concept log_min_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
347 {
348 return log_min_value<concepts::real_concept_base_type>();
349 }
350
351 template <>
352 inline concepts::real_concept epsilon<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
353 {
354 #ifdef __SUNPRO_CC
355 return std::numeric_limits<concepts::real_concept_base_type>::epsilon();
356 #else
357 return tools::epsilon<concepts::real_concept_base_type>();
358 #endif
359 }
360
361 template <>
362 inline constexpr int digits<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept)) noexcept
363 {
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.
372 }
373
374 } // namespace tools
375 } // namespace math
376 } // namespace boost
377
378 #endif // BOOST_MATH_REAL_CONCEPT_HPP
379
380