]>
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 | // 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/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> | |
b32b8144 FG |
34 | #include <boost/math/special_functions/asinh.hpp> |
35 | #include <boost/math/special_functions/atanh.hpp> | |
7c673cae FG |
36 | #if defined(__SGI_STL_PORT) |
37 | # include <boost/math/tools/real_cast.hpp> | |
38 | #endif | |
39 | #include <ostream> | |
40 | #include <istream> | |
41 | #include <boost/config/no_tr1/cmath.hpp> | |
42 | #include <math.h> // fmodl | |
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 | #ifndef BOOST_NO_INTRINSIC_WCHAR_T | |
66 | real_concept(wchar_t c) : m_value(c){} | |
67 | #endif | |
68 | real_concept(unsigned char c) : m_value(c){} | |
69 | real_concept(signed char c) : m_value(c){} | |
70 | real_concept(unsigned short c) : m_value(c){} | |
71 | real_concept(short c) : m_value(c){} | |
72 | real_concept(unsigned int c) : m_value(c){} | |
73 | real_concept(int c) : m_value(c){} | |
74 | real_concept(unsigned long c) : m_value(c){} | |
75 | real_concept(long c) : m_value(c){} | |
76 | #if defined(__DECCXX) || defined(__SUNPRO_CC) | |
77 | real_concept(unsigned long long c) : m_value(static_cast<real_concept_base_type>(c)){} | |
78 | real_concept(long long c) : m_value(static_cast<real_concept_base_type>(c)){} | |
79 | #elif defined(BOOST_HAS_LONG_LONG) | |
80 | real_concept(boost::ulong_long_type c) : m_value(static_cast<real_concept_base_type>(c)){} | |
81 | real_concept(boost::long_long_type c) : m_value(static_cast<real_concept_base_type>(c)){} | |
82 | #elif defined(BOOST_HAS_MS_INT64) | |
83 | real_concept(unsigned __int64 c) : m_value(static_cast<real_concept_base_type>(c)){} | |
84 | real_concept(__int64 c) : m_value(static_cast<real_concept_base_type>(c)){} | |
85 | #endif | |
86 | real_concept(float c) : m_value(c){} | |
87 | real_concept(double c) : m_value(c){} | |
88 | real_concept(long double c) : m_value(c){} | |
89 | #ifdef BOOST_MATH_USE_FLOAT128 | |
90 | real_concept(BOOST_MATH_FLOAT128_TYPE c) : m_value(c){} | |
91 | #endif | |
92 | ||
93 | // Assignment: | |
94 | real_concept& operator=(char c) { m_value = c; return *this; } | |
95 | real_concept& operator=(unsigned char c) { m_value = c; return *this; } | |
96 | real_concept& operator=(signed char c) { m_value = c; return *this; } | |
97 | #ifndef BOOST_NO_INTRINSIC_WCHAR_T | |
98 | real_concept& operator=(wchar_t c) { m_value = c; return *this; } | |
99 | #endif | |
100 | real_concept& operator=(short c) { m_value = c; return *this; } | |
101 | real_concept& operator=(unsigned short c) { m_value = c; return *this; } | |
102 | real_concept& operator=(int c) { m_value = c; return *this; } | |
103 | real_concept& operator=(unsigned int c) { m_value = c; return *this; } | |
104 | real_concept& operator=(long c) { m_value = c; return *this; } | |
105 | real_concept& operator=(unsigned long c) { m_value = c; return *this; } | |
106 | #ifdef BOOST_HAS_LONG_LONG | |
107 | real_concept& operator=(boost::long_long_type c) { m_value = static_cast<real_concept_base_type>(c); return *this; } | |
108 | real_concept& operator=(boost::ulong_long_type c) { m_value = static_cast<real_concept_base_type>(c); return *this; } | |
109 | #endif | |
110 | real_concept& operator=(float c) { m_value = c; return *this; } | |
111 | real_concept& operator=(double c) { m_value = c; return *this; } | |
112 | real_concept& operator=(long double c) { m_value = c; return *this; } | |
113 | ||
114 | // Access: | |
115 | real_concept_base_type value()const{ return m_value; } | |
116 | ||
117 | // Member arithmetic: | |
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 real_concept& other) | |
125 | { m_value /= other.value(); return *this; } | |
126 | real_concept operator-()const | |
127 | { return -m_value; } | |
128 | real_concept const& operator+()const | |
129 | { return *this; } | |
130 | real_concept& operator++() | |
131 | { ++m_value; return *this; } | |
132 | real_concept& operator--() | |
133 | { --m_value; return *this; } | |
134 | ||
135 | private: | |
136 | real_concept_base_type m_value; | |
137 | }; | |
138 | ||
139 | // Non-member arithmetic: | |
140 | inline real_concept operator+(const real_concept& a, const real_concept& b) | |
141 | { | |
142 | real_concept result(a); | |
143 | result += b; | |
144 | return result; | |
145 | } | |
146 | inline real_concept operator-(const real_concept& a, const real_concept& b) | |
147 | { | |
148 | real_concept result(a); | |
149 | result -= b; | |
150 | return result; | |
151 | } | |
152 | inline real_concept operator*(const real_concept& a, const real_concept& b) | |
153 | { | |
154 | real_concept result(a); | |
155 | result *= b; | |
156 | return result; | |
157 | } | |
158 | inline real_concept operator/(const real_concept& a, const real_concept& b) | |
159 | { | |
160 | real_concept result(a); | |
161 | result /= b; | |
162 | return result; | |
163 | } | |
164 | ||
165 | // Comparison: | |
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(); } | |
176 | inline bool operator >= (const real_concept& a, const real_concept& b) | |
177 | { return a.value() >= b.value(); } | |
178 | ||
179 | // Non-member functions: | |
180 | inline real_concept acos(real_concept a) | |
181 | { return std::acos(a.value()); } | |
182 | inline real_concept cos(real_concept a) | |
183 | { return std::cos(a.value()); } | |
184 | inline real_concept asin(real_concept a) | |
185 | { return std::asin(a.value()); } | |
186 | inline real_concept atan(real_concept a) | |
187 | { return std::atan(a.value()); } | |
188 | inline real_concept atan2(real_concept a, real_concept b) | |
189 | { return std::atan2(a.value(), b.value()); } | |
190 | inline real_concept ceil(real_concept a) | |
191 | { return std::ceil(a.value()); } | |
192 | #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS | |
193 | // I've seen std::fmod(long double) crash on some platforms | |
194 | // so use fmodl instead: | |
195 | #ifdef _WIN32_WCE | |
196 | // | |
197 | // Ugly workaround for macro fmodl: | |
198 | // | |
199 | inline long double call_fmodl(long double a, long double b) | |
200 | { return fmodl(a, b); } | |
201 | inline real_concept fmod(real_concept a, real_concept b) | |
202 | { return call_fmodl(a.value(), b.value()); } | |
203 | #else | |
204 | inline real_concept fmod(real_concept a, real_concept b) | |
205 | { return fmodl(a.value(), b.value()); } | |
206 | #endif | |
207 | #endif | |
208 | inline real_concept cosh(real_concept a) | |
209 | { return std::cosh(a.value()); } | |
210 | inline real_concept exp(real_concept a) | |
211 | { return std::exp(a.value()); } | |
212 | inline real_concept fabs(real_concept a) | |
213 | { return std::fabs(a.value()); } | |
214 | inline real_concept abs(real_concept a) | |
215 | { return std::abs(a.value()); } | |
216 | inline real_concept floor(real_concept a) | |
217 | { return std::floor(a.value()); } | |
218 | inline real_concept modf(real_concept a, real_concept* ipart) | |
219 | { | |
220 | #ifdef __MINGW32__ | |
221 | real_concept_base_type ip; | |
222 | real_concept_base_type result = boost::math::modf(a.value(), &ip); | |
223 | *ipart = ip; | |
224 | return result; | |
225 | #else | |
226 | real_concept_base_type ip; | |
227 | real_concept_base_type result = std::modf(a.value(), &ip); | |
228 | *ipart = ip; | |
229 | return result; | |
230 | #endif | |
231 | } | |
232 | inline real_concept frexp(real_concept a, int* expon) | |
233 | { return std::frexp(a.value(), expon); } | |
234 | inline real_concept ldexp(real_concept a, int expon) | |
235 | { return std::ldexp(a.value(), expon); } | |
236 | inline real_concept log(real_concept a) | |
237 | { return std::log(a.value()); } | |
238 | inline real_concept log10(real_concept a) | |
239 | { return std::log10(a.value()); } | |
240 | inline real_concept tan(real_concept a) | |
241 | { return std::tan(a.value()); } | |
242 | inline real_concept pow(real_concept a, real_concept b) | |
243 | { return std::pow(a.value(), b.value()); } | |
244 | #if !defined(__SUNPRO_CC) | |
245 | inline real_concept pow(real_concept a, int b) | |
246 | { return std::pow(a.value(), b); } | |
247 | #else | |
248 | inline real_concept pow(real_concept a, int b) | |
249 | { return std::pow(a.value(), static_cast<real_concept_base_type>(b)); } | |
250 | #endif | |
251 | inline real_concept sin(real_concept a) | |
252 | { return std::sin(a.value()); } | |
253 | inline real_concept sinh(real_concept a) | |
254 | { return std::sinh(a.value()); } | |
255 | inline real_concept sqrt(real_concept a) | |
256 | { return std::sqrt(a.value()); } | |
257 | inline real_concept tanh(real_concept a) | |
258 | { return std::tanh(a.value()); } | |
259 | ||
260 | // | |
b32b8144 FG |
261 | // C++11 ism's |
262 | // Note that these must not actually call the std:: versions as that precludes using this | |
263 | // header to test in C++03 mode, call the Boost versions instead: | |
264 | // | |
265 | inline boost::math::concepts::real_concept asinh(boost::math::concepts::real_concept a) | |
266 | { | |
267 | return boost::math::asinh(a.value(), boost::math::policies::make_policy(boost::math::policies::overflow_error<boost::math::policies::ignore_error>())); | |
268 | } | |
269 | inline boost::math::concepts::real_concept acosh(boost::math::concepts::real_concept a) | |
270 | { | |
271 | return boost::math::acosh(a.value(), boost::math::policies::make_policy(boost::math::policies::overflow_error<boost::math::policies::ignore_error>())); | |
272 | } | |
273 | inline boost::math::concepts::real_concept atanh(boost::math::concepts::real_concept a) | |
274 | { | |
275 | return boost::math::atanh(a.value(), boost::math::policies::make_policy(boost::math::policies::overflow_error<boost::math::policies::ignore_error>())); | |
276 | } | |
277 | ||
278 | // | |
7c673cae FG |
279 | // Conversion and truncation routines: |
280 | // | |
281 | template <class Policy> | |
282 | inline int iround(const concepts::real_concept& v, const Policy& pol) | |
283 | { return boost::math::iround(v.value(), pol); } | |
284 | inline int iround(const concepts::real_concept& v) | |
285 | { return boost::math::iround(v.value(), policies::policy<>()); } | |
286 | template <class Policy> | |
287 | inline long lround(const concepts::real_concept& v, const Policy& pol) | |
288 | { return boost::math::lround(v.value(), pol); } | |
289 | inline long lround(const concepts::real_concept& v) | |
290 | { return boost::math::lround(v.value(), policies::policy<>()); } | |
291 | ||
292 | #ifdef BOOST_HAS_LONG_LONG | |
293 | template <class Policy> | |
294 | inline boost::long_long_type llround(const concepts::real_concept& v, const Policy& pol) | |
295 | { return boost::math::llround(v.value(), pol); } | |
296 | inline boost::long_long_type llround(const concepts::real_concept& v) | |
297 | { return boost::math::llround(v.value(), policies::policy<>()); } | |
298 | #endif | |
299 | ||
300 | template <class Policy> | |
301 | inline int itrunc(const concepts::real_concept& v, const Policy& pol) | |
302 | { return boost::math::itrunc(v.value(), pol); } | |
303 | inline int itrunc(const concepts::real_concept& v) | |
304 | { return boost::math::itrunc(v.value(), policies::policy<>()); } | |
305 | template <class Policy> | |
306 | inline long ltrunc(const concepts::real_concept& v, const Policy& pol) | |
307 | { return boost::math::ltrunc(v.value(), pol); } | |
308 | inline long ltrunc(const concepts::real_concept& v) | |
309 | { return boost::math::ltrunc(v.value(), policies::policy<>()); } | |
310 | ||
311 | #ifdef BOOST_HAS_LONG_LONG | |
312 | template <class Policy> | |
313 | inline boost::long_long_type lltrunc(const concepts::real_concept& v, const Policy& pol) | |
314 | { return boost::math::lltrunc(v.value(), pol); } | |
315 | inline boost::long_long_type lltrunc(const concepts::real_concept& v) | |
316 | { return boost::math::lltrunc(v.value(), policies::policy<>()); } | |
317 | #endif | |
318 | ||
319 | // Streaming: | |
320 | template <class charT, class traits> | |
321 | inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const real_concept& a) | |
322 | { | |
323 | return os << a.value(); | |
324 | } | |
325 | template <class charT, class traits> | |
326 | inline std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& is, real_concept& a) | |
327 | { | |
328 | #if defined(BOOST_MSVC) && defined(__SGI_STL_PORT) | |
329 | // | |
330 | // STLPort 5.1.4 has a problem reading long doubles from strings, | |
331 | // see http://sourceforge.net/tracker/index.php?func=detail&aid=1811043&group_id=146814&atid=766244 | |
332 | // | |
333 | double v; | |
334 | is >> v; | |
335 | a = v; | |
336 | return is; | |
337 | #elif defined(__SGI_STL_PORT) || defined(_RWSTD_VER) || defined(__LIBCOMO__) || defined(_LIBCPP_VERSION) | |
338 | std::string s; | |
339 | real_concept_base_type d; | |
340 | is >> s; | |
341 | std::sscanf(s.c_str(), "%Lf", &d); | |
342 | a = d; | |
343 | return is; | |
344 | #else | |
345 | real_concept_base_type v; | |
346 | is >> v; | |
347 | a = v; | |
348 | return is; | |
349 | #endif | |
350 | } | |
351 | ||
352 | } // namespace concepts | |
353 | ||
354 | namespace tools | |
355 | { | |
356 | ||
357 | template <> | |
f67539c2 | 358 | inline concepts::real_concept make_big_value<concepts::real_concept>(boost::math::tools::largest_float val, const char* , boost::false_type const&, boost::false_type const&) |
7c673cae FG |
359 | { |
360 | return val; // Can't use lexical_cast here, sometimes it fails.... | |
361 | } | |
362 | ||
363 | template <> | |
364 | inline concepts::real_concept max_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept)) | |
365 | { | |
366 | return max_value<concepts::real_concept_base_type>(); | |
367 | } | |
368 | ||
369 | template <> | |
370 | inline concepts::real_concept min_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept)) | |
371 | { | |
372 | return min_value<concepts::real_concept_base_type>(); | |
373 | } | |
374 | ||
375 | template <> | |
376 | inline concepts::real_concept log_max_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept)) | |
377 | { | |
378 | return log_max_value<concepts::real_concept_base_type>(); | |
379 | } | |
380 | ||
381 | template <> | |
382 | inline concepts::real_concept log_min_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept)) | |
383 | { | |
384 | return log_min_value<concepts::real_concept_base_type>(); | |
385 | } | |
386 | ||
387 | template <> | |
388 | inline concepts::real_concept epsilon<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept)) | |
389 | { | |
390 | #ifdef __SUNPRO_CC | |
391 | return std::numeric_limits<concepts::real_concept_base_type>::epsilon(); | |
392 | #else | |
393 | return tools::epsilon<concepts::real_concept_base_type>(); | |
394 | #endif | |
395 | } | |
396 | ||
397 | template <> | |
398 | inline BOOST_MATH_CONSTEXPR int digits<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept)) BOOST_NOEXCEPT | |
399 | { | |
400 | // Assume number of significand bits is same as real_concept_base_type, | |
401 | // unless std::numeric_limits<T>::is_specialized to provide digits. | |
402 | return tools::digits<concepts::real_concept_base_type>(); | |
403 | // Note that if numeric_limits real concept is NOT specialized to provide digits10 | |
404 | // (or max_digits10) then the default precision of 6 decimal digits will be used | |
405 | // by Boost test (giving misleading error messages like | |
406 | // "difference between {9.79796} and {9.79796} exceeds 5.42101e-19%" | |
407 | // and by Boost lexical cast and serialization causing loss of accuracy. | |
408 | } | |
409 | ||
410 | } // namespace tools | |
411 | /* | |
412 | namespace policies { | |
413 | namespace detail { | |
414 | ||
415 | template <class T> | |
416 | inline concepts::real_concept raise_rounding_error( | |
417 | const char*, | |
418 | const char*, | |
419 | const T& val, | |
420 | const concepts::real_concept&, | |
421 | const ::boost::math::policies::rounding_error< ::boost::math::policies::errno_on_error>&) BOOST_MATH_NOEXCEPT(T) | |
422 | { | |
423 | errno = ERANGE; | |
424 | // This may or may not do the right thing, but the user asked for the error | |
425 | // to be silent so here we go anyway: | |
426 | return val > 0 ? boost::math::tools::max_value<concepts::real_concept>() : -boost::math::tools::max_value<concepts::real_concept>(); | |
427 | } | |
428 | ||
429 | } | |
430 | }*/ | |
431 | ||
432 | #if defined(__SGI_STL_PORT) || defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) | |
433 | // | |
434 | // We shouldn't really need these type casts any more, but there are some | |
435 | // STLport iostream bugs we work around by using them.... | |
436 | // | |
437 | namespace tools | |
438 | { | |
439 | // real_cast converts from T to integer and narrower floating-point types. | |
440 | ||
441 | // Convert from T to integer types. | |
442 | ||
443 | template <> | |
444 | inline unsigned int real_cast<unsigned int, concepts::real_concept>(concepts::real_concept r) | |
445 | { | |
446 | return static_cast<unsigned int>(r.value()); | |
447 | } | |
448 | ||
449 | template <> | |
450 | inline int real_cast<int, concepts::real_concept>(concepts::real_concept r) | |
451 | { | |
452 | return static_cast<int>(r.value()); | |
453 | } | |
454 | ||
455 | template <> | |
456 | inline long real_cast<long, concepts::real_concept>(concepts::real_concept r) | |
457 | { | |
458 | return static_cast<long>(r.value()); | |
459 | } | |
460 | ||
461 | // Converts from T to narrower floating-point types, float, double & long double. | |
462 | ||
463 | template <> | |
464 | inline float real_cast<float, concepts::real_concept>(concepts::real_concept r) | |
465 | { | |
466 | return static_cast<float>(r.value()); | |
467 | } | |
468 | template <> | |
469 | inline double real_cast<double, concepts::real_concept>(concepts::real_concept r) | |
470 | { | |
471 | return static_cast<double>(r.value()); | |
472 | } | |
473 | template <> | |
474 | inline long double real_cast<long double, concepts::real_concept>(concepts::real_concept r) | |
475 | { | |
476 | return r.value(); | |
477 | } | |
478 | ||
479 | } // STLPort | |
480 | ||
481 | #endif | |
482 | ||
483 | #if BOOST_WORKAROUND(BOOST_MSVC, <= 1310) | |
484 | // | |
485 | // For some strange reason ADL sometimes fails to find the | |
486 | // correct overloads, unless we bring these declarations into scope: | |
487 | // | |
488 | using concepts::itrunc; | |
489 | using concepts::iround; | |
490 | ||
491 | #endif | |
492 | ||
493 | } // namespace math | |
494 | } // namespace boost | |
495 | ||
496 | #endif // BOOST_MATH_REAL_CONCEPT_HPP | |
497 | ||
498 |