Debian Codegraph @

third-party/boost-math/include/boost/math/concepts/std_real_concept.hpp

raw
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 // std_real_concept is an archetype for built-in Real types.
7
8 // The main purpose in providing this type is to verify
9 // that std lib functions are found via a using declaration
10 // bringing those functions into the current scope, and not
11 // just because they happen to be in global scope.
12 //
13 // If ::pow is found rather than std::pow say, then the code
14 // will silently compile, but truncation of long doubles to
15 // double will cause a significant loss of precision.
16 // A template instantiated with std_real_concept will *only*
17 // compile if it std::whatever is in scope.
18
19 #include <boost/math/policies/policy.hpp>
20 #include <boost/math/special_functions/math_fwd.hpp>
21 #include <limits>
22 #include <ostream>
23 #include <istream>
24 #include <cmath>
25
26 #ifndef BOOST_MATH_STD_REAL_CONCEPT_HPP
27 #define BOOST_MATH_STD_REAL_CONCEPT_HPP
28
29 namespace boost{ namespace math{
30
31 namespace concepts
32 {
33
34 #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
35 typedef double std_real_concept_base_type;
36 #else
37 typedef long double std_real_concept_base_type;
38 #endif
39
40 class std_real_concept
41 {
42 public:
43 // Constructors:
44 std_real_concept() : m_value(0){}
45 std_real_concept(char c) : m_value(c){}
46 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
47 std_real_concept(wchar_t c) : m_value(c){}
48 #endif
49 std_real_concept(unsigned char c) : m_value(c){}
50 std_real_concept(signed char c) : m_value(c){}
51 std_real_concept(unsigned short c) : m_value(c){}
52 std_real_concept(short c) : m_value(c){}
53 std_real_concept(unsigned int c) : m_value(c){}
54 std_real_concept(int c) : m_value(c){}
55 std_real_concept(unsigned long c) : m_value(c){}
56 std_real_concept(long c) : m_value(c){}
57 #if defined(__DECCXX) || defined(__SUNPRO_CC)
58 std_real_concept(unsigned long long c) : m_value(static_cast<std_real_concept_base_type>(c)){}
59 std_real_concept(long long c) : m_value(static_cast<std_real_concept_base_type>(c)){}
60 #endif
61 std_real_concept(unsigned long long c) : m_value(static_cast<std_real_concept_base_type>(c)){}
62 std_real_concept(long long c) : m_value(static_cast<std_real_concept_base_type>(c)){}
63 std_real_concept(float c) : m_value(c){}
64 std_real_concept(double c) : m_value(c){}
65 std_real_concept(long double c) : m_value(c){}
66 #ifdef BOOST_MATH_USE_FLOAT128
67 std_real_concept(BOOST_MATH_FLOAT128_TYPE c) : m_value(c){}
68 #endif
69
70 // Assignment:
71 std_real_concept& operator=(char c) { m_value = c; return *this; }
72 std_real_concept& operator=(unsigned char c) { m_value = c; return *this; }
73 std_real_concept& operator=(signed char c) { m_value = c; return *this; }
74 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
75 std_real_concept& operator=(wchar_t c) { m_value = c; return *this; }
76 #endif
77 std_real_concept& operator=(short c) { m_value = c; return *this; }
78 std_real_concept& operator=(unsigned short c) { m_value = c; return *this; }
79 std_real_concept& operator=(int c) { m_value = c; return *this; }
80 std_real_concept& operator=(unsigned int c) { m_value = c; return *this; }
81 std_real_concept& operator=(long c) { m_value = c; return *this; }
82 std_real_concept& operator=(unsigned long c) { m_value = c; return *this; }
83 #if defined(__DECCXX) || defined(__SUNPRO_CC)
84 std_real_concept& operator=(unsigned long long c) { m_value = static_cast<std_real_concept_base_type>(c); return *this; }
85 std_real_concept& operator=(long long c) { m_value = static_cast<std_real_concept_base_type>(c); return *this; }
86 #endif
87 std_real_concept& operator=(long long c) { m_value = static_cast<std_real_concept_base_type>(c); return *this; }
88 std_real_concept& operator=(unsigned long long c) { m_value = static_cast<std_real_concept_base_type>(c); return *this; }
89
90 std_real_concept& operator=(float c) { m_value = c; return *this; }
91 std_real_concept& operator=(double c) { m_value = c; return *this; }
92 std_real_concept& operator=(long double c) { m_value = c; return *this; }
93 #ifdef BOOST_MATH_USE_FLOAT128
94 std_real_concept& operator=(BOOST_MATH_FLOAT128_TYPE c) { m_value = c; return *this; }
95 #endif
96
97 // Access:
98 std_real_concept_base_type value()const{ return m_value; }
99
100 // Member arithmetic:
101 std_real_concept& operator+=(const std_real_concept& other)
102 { m_value += other.value(); return *this; }
103 std_real_concept& operator-=(const std_real_concept& other)
104 { m_value -= other.value(); return *this; }
105 std_real_concept& operator*=(const std_real_concept& other)
106 { m_value *= other.value(); return *this; }
107 std_real_concept& operator/=(const std_real_concept& other)
108 { m_value /= other.value(); return *this; }
109 std_real_concept operator-()const
110 { return -m_value; }
111 std_real_concept const& operator+()const
112 { return *this; }
113
114 private:
115 std_real_concept_base_type m_value;
116 };
117
118 // Non-member arithmetic:
119 inline std_real_concept operator+(const std_real_concept& a, const std_real_concept& b)
120 {
121 std_real_concept result(a);
122 result += b;
123 return result;
124 }
125 inline std_real_concept operator-(const std_real_concept& a, const std_real_concept& b)
126 {
127 std_real_concept result(a);
128 result -= b;
129 return result;
130 }
131 inline std_real_concept operator*(const std_real_concept& a, const std_real_concept& b)
132 {
133 std_real_concept result(a);
134 result *= b;
135 return result;
136 }
137 inline std_real_concept operator/(const std_real_concept& a, const std_real_concept& b)
138 {
139 std_real_concept result(a);
140 result /= b;
141 return result;
142 }
143
144 // Comparison:
145 inline bool operator == (const std_real_concept& a, const std_real_concept& b)
146 { return a.value() == b.value(); }
147 inline bool operator != (const std_real_concept& a, const std_real_concept& b)
148 { return a.value() != b.value();}
149 inline bool operator < (const std_real_concept& a, const std_real_concept& b)
150 { return a.value() < b.value(); }
151 inline bool operator <= (const std_real_concept& a, const std_real_concept& b)
152 { return a.value() <= b.value(); }
153 inline bool operator > (const std_real_concept& a, const std_real_concept& b)
154 { return a.value() > b.value(); }
155 inline bool operator >= (const std_real_concept& a, const std_real_concept& b)
156 { return a.value() >= b.value(); }
157
158 } // namespace concepts
159 } // namespace math
160 } // namespace boost
161
162 namespace std{
163
164 // Non-member functions:
165 inline boost::math::concepts::std_real_concept acos(boost::math::concepts::std_real_concept a)
166 { return std::acos(a.value()); }
167 inline boost::math::concepts::std_real_concept cos(boost::math::concepts::std_real_concept a)
168 { return std::cos(a.value()); }
169 inline boost::math::concepts::std_real_concept asin(boost::math::concepts::std_real_concept a)
170 { return std::asin(a.value()); }
171 inline boost::math::concepts::std_real_concept atan(boost::math::concepts::std_real_concept a)
172 { return std::atan(a.value()); }
173 inline boost::math::concepts::std_real_concept atan2(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept b)
174 { return std::atan2(a.value(), b.value()); }
175 inline boost::math::concepts::std_real_concept ceil(boost::math::concepts::std_real_concept a)
176 { return std::ceil(a.value()); }
177 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
178 inline boost::math::concepts::std_real_concept fmod(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept b)
179 { return fmodl(a.value(), b.value()); }
180 #else
181 inline boost::math::concepts::std_real_concept fmod(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept b)
182 { return std::fmod(a.value(), b.value()); }
183 #endif
184 inline boost::math::concepts::std_real_concept cosh(boost::math::concepts::std_real_concept a)
185 { return std::cosh(a.value()); }
186 inline boost::math::concepts::std_real_concept exp(boost::math::concepts::std_real_concept a)
187 { return std::exp(a.value()); }
188 inline boost::math::concepts::std_real_concept fabs(boost::math::concepts::std_real_concept a)
189 { return std::fabs(a.value()); }
190 inline boost::math::concepts::std_real_concept abs(boost::math::concepts::std_real_concept a)
191 { return std::abs(a.value()); }
192 inline boost::math::concepts::std_real_concept floor(boost::math::concepts::std_real_concept a)
193 { return std::floor(a.value()); }
194 inline boost::math::concepts::std_real_concept modf(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept* ipart)
195 {
196 boost::math::concepts::std_real_concept_base_type ip;
197 boost::math::concepts::std_real_concept_base_type result = std::modf(a.value(), &ip);
198 *ipart = ip;
199 return result;
200 }
201 inline boost::math::concepts::std_real_concept frexp(boost::math::concepts::std_real_concept a, int* expon)
202 { return std::frexp(a.value(), expon); }
203 inline boost::math::concepts::std_real_concept ldexp(boost::math::concepts::std_real_concept a, int expon)
204 { return std::ldexp(a.value(), expon); }
205 inline boost::math::concepts::std_real_concept log(boost::math::concepts::std_real_concept a)
206 { return std::log(a.value()); }
207 inline boost::math::concepts::std_real_concept log10(boost::math::concepts::std_real_concept a)
208 { return std::log10(a.value()); }
209 inline boost::math::concepts::std_real_concept tan(boost::math::concepts::std_real_concept a)
210 { return std::tan(a.value()); }
211 inline boost::math::concepts::std_real_concept pow(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept b)
212 { return std::pow(a.value(), b.value()); }
213 #if !defined(__SUNPRO_CC)
214 inline boost::math::concepts::std_real_concept pow(boost::math::concepts::std_real_concept a, int b)
215 { return std::pow(a.value(), b); }
216 #else
217 inline boost::math::concepts::std_real_concept pow(boost::math::concepts::std_real_concept a, int b)
218 { return std::pow(a.value(), static_cast<long double>(b)); }
219 #endif
220 inline boost::math::concepts::std_real_concept sin(boost::math::concepts::std_real_concept a)
221 { return std::sin(a.value()); }
222 inline boost::math::concepts::std_real_concept sinh(boost::math::concepts::std_real_concept a)
223 { return std::sinh(a.value()); }
224 inline boost::math::concepts::std_real_concept sqrt(boost::math::concepts::std_real_concept a)
225 { return std::sqrt(a.value()); }
226 inline boost::math::concepts::std_real_concept tanh(boost::math::concepts::std_real_concept a)
227 { return std::tanh(a.value()); }
228 inline boost::math::concepts::std_real_concept (nextafter)(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept b)
229 { return (boost::math::nextafter)(a, b); }
230 //
231 // C++11 ism's
232 // Now that we only support C++11 and later, we can allow use of these:
233 //
234 inline boost::math::concepts::std_real_concept asinh(boost::math::concepts::std_real_concept a)
235 { return std::asinh(a.value()); }
236 inline boost::math::concepts::std_real_concept acosh(boost::math::concepts::std_real_concept a)
237 { return std::acosh(a.value()); }
238 inline boost::math::concepts::std_real_concept atanh(boost::math::concepts::std_real_concept a)
239 { return std::atanh(a.value()); }
240 inline bool (isfinite)(boost::math::concepts::std_real_concept a)
241 {
242 return (boost::math::isfinite)(a.value());
243 }
244 inline boost::math::concepts::std_real_concept log2(boost::math::concepts::std_real_concept a)
245 { return std::log2(a.value()); }
246 inline int ilogb(boost::math::concepts::std_real_concept a)
247 { return std::ilogb(a.value()); }
248
249
250 } // namespace std
251
252 #include <boost/math/special_functions/round.hpp>
253 #include <boost/math/special_functions/trunc.hpp>
254 #include <boost/math/special_functions/modf.hpp>
255 #include <boost/math/tools/precision.hpp>
256
257 namespace boost{ namespace math{ namespace concepts{
258
259 //
260 // Conversion and truncation routines:
261 //
262 template <class Policy>
263 inline int iround(const concepts::std_real_concept& v, const Policy& pol)
264 {
265 return boost::math::iround(v.value(), pol);
266 }
267 inline int iround(const concepts::std_real_concept& v)
268 {
269 return boost::math::iround(v.value(), policies::policy<>());
270 }
271
272 template <class Policy>
273 inline long lround(const concepts::std_real_concept& v, const Policy& pol)
274 {
275 return boost::math::lround(v.value(), pol);
276 }
277 inline long lround(const concepts::std_real_concept& v)
278 {
279 return boost::math::lround(v.value(), policies::policy<>());
280 }
281
282 template <class Policy>
283 inline long long llround(const concepts::std_real_concept& v, const Policy& pol)
284 {
285 return boost::math::llround(v.value(), pol);
286 }
287 inline long long llround(const concepts::std_real_concept& v)
288 {
289 return boost::math::llround(v.value(), policies::policy<>());
290 }
291
292 template <class Policy>
293 inline int itrunc(const concepts::std_real_concept& v, const Policy& pol)
294 {
295 return boost::math::itrunc(v.value(), pol);
296 }
297 inline int itrunc(const concepts::std_real_concept& v)
298 {
299 return boost::math::itrunc(v.value(), policies::policy<>());
300 }
301
302 template <class Policy>
303 inline long ltrunc(const concepts::std_real_concept& v, const Policy& pol)
304 {
305 return boost::math::ltrunc(v.value(), pol);
306 }
307 inline long ltrunc(const concepts::std_real_concept& v)
308 {
309 return boost::math::ltrunc(v.value(), policies::policy<>());
310 }
311
312 template <class Policy>
313 inline long long lltrunc(const concepts::std_real_concept& v, const Policy& pol)
314 {
315 return boost::math::lltrunc(v.value(), pol);
316 }
317 inline long long lltrunc(const concepts::std_real_concept& v)
318 {
319 return boost::math::lltrunc(v.value(), policies::policy<>());
320 }
321
322 // Streaming:
323 template <class charT, class traits>
324 inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const std_real_concept& a)
325 {
326 return os << a.value();
327 }
328 template <class charT, class traits>
329 inline std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& is, std_real_concept& a)
330 {
331 #if defined(__SGI_STL_PORT) || defined(_RWSTD_VER) || defined(__LIBCOMO__) || defined(_LIBCPP_VERSION)
332 std::string s;
333 std_real_concept_base_type d;
334 is >> s;
335 std::sscanf(s.c_str(), "%Lf", &d);
336 a = d;
337 return is;
338 #else
339 std_real_concept_base_type v;
340 is >> v;
341 a = v;
342 return is;
343 #endif
344 }
345
346 } // namespace concepts
347 }}
348
349 #include <boost/math/tools/big_constant.hpp>
350
351 namespace boost{ namespace math{
352 namespace tools
353 {
354
355 template <>
356 inline concepts::std_real_concept make_big_value<concepts::std_real_concept>(boost::math::tools::largest_float val, const char*, std::false_type const&, std::false_type const&)
357 {
358 return val; // Can't use lexical_cast here, sometimes it fails....
359 }
360
361 template <>
362 inline concepts::std_real_concept max_value<concepts::std_real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept))
363 {
364 return max_value<concepts::std_real_concept_base_type>();
365 }
366
367 template <>
368 inline concepts::std_real_concept min_value<concepts::std_real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept))
369 {
370 return min_value<concepts::std_real_concept_base_type>();
371 }
372
373 template <>
374 inline concepts::std_real_concept log_max_value<concepts::std_real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept))
375 {
376 return log_max_value<concepts::std_real_concept_base_type>();
377 }
378
379 template <>
380 inline concepts::std_real_concept log_min_value<concepts::std_real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept))
381 {
382 return log_min_value<concepts::std_real_concept_base_type>();
383 }
384
385 template <>
386 inline concepts::std_real_concept epsilon(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept))
387 {
388 return tools::epsilon<concepts::std_real_concept_base_type>();
389 }
390
391 template <>
392 inline constexpr int digits<concepts::std_real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept)) noexcept
393 { // Assume number of significand bits is same as std_real_concept_base_type,
394 // unless std::numeric_limits<T>::is_specialized to provide digits.
395 return digits<concepts::std_real_concept_base_type>();
396 }
397
398 template <>
399 inline double real_cast<double, concepts::std_real_concept>(concepts::std_real_concept r)
400 {
401 return static_cast<double>(r.value());
402 }
403
404
405 } // namespace tools
406
407 #if defined(_MSC_VER) && (_MSC_VER <= 1310)
408 using concepts::itrunc;
409 using concepts::ltrunc;
410 using concepts::lltrunc;
411 using concepts::iround;
412 using concepts::lround;
413 using concepts::llround;
414 #endif
415
416 } // namespace math
417 } // namespace boost
418
419 //
420 // These must go at the end, as they include stuff that won't compile until
421 // after std_real_concept has been defined:
422 //
423 #include <boost/math/special_functions/acosh.hpp>
424 #include <boost/math/special_functions/asinh.hpp>
425 #include <boost/math/special_functions/atanh.hpp>
426
427 #endif // BOOST_MATH_STD_REAL_CONCEPT_HPP