]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
7c673cae FG |
19 | #include <boost/math/policies/policy.hpp> |
20 | #include <boost/math/special_functions/math_fwd.hpp> | |
1e59de90 | 21 | #include <limits> |
7c673cae FG |
22 | #include <ostream> |
23 | #include <istream> | |
1e59de90 | 24 | #include <cmath> |
7c673cae FG |
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)){} | |
7c673cae | 60 | #endif |
1e59de90 TL |
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)){} | |
7c673cae FG |
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; } | |
7c673cae | 86 | #endif |
1e59de90 TL |
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 | ||
7c673cae FG |
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 | ||
94 | // Access: | |
95 | std_real_concept_base_type value()const{ return m_value; } | |
96 | ||
97 | // Member arithmetic: | |
98 | std_real_concept& operator+=(const std_real_concept& other) | |
99 | { m_value += other.value(); return *this; } | |
100 | std_real_concept& operator-=(const std_real_concept& other) | |
101 | { m_value -= other.value(); return *this; } | |
102 | std_real_concept& operator*=(const std_real_concept& other) | |
103 | { m_value *= other.value(); return *this; } | |
104 | std_real_concept& operator/=(const std_real_concept& other) | |
105 | { m_value /= other.value(); return *this; } | |
106 | std_real_concept operator-()const | |
107 | { return -m_value; } | |
108 | std_real_concept const& operator+()const | |
109 | { return *this; } | |
110 | ||
111 | private: | |
112 | std_real_concept_base_type m_value; | |
113 | }; | |
114 | ||
115 | // Non-member arithmetic: | |
116 | inline std_real_concept operator+(const std_real_concept& a, const std_real_concept& b) | |
117 | { | |
118 | std_real_concept result(a); | |
119 | result += b; | |
120 | return result; | |
121 | } | |
122 | inline std_real_concept operator-(const std_real_concept& a, const std_real_concept& b) | |
123 | { | |
124 | std_real_concept result(a); | |
125 | result -= b; | |
126 | return result; | |
127 | } | |
128 | inline std_real_concept operator*(const std_real_concept& a, const std_real_concept& b) | |
129 | { | |
130 | std_real_concept result(a); | |
131 | result *= b; | |
132 | return result; | |
133 | } | |
134 | inline std_real_concept operator/(const std_real_concept& a, const std_real_concept& b) | |
135 | { | |
136 | std_real_concept result(a); | |
137 | result /= b; | |
138 | return result; | |
139 | } | |
140 | ||
141 | // Comparison: | |
142 | inline bool operator == (const std_real_concept& a, const std_real_concept& b) | |
143 | { return a.value() == b.value(); } | |
144 | inline bool operator != (const std_real_concept& a, const std_real_concept& b) | |
145 | { return a.value() != b.value();} | |
146 | inline bool operator < (const std_real_concept& a, const std_real_concept& b) | |
147 | { return a.value() < b.value(); } | |
148 | inline bool operator <= (const std_real_concept& a, const std_real_concept& b) | |
149 | { return a.value() <= b.value(); } | |
150 | inline bool operator > (const std_real_concept& a, const std_real_concept& b) | |
151 | { return a.value() > b.value(); } | |
152 | inline bool operator >= (const std_real_concept& a, const std_real_concept& b) | |
153 | { return a.value() >= b.value(); } | |
154 | ||
155 | } // namespace concepts | |
156 | } // namespace math | |
157 | } // namespace boost | |
158 | ||
159 | namespace std{ | |
160 | ||
161 | // Non-member functions: | |
162 | inline boost::math::concepts::std_real_concept acos(boost::math::concepts::std_real_concept a) | |
163 | { return std::acos(a.value()); } | |
164 | inline boost::math::concepts::std_real_concept cos(boost::math::concepts::std_real_concept a) | |
165 | { return std::cos(a.value()); } | |
166 | inline boost::math::concepts::std_real_concept asin(boost::math::concepts::std_real_concept a) | |
167 | { return std::asin(a.value()); } | |
168 | inline boost::math::concepts::std_real_concept atan(boost::math::concepts::std_real_concept a) | |
169 | { return std::atan(a.value()); } | |
170 | inline boost::math::concepts::std_real_concept atan2(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept b) | |
171 | { return std::atan2(a.value(), b.value()); } | |
172 | inline boost::math::concepts::std_real_concept ceil(boost::math::concepts::std_real_concept a) | |
173 | { return std::ceil(a.value()); } | |
174 | #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS | |
175 | inline boost::math::concepts::std_real_concept fmod(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept b) | |
176 | { return fmodl(a.value(), b.value()); } | |
177 | #else | |
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 std::fmod(a.value(), b.value()); } | |
180 | #endif | |
181 | inline boost::math::concepts::std_real_concept cosh(boost::math::concepts::std_real_concept a) | |
182 | { return std::cosh(a.value()); } | |
183 | inline boost::math::concepts::std_real_concept exp(boost::math::concepts::std_real_concept a) | |
184 | { return std::exp(a.value()); } | |
185 | inline boost::math::concepts::std_real_concept fabs(boost::math::concepts::std_real_concept a) | |
186 | { return std::fabs(a.value()); } | |
187 | inline boost::math::concepts::std_real_concept abs(boost::math::concepts::std_real_concept a) | |
188 | { return std::abs(a.value()); } | |
189 | inline boost::math::concepts::std_real_concept floor(boost::math::concepts::std_real_concept a) | |
190 | { return std::floor(a.value()); } | |
191 | inline boost::math::concepts::std_real_concept modf(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept* ipart) | |
192 | { | |
193 | boost::math::concepts::std_real_concept_base_type ip; | |
194 | boost::math::concepts::std_real_concept_base_type result = std::modf(a.value(), &ip); | |
195 | *ipart = ip; | |
196 | return result; | |
197 | } | |
198 | inline boost::math::concepts::std_real_concept frexp(boost::math::concepts::std_real_concept a, int* expon) | |
199 | { return std::frexp(a.value(), expon); } | |
200 | inline boost::math::concepts::std_real_concept ldexp(boost::math::concepts::std_real_concept a, int expon) | |
201 | { return std::ldexp(a.value(), expon); } | |
202 | inline boost::math::concepts::std_real_concept log(boost::math::concepts::std_real_concept a) | |
203 | { return std::log(a.value()); } | |
204 | inline boost::math::concepts::std_real_concept log10(boost::math::concepts::std_real_concept a) | |
205 | { return std::log10(a.value()); } | |
206 | inline boost::math::concepts::std_real_concept tan(boost::math::concepts::std_real_concept a) | |
207 | { return std::tan(a.value()); } | |
208 | inline boost::math::concepts::std_real_concept pow(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept b) | |
209 | { return std::pow(a.value(), b.value()); } | |
210 | #if !defined(__SUNPRO_CC) | |
211 | inline boost::math::concepts::std_real_concept pow(boost::math::concepts::std_real_concept a, int b) | |
212 | { return std::pow(a.value(), b); } | |
213 | #else | |
214 | inline boost::math::concepts::std_real_concept pow(boost::math::concepts::std_real_concept a, int b) | |
215 | { return std::pow(a.value(), static_cast<long double>(b)); } | |
216 | #endif | |
217 | inline boost::math::concepts::std_real_concept sin(boost::math::concepts::std_real_concept a) | |
218 | { return std::sin(a.value()); } | |
219 | inline boost::math::concepts::std_real_concept sinh(boost::math::concepts::std_real_concept a) | |
220 | { return std::sinh(a.value()); } | |
221 | inline boost::math::concepts::std_real_concept sqrt(boost::math::concepts::std_real_concept a) | |
222 | { return std::sqrt(a.value()); } | |
223 | inline boost::math::concepts::std_real_concept tanh(boost::math::concepts::std_real_concept a) | |
224 | { return std::tanh(a.value()); } | |
11fdf7f2 TL |
225 | inline boost::math::concepts::std_real_concept (nextafter)(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept b) |
226 | { return (boost::math::nextafter)(a, b); } | |
b32b8144 FG |
227 | // |
228 | // C++11 ism's | |
229 | // Note that these must not actually call the std:: versions as that precludes using this | |
230 | // header to test in C++03 mode, call the Boost versions instead: | |
231 | // | |
232 | inline boost::math::concepts::std_real_concept asinh(boost::math::concepts::std_real_concept a) | |
233 | { return boost::math::asinh(a.value(), boost::math::policies::make_policy(boost::math::policies::overflow_error<boost::math::policies::ignore_error>())); } | |
234 | inline boost::math::concepts::std_real_concept acosh(boost::math::concepts::std_real_concept a) | |
235 | { return boost::math::acosh(a.value(), boost::math::policies::make_policy(boost::math::policies::overflow_error<boost::math::policies::ignore_error>())); } | |
236 | inline boost::math::concepts::std_real_concept atanh(boost::math::concepts::std_real_concept a) | |
237 | { return boost::math::atanh(a.value(), boost::math::policies::make_policy(boost::math::policies::overflow_error<boost::math::policies::ignore_error>())); } | |
11fdf7f2 TL |
238 | inline bool (isfinite)(boost::math::concepts::std_real_concept a) |
239 | { | |
240 | return (boost::math::isfinite)(a.value()); | |
241 | } | |
b32b8144 | 242 | |
7c673cae FG |
243 | |
244 | } // namespace std | |
245 | ||
246 | #include <boost/math/special_functions/round.hpp> | |
247 | #include <boost/math/special_functions/trunc.hpp> | |
248 | #include <boost/math/special_functions/modf.hpp> | |
249 | #include <boost/math/tools/precision.hpp> | |
250 | ||
251 | namespace boost{ namespace math{ namespace concepts{ | |
252 | ||
253 | // | |
254 | // Conversion and truncation routines: | |
255 | // | |
256 | template <class Policy> | |
257 | inline int iround(const concepts::std_real_concept& v, const Policy& pol) | |
258 | { | |
259 | return boost::math::iround(v.value(), pol); | |
260 | } | |
261 | inline int iround(const concepts::std_real_concept& v) | |
262 | { | |
263 | return boost::math::iround(v.value(), policies::policy<>()); | |
264 | } | |
265 | ||
266 | template <class Policy> | |
267 | inline long lround(const concepts::std_real_concept& v, const Policy& pol) | |
268 | { | |
269 | return boost::math::lround(v.value(), pol); | |
270 | } | |
271 | inline long lround(const concepts::std_real_concept& v) | |
272 | { | |
273 | return boost::math::lround(v.value(), policies::policy<>()); | |
274 | } | |
275 | ||
7c673cae | 276 | template <class Policy> |
1e59de90 | 277 | inline long long llround(const concepts::std_real_concept& v, const Policy& pol) |
7c673cae FG |
278 | { |
279 | return boost::math::llround(v.value(), pol); | |
280 | } | |
1e59de90 | 281 | inline long long llround(const concepts::std_real_concept& v) |
7c673cae FG |
282 | { |
283 | return boost::math::llround(v.value(), policies::policy<>()); | |
284 | } | |
285 | ||
7c673cae FG |
286 | template <class Policy> |
287 | inline int itrunc(const concepts::std_real_concept& v, const Policy& pol) | |
288 | { | |
289 | return boost::math::itrunc(v.value(), pol); | |
290 | } | |
291 | inline int itrunc(const concepts::std_real_concept& v) | |
292 | { | |
293 | return boost::math::itrunc(v.value(), policies::policy<>()); | |
294 | } | |
295 | ||
296 | template <class Policy> | |
297 | inline long ltrunc(const concepts::std_real_concept& v, const Policy& pol) | |
298 | { | |
299 | return boost::math::ltrunc(v.value(), pol); | |
300 | } | |
301 | inline long ltrunc(const concepts::std_real_concept& v) | |
302 | { | |
303 | return boost::math::ltrunc(v.value(), policies::policy<>()); | |
304 | } | |
305 | ||
7c673cae | 306 | template <class Policy> |
1e59de90 | 307 | inline long long lltrunc(const concepts::std_real_concept& v, const Policy& pol) |
7c673cae FG |
308 | { |
309 | return boost::math::lltrunc(v.value(), pol); | |
310 | } | |
1e59de90 | 311 | inline long long lltrunc(const concepts::std_real_concept& v) |
7c673cae FG |
312 | { |
313 | return boost::math::lltrunc(v.value(), policies::policy<>()); | |
314 | } | |
315 | ||
7c673cae FG |
316 | // Streaming: |
317 | template <class charT, class traits> | |
318 | inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const std_real_concept& a) | |
319 | { | |
320 | return os << a.value(); | |
321 | } | |
322 | template <class charT, class traits> | |
323 | inline std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& is, std_real_concept& a) | |
324 | { | |
325 | #if defined(__SGI_STL_PORT) || defined(_RWSTD_VER) || defined(__LIBCOMO__) || defined(_LIBCPP_VERSION) | |
326 | std::string s; | |
327 | std_real_concept_base_type d; | |
328 | is >> s; | |
329 | std::sscanf(s.c_str(), "%Lf", &d); | |
330 | a = d; | |
331 | return is; | |
332 | #else | |
333 | std_real_concept_base_type v; | |
334 | is >> v; | |
335 | a = v; | |
336 | return is; | |
337 | #endif | |
338 | } | |
339 | ||
340 | } // namespace concepts | |
341 | }} | |
342 | ||
343 | #include <boost/math/tools/precision.hpp> | |
344 | #include <boost/math/tools/big_constant.hpp> | |
345 | ||
346 | namespace boost{ namespace math{ | |
347 | namespace tools | |
348 | { | |
349 | ||
350 | template <> | |
1e59de90 | 351 | 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&) |
7c673cae FG |
352 | { |
353 | return val; // Can't use lexical_cast here, sometimes it fails.... | |
354 | } | |
355 | ||
356 | template <> | |
357 | inline concepts::std_real_concept max_value<concepts::std_real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept)) | |
358 | { | |
359 | return max_value<concepts::std_real_concept_base_type>(); | |
360 | } | |
361 | ||
362 | template <> | |
363 | inline concepts::std_real_concept min_value<concepts::std_real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept)) | |
364 | { | |
365 | return min_value<concepts::std_real_concept_base_type>(); | |
366 | } | |
367 | ||
368 | template <> | |
369 | inline concepts::std_real_concept log_max_value<concepts::std_real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept)) | |
370 | { | |
371 | return log_max_value<concepts::std_real_concept_base_type>(); | |
372 | } | |
373 | ||
374 | template <> | |
375 | inline concepts::std_real_concept log_min_value<concepts::std_real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept)) | |
376 | { | |
377 | return log_min_value<concepts::std_real_concept_base_type>(); | |
378 | } | |
379 | ||
380 | template <> | |
381 | inline concepts::std_real_concept epsilon(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept)) | |
382 | { | |
383 | return tools::epsilon<concepts::std_real_concept_base_type>(); | |
384 | } | |
385 | ||
386 | template <> | |
1e59de90 | 387 | inline constexpr int digits<concepts::std_real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept)) noexcept |
7c673cae FG |
388 | { // Assume number of significand bits is same as std_real_concept_base_type, |
389 | // unless std::numeric_limits<T>::is_specialized to provide digits. | |
390 | return digits<concepts::std_real_concept_base_type>(); | |
391 | } | |
392 | ||
11fdf7f2 TL |
393 | template <> |
394 | inline double real_cast<double, concepts::std_real_concept>(concepts::std_real_concept r) | |
395 | { | |
396 | return static_cast<double>(r.value()); | |
397 | } | |
398 | ||
399 | ||
7c673cae FG |
400 | } // namespace tools |
401 | ||
1e59de90 | 402 | #if defined(_MSC_VER) && (_MSC_VER <= 1310) |
7c673cae FG |
403 | using concepts::itrunc; |
404 | using concepts::ltrunc; | |
405 | using concepts::lltrunc; | |
406 | using concepts::iround; | |
407 | using concepts::lround; | |
408 | using concepts::llround; | |
409 | #endif | |
410 | ||
411 | } // namespace math | |
412 | } // namespace boost | |
413 | ||
b32b8144 FG |
414 | // |
415 | // These must go at the end, as they include stuff that won't compile until | |
416 | // after std_real_concept has been defined: | |
417 | // | |
418 | #include <boost/math/special_functions/acosh.hpp> | |
419 | #include <boost/math/special_functions/asinh.hpp> | |
420 | #include <boost/math/special_functions/atanh.hpp> | |
7c673cae | 421 | |
b32b8144 | 422 | #endif // BOOST_MATH_STD_REAL_CONCEPT_HPP |