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