File indexing completed on 2025-01-30 09:49:07
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_NUMERIC_INTERVAL_CHECKING_HPP
0011 #define BOOST_NUMERIC_INTERVAL_CHECKING_HPP
0012
0013 #include <stdexcept>
0014 #include <string>
0015 #include <cassert>
0016 #include <boost/limits.hpp>
0017
0018 namespace boost {
0019 namespace numeric {
0020 namespace interval_lib {
0021
0022 struct exception_create_empty
0023 {
0024 void operator()()
0025 {
0026 throw std::runtime_error("boost::interval: empty interval created");
0027 }
0028 };
0029
0030 struct exception_invalid_number
0031 {
0032 void operator()()
0033 {
0034 throw std::invalid_argument("boost::interval: invalid number");
0035 }
0036 };
0037
0038 template<class T>
0039 struct checking_base
0040 {
0041 static T pos_inf()
0042 {
0043 assert(std::numeric_limits<T>::has_infinity);
0044 return std::numeric_limits<T>::infinity();
0045 }
0046 static T neg_inf()
0047 {
0048 assert(std::numeric_limits<T>::has_infinity);
0049 return -std::numeric_limits<T>::infinity();
0050 }
0051 static T nan()
0052 {
0053 assert(std::numeric_limits<T>::has_quiet_NaN);
0054 return std::numeric_limits<T>::quiet_NaN();
0055 }
0056 static bool is_nan(const T& x)
0057 {
0058 return std::numeric_limits<T>::has_quiet_NaN && (x != x);
0059 }
0060 static T empty_lower()
0061 {
0062 return (std::numeric_limits<T>::has_quiet_NaN ?
0063 std::numeric_limits<T>::quiet_NaN() : static_cast<T>(1));
0064 }
0065 static T empty_upper()
0066 {
0067 return (std::numeric_limits<T>::has_quiet_NaN ?
0068 std::numeric_limits<T>::quiet_NaN() : static_cast<T>(0));
0069 }
0070 static bool is_empty(const T& l, const T& u)
0071 {
0072 return !(l <= u);
0073 }
0074 };
0075
0076 template<class T, class Checking = checking_base<T>,
0077 class Exception = exception_create_empty>
0078 struct checking_no_empty: Checking
0079 {
0080 static T nan()
0081 {
0082 assert(false);
0083 return Checking::nan();
0084 }
0085 static T empty_lower()
0086 {
0087 Exception()();
0088 return Checking::empty_lower();
0089 }
0090 static T empty_upper()
0091 {
0092 Exception()();
0093 return Checking::empty_upper();
0094 }
0095 static bool is_empty(const T&, const T&)
0096 {
0097 return false;
0098 }
0099 };
0100
0101 template<class T, class Checking = checking_base<T> >
0102 struct checking_no_nan: Checking
0103 {
0104 static bool is_nan(const T&)
0105 {
0106 return false;
0107 }
0108 };
0109
0110 template<class T, class Checking = checking_base<T>,
0111 class Exception = exception_invalid_number>
0112 struct checking_catch_nan: Checking
0113 {
0114 static bool is_nan(const T& x)
0115 {
0116 if (Checking::is_nan(x)) Exception()();
0117 return false;
0118 }
0119 };
0120
0121 template<class T>
0122 struct checking_strict:
0123 checking_no_nan<T, checking_no_empty<T> >
0124 {};
0125
0126 }
0127 }
0128 }
0129
0130 #endif