comparison DEPENDENCIES/generic/include/boost/math/concepts/real_concept.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
comparison
equal deleted inserted replaced
15:663ca0da4350 16:2665513ce2d3
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(__float128 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 real_concept_base_type ip;
219 real_concept_base_type result = std::modf(a.value(), &ip);
220 *ipart = ip;
221 return result;
222 }
223 inline real_concept frexp(real_concept a, int* expon)
224 { return std::frexp(a.value(), expon); }
225 inline real_concept ldexp(real_concept a, int expon)
226 { return std::ldexp(a.value(), expon); }
227 inline real_concept log(real_concept a)
228 { return std::log(a.value()); }
229 inline real_concept log10(real_concept a)
230 { return std::log10(a.value()); }
231 inline real_concept tan(real_concept a)
232 { return std::tan(a.value()); }
233 inline real_concept pow(real_concept a, real_concept b)
234 { return std::pow(a.value(), b.value()); }
235 #if !defined(__SUNPRO_CC)
236 inline real_concept pow(real_concept a, int b)
237 { return std::pow(a.value(), b); }
238 #else
239 inline real_concept pow(real_concept a, int b)
240 { return std::pow(a.value(), static_cast<real_concept_base_type>(b)); }
241 #endif
242 inline real_concept sin(real_concept a)
243 { return std::sin(a.value()); }
244 inline real_concept sinh(real_concept a)
245 { return std::sinh(a.value()); }
246 inline real_concept sqrt(real_concept a)
247 { return std::sqrt(a.value()); }
248 inline real_concept tanh(real_concept a)
249 { return std::tanh(a.value()); }
250
251 //
252 // Conversion and truncation routines:
253 //
254 template <class Policy>
255 inline int iround(const concepts::real_concept& v, const Policy& pol)
256 { return boost::math::iround(v.value(), pol); }
257 inline int iround(const concepts::real_concept& v)
258 { return boost::math::iround(v.value(), policies::policy<>()); }
259 template <class Policy>
260 inline long lround(const concepts::real_concept& v, const Policy& pol)
261 { return boost::math::lround(v.value(), pol); }
262 inline long lround(const concepts::real_concept& v)
263 { return boost::math::lround(v.value(), policies::policy<>()); }
264
265 #ifdef BOOST_HAS_LONG_LONG
266 template <class Policy>
267 inline boost::long_long_type llround(const concepts::real_concept& v, const Policy& pol)
268 { return boost::math::llround(v.value(), pol); }
269 inline boost::long_long_type llround(const concepts::real_concept& v)
270 { return boost::math::llround(v.value(), policies::policy<>()); }
271 #endif
272
273 template <class Policy>
274 inline int itrunc(const concepts::real_concept& v, const Policy& pol)
275 { return boost::math::itrunc(v.value(), pol); }
276 inline int itrunc(const concepts::real_concept& v)
277 { return boost::math::itrunc(v.value(), policies::policy<>()); }
278 template <class Policy>
279 inline long ltrunc(const concepts::real_concept& v, const Policy& pol)
280 { return boost::math::ltrunc(v.value(), pol); }
281 inline long ltrunc(const concepts::real_concept& v)
282 { return boost::math::ltrunc(v.value(), policies::policy<>()); }
283
284 #ifdef BOOST_HAS_LONG_LONG
285 template <class Policy>
286 inline boost::long_long_type lltrunc(const concepts::real_concept& v, const Policy& pol)
287 { return boost::math::lltrunc(v.value(), pol); }
288 inline boost::long_long_type lltrunc(const concepts::real_concept& v)
289 { return boost::math::lltrunc(v.value(), policies::policy<>()); }
290 #endif
291
292 // Streaming:
293 template <class charT, class traits>
294 inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const real_concept& a)
295 {
296 return os << a.value();
297 }
298 template <class charT, class traits>
299 inline std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& is, real_concept& a)
300 {
301 #if defined(BOOST_MSVC) && defined(__SGI_STL_PORT)
302 //
303 // STLPort 5.1.4 has a problem reading long doubles from strings,
304 // see http://sourceforge.net/tracker/index.php?func=detail&aid=1811043&group_id=146814&atid=766244
305 //
306 double v;
307 is >> v;
308 a = v;
309 return is;
310 #elif defined(__SGI_STL_PORT) || defined(_RWSTD_VER) || defined(__LIBCOMO__)
311 std::string s;
312 real_concept_base_type d;
313 is >> s;
314 std::sscanf(s.c_str(), "%Lf", &d);
315 a = d;
316 return is;
317 #else
318 real_concept_base_type v;
319 is >> v;
320 a = v;
321 return is;
322 #endif
323 }
324
325 } // namespace concepts
326
327 namespace tools
328 {
329
330 template <>
331 inline concepts::real_concept make_big_value<concepts::real_concept>(long double val, const char* , mpl::false_ const&, mpl::false_ const&)
332 {
333 return val; // Can't use lexical_cast here, sometimes it fails....
334 }
335
336 template <>
337 inline concepts::real_concept max_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
338 {
339 return max_value<concepts::real_concept_base_type>();
340 }
341
342 template <>
343 inline concepts::real_concept min_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
344 {
345 return min_value<concepts::real_concept_base_type>();
346 }
347
348 template <>
349 inline concepts::real_concept log_max_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
350 {
351 return log_max_value<concepts::real_concept_base_type>();
352 }
353
354 template <>
355 inline concepts::real_concept log_min_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
356 {
357 return log_min_value<concepts::real_concept_base_type>();
358 }
359
360 template <>
361 inline concepts::real_concept epsilon<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
362 {
363 #ifdef __SUNPRO_CC
364 return std::numeric_limits<concepts::real_concept_base_type>::epsilon();
365 #else
366 return tools::epsilon<concepts::real_concept_base_type>();
367 #endif
368 }
369
370 template <>
371 inline int digits<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
372 {
373 // Assume number of significand bits is same as real_concept_base_type,
374 // unless std::numeric_limits<T>::is_specialized to provide digits.
375 return tools::digits<concepts::real_concept_base_type>();
376 // Note that if numeric_limits real concept is NOT specialized to provide digits10
377 // (or max_digits10) then the default precision of 6 decimal digits will be used
378 // by Boost test (giving misleading error messages like
379 // "difference between {9.79796} and {9.79796} exceeds 5.42101e-19%"
380 // and by Boost lexical cast and serialization causing loss of accuracy.
381 }
382
383 } // namespace tools
384
385 #if defined(__SGI_STL_PORT) || defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS)
386 //
387 // We shouldn't really need these type casts any more, but there are some
388 // STLport iostream bugs we work around by using them....
389 //
390 namespace tools
391 {
392 // real_cast converts from T to integer and narrower floating-point types.
393
394 // Convert from T to integer types.
395
396 template <>
397 inline unsigned int real_cast<unsigned int, concepts::real_concept>(concepts::real_concept r)
398 {
399 return static_cast<unsigned int>(r.value());
400 }
401
402 template <>
403 inline int real_cast<int, concepts::real_concept>(concepts::real_concept r)
404 {
405 return static_cast<int>(r.value());
406 }
407
408 template <>
409 inline long real_cast<long, concepts::real_concept>(concepts::real_concept r)
410 {
411 return static_cast<long>(r.value());
412 }
413
414 // Converts from T to narrower floating-point types, float, double & long double.
415
416 template <>
417 inline float real_cast<float, concepts::real_concept>(concepts::real_concept r)
418 {
419 return static_cast<float>(r.value());
420 }
421 template <>
422 inline double real_cast<double, concepts::real_concept>(concepts::real_concept r)
423 {
424 return static_cast<double>(r.value());
425 }
426 template <>
427 inline long double real_cast<long double, concepts::real_concept>(concepts::real_concept r)
428 {
429 return r.value();
430 }
431
432 } // STLPort
433
434 #endif
435
436 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
437 //
438 // For some strange reason ADL sometimes fails to find the
439 // correct overloads, unless we bring these declarations into scope:
440 //
441 using concepts::itrunc;
442 using concepts::iround;
443
444 #endif
445
446 } // namespace math
447 } // namespace boost
448
449 #endif // BOOST_MATH_REAL_CONCEPT_HPP
450
451