00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef PPL_Checked_Number_defs_hh
00024 #define PPL_Checked_Number_defs_hh 1
00025
00026 #include "Checked_Number.types.hh"
00027 #include "checked.defs.hh"
00028
00029 namespace Parma_Polyhedra_Library {
00030
00031 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
00032
00033 #endif // PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
00034 struct Checked_Number_Transparent_Policy {
00036 static const int check_overflow = 0;
00037
00039 static const int check_inf_add_inf = 0;
00040
00042 static const int check_inf_sub_inf = 0;
00043
00045 static const int check_inf_mul_zero = 0;
00046
00048 static const int check_div_zero = 0;
00049
00051 static const int check_inf_div_inf = 0;
00052
00054 static const int check_inf_mod = 0;
00055
00057 static const int check_sqrt_neg = 0;
00058
00060 static const int handle_nan = 0;
00061
00063 static const int handle_infinity = 0;
00064
00066 static const int convertible = 1;
00067
00069 static const int fpu_check_inexact = 0;
00070
00072 static const int check_nan_args = 0;
00073 static const Rounding_Dir ROUND_DEFAULT_CONSTRUCTOR = ROUND_NATIVE;
00074 static const Rounding_Dir ROUND_DEFAULT_OPERATOR = ROUND_NATIVE;
00075 static const Rounding_Dir ROUND_DEFAULT_FUNCTION = ROUND_NATIVE;
00076 static const Rounding_Dir ROUND_DEFAULT_INPUT = ROUND_NATIVE;
00077 static const Rounding_Dir ROUND_DEFAULT_OUTPUT = ROUND_NATIVE;
00078 static void handle_result(Result r);
00079 };
00080
00081 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
00082
00083 #endif // PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
00084 struct Checked_Number_Default_Policy {
00085 static const int check_overflow = 1;
00086 static const int check_inf_add_inf = 0;
00087 static const int check_inf_sub_inf = 0;
00088 static const int check_inf_mul_zero = 0;
00089 static const int check_div_zero = 0;
00090 static const int check_inf_div_inf = 0;
00091 static const int check_inf_mod = 0;
00092 static const int check_sqrt_neg = 0;
00093 static const int handle_nan = 0;
00094 static const int handle_infinity = 0;
00095 static const int convertible = 1;
00096 static const int fpu_check_inexact = 0;
00097 static const int check_nan_args = 1;
00098 static const Rounding_Dir ROUND_DEFAULT_CONSTRUCTOR = ROUND_NATIVE;
00099 static const Rounding_Dir ROUND_DEFAULT_OPERATOR = ROUND_NATIVE;
00100 static const Rounding_Dir ROUND_DEFAULT_FUNCTION = ROUND_NATIVE;
00101 static const Rounding_Dir ROUND_DEFAULT_INPUT = ROUND_NATIVE;
00102 static const Rounding_Dir ROUND_DEFAULT_OUTPUT = ROUND_NATIVE;
00103 static void handle_result(Result r);
00104 };
00105
00106 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
00107
00108 #endif // PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
00109 struct Extended_Number_Policy {
00110 static const int check_overflow = 1;
00111 static const int check_inf_add_inf = 0;
00112 static const int check_inf_sub_inf = 0;
00113 static const int check_inf_mul_zero = 0;
00114 static const int check_div_zero = 0;
00115 static const int check_inf_div_inf = 0;
00116 static const int check_inf_mod = 0;
00117 static const int check_sqrt_neg = 0;
00118 static const int handle_nan = 1;
00119 static const int handle_infinity = 1;
00120
00121
00122
00123 #ifdef DEBUG_ROUND_NOT_NEEDED
00124 static const int fpu_check_inexact = 1;
00125 #else
00126 static const int fpu_check_inexact = 0;
00127 #endif
00128 static const int check_nan_args = 1;
00129 static const Rounding_Dir ROUND_DEFAULT_CONSTRUCTOR_INF = ROUND_NOT_NEEDED;
00130 static const Rounding_Dir ROUND_DEFAULT_ASSIGN_INF = ROUND_NOT_NEEDED;
00131
00132
00133
00134
00135
00136
00137
00138 static void handle_result(Result r);
00139 };
00140
00141 typedef Checked::Check_Overflow_Policy Default_To_Policy;
00142 typedef Checked_Number_Transparent_Policy Default_From_Policy;
00143
00144 template <typename T>
00145 struct Native_Checked_From_Wrapper {
00146 typedef Default_From_Policy Policy;
00147 static const T& raw_value(const T& v) {
00148 return v;
00149 }
00150 };
00151
00152 template <typename T, typename P>
00153 struct Native_Checked_From_Wrapper<Checked_Number<T, P> > {
00154 typedef P Policy;
00155 static const T& raw_value(const Checked_Number<T, P>& v) {
00156 return v.raw_value();
00157 }
00158 };
00159
00160 template <typename T>
00161 struct Native_Checked_To_Wrapper {
00162 typedef Default_To_Policy Policy;
00163 static T& raw_value(T& v) {
00164 return v;
00165 }
00166 };
00167
00168 template <typename T, typename P>
00169 struct Native_Checked_To_Wrapper<Checked_Number<T, P> > {
00170 typedef P Policy;
00171 static T& raw_value(Checked_Number<T, P>& v) {
00172 return v.raw_value();
00173 }
00174 };
00175
00177
00184 template <typename T, typename Policy>
00185 class Checked_Number {
00186 public:
00187
00189
00190
00192 Checked_Number();
00193
00195 Checked_Number(const Checked_Number& y);
00196
00198 template <typename From, typename From_Policy>
00199 Checked_Number(const Checked_Number<From, From_Policy>& y, Rounding_Dir dir);
00200
00202 Checked_Number(const signed char y, Rounding_Dir dir);
00203
00205 Checked_Number(const signed short y, Rounding_Dir dir);
00206
00208 Checked_Number(const signed int y, Rounding_Dir dir);
00209
00211 Checked_Number(const signed long y, Rounding_Dir dir);
00212
00214 Checked_Number(const signed long long y, Rounding_Dir dir);
00215
00217 Checked_Number(const unsigned char y, Rounding_Dir dir);
00218
00220 Checked_Number(const unsigned short y, Rounding_Dir dir);
00221
00223 Checked_Number(const unsigned int y, Rounding_Dir dir);
00224
00226 Checked_Number(const unsigned long y, Rounding_Dir dir);
00227
00229 Checked_Number(const unsigned long long y, Rounding_Dir dir);
00230
00231 #if PPL_SUPPORTED_FLOAT
00233 Checked_Number(const float y, Rounding_Dir dir);
00234 #endif
00235
00236 #if PPL_SUPPORTED_DOUBLE
00238 Checked_Number(const double y, Rounding_Dir dir);
00239 #endif
00240
00241 #if PPL_SUPPORTED_LONG_DOUBLE
00243 Checked_Number(const long double y, Rounding_Dir dir);
00244 #endif
00245
00247 Checked_Number(const mpq_class& y, Rounding_Dir dir);
00248
00250 Checked_Number(const mpz_class& y, Rounding_Dir dir);
00251
00253 Checked_Number(const char* y, Rounding_Dir dir);
00254
00256 Checked_Number(const Minus_Infinity& y, Rounding_Dir dir);
00257
00259 Checked_Number(const Plus_Infinity& y, Rounding_Dir dir);
00260
00262 Checked_Number(const Not_A_Number& y, Rounding_Dir dir);
00263
00265 template <typename From, typename From_Policy>
00266 explicit Checked_Number(const Checked_Number<From, From_Policy>& y);
00267
00269 Checked_Number(const signed char y);
00270
00272 Checked_Number(const signed short y);
00273
00275 Checked_Number(const signed int y);
00276
00278 Checked_Number(const signed long y);
00279
00281 Checked_Number(const signed long long y);
00282
00284 Checked_Number(const unsigned char y);
00285
00287 Checked_Number(const unsigned short y);
00288
00290 Checked_Number(const unsigned int y);
00291
00293 Checked_Number(const unsigned long y);
00294
00296 Checked_Number(const unsigned long long y);
00297
00299 Checked_Number(const float y);
00300
00302 Checked_Number(const double y);
00303
00305 Checked_Number(const long double y);
00306
00308 Checked_Number(const mpq_class& y);
00309
00311 Checked_Number(const mpz_class& y);
00312
00314 Checked_Number(const char* y);
00315
00317 Checked_Number(const Minus_Infinity& y);
00318
00320 Checked_Number(const Plus_Infinity& y);
00321
00323 Checked_Number(const Not_A_Number& y);
00324
00326
00328
00329
00331 operator T() const;
00332
00334 T& raw_value();
00335
00337 const T& raw_value() const;
00338
00340
00342 bool OK() const;
00343
00345
00354 Result classify(bool nan = true, bool inf = true, bool sign = true) const;
00355
00357
00358
00360 Checked_Number& operator=(const Checked_Number& y);
00361
00363 template <typename From, typename From_Policy>
00364 Checked_Number& operator=(const Checked_Number<From, From_Policy>& y);
00365
00367 template <typename From>
00368 Checked_Number& operator=(const From& y);
00369
00371 Checked_Number& operator=(const Not_A_Number& y);
00372
00374 Checked_Number& operator=(const Minus_Infinity& y);
00375
00377 Checked_Number& operator=(const Plus_Infinity& y);
00378
00380 template <typename From_Policy>
00381 Checked_Number& operator+=(const Checked_Number<T, From_Policy>& y);
00382
00384 Checked_Number& operator+=(const T& y);
00385
00387 template <typename From, typename From_Policy>
00388 Checked_Number& operator+=(const Checked_Number<From, From_Policy>& y);
00389
00390 template <typename From>
00391 Checked_Number& operator+=(const From& y);
00392
00394 template <typename From_Policy>
00395 Checked_Number& operator-=(const Checked_Number<T, From_Policy>& y);
00396
00398 Checked_Number& operator-=(const T& y);
00399
00401 template <typename From, typename From_Policy>
00402 Checked_Number& operator-=(const Checked_Number<From, From_Policy>& y);
00403
00405 template <typename From>
00406 Checked_Number& operator-=(const From& y);
00407
00409 template <typename From_Policy>
00410 Checked_Number& operator*=(const Checked_Number<T, From_Policy>& y);
00411
00413 Checked_Number& operator*=(const T& y);
00414 template <typename From, typename From_Policy>
00415
00417 Checked_Number& operator*=(const Checked_Number<From, From_Policy>& y);
00418
00420 template <typename From>
00421 Checked_Number& operator*=(const From& y);
00422
00424 template <typename From_Policy>
00425 Checked_Number& operator/=(const Checked_Number<T, From_Policy>& y);
00426
00428 Checked_Number& operator/=(const T& y);
00429
00431 template <typename From, typename From_Policy>
00432 Checked_Number& operator/=(const Checked_Number<From, From_Policy>& y);
00433
00435 template <typename From>
00436 Checked_Number& operator/=(const From& y);
00437
00439 template <typename From_Policy>
00440 Checked_Number& operator%=(const Checked_Number<T, From_Policy>& y);
00441
00443 Checked_Number& operator%=(const T& y);
00444
00446 template <typename From, typename From_Policy>
00447 Checked_Number& operator%=(const Checked_Number<From, From_Policy>& y);
00448
00450 template <typename From>
00451 Checked_Number& operator%=(const From& y);
00452
00454
00455
00457
00458
00460 Checked_Number& operator++();
00461
00463 Checked_Number operator++(int);
00464
00466 Checked_Number& operator--();
00467
00469 Checked_Number operator--(int);
00470
00472
00473 private:
00475 T v;
00476 };
00477
00478 template <typename T, typename Policy>
00479 bool is_not_a_number(const Checked_Number<T, Policy>& x);
00480 template <typename T, typename Policy>
00481 bool is_minus_infinity(const Checked_Number<T, Policy>& x);
00482 template <typename T, typename Policy>
00483 bool is_plus_infinity(const Checked_Number<T, Policy>& x);
00484
00485 template <typename To>
00486 Result assign_r(To& to, const Minus_Infinity& x, Rounding_Dir dir);
00487 template <typename To>
00488 Result assign_r(To& to, const Plus_Infinity& x, Rounding_Dir dir);
00489 template <typename To>
00490 Result assign_r(To& to, const Not_A_Number& x, Rounding_Dir dir);
00491 template <typename To>
00492 Result assign_r(To& to, const char* x, Rounding_Dir dir);
00493 template <typename To, typename To_Policy>
00494 Result assign_r(To& to, char* x, Rounding_Dir dir);
00495
00496 #define FUNC1(name) \
00497 template <typename To, typename From> \
00498 Result name(To& to, const From& x, Rounding_Dir dir);
00499
00500 FUNC1(assign_r)
00501 FUNC1(neg_assign_r)
00502 FUNC1(abs_assign_r)
00503 FUNC1(sqrt_assign_r)
00504
00505 #undef FUNC1
00506
00507 #define FUNC1(name) \
00508 template <typename To, typename From> \
00509 Result name(To& to, const From& x, int exp, Rounding_Dir dir);
00510
00511 FUNC1(mul2exp_assign_r)
00512 FUNC1(div2exp_assign_r)
00513
00514 #undef FUNC1
00515
00516 #define FUNC2(name) \
00517 template <typename To, typename From1, typename From2> \
00518 Result name(To& to, const From1& x, const From2& y, Rounding_Dir dir);
00519
00520 FUNC2(add_assign_r)
00521 FUNC2(sub_assign_r)
00522 FUNC2(mul_assign_r)
00523 FUNC2(div_assign_r)
00524 FUNC2(rem_assign_r)
00525 FUNC2(gcd_assign_r)
00526 FUNC2(lcm_assign_r)
00527 FUNC2(add_mul_assign_r)
00528 FUNC2(sub_mul_assign_r)
00529
00530 #undef FUNC2
00531
00532 #define FUNC4(name) \
00533 template <typename To1, typename From1, typename From2, \
00534 typename To2, typename To3> \
00535 Result name(To1& to, const From1& x, const From2& y, \
00536 To2& s, To3& t, Rounding_Dir dir);
00537
00538 FUNC4(gcdext_assign_r)
00539
00540 #undef FUNC4
00541
00543
00544 template <typename T, typename Policy>
00545 void swap(Checked_Number<T, Policy>& x, Checked_Number<T, Policy>& y);
00546
00548
00549
00551
00552 template <typename T, typename Policy>
00553 const T&
00554 raw_value(const Checked_Number<T, Policy>& x);
00555
00557
00558 template <typename T, typename Policy>
00559 T&
00560 raw_value(Checked_Number<T, Policy>& x);
00561
00563
00565
00566
00568
00569 template <typename T, typename Policy>
00570 size_t
00571 total_memory_in_bytes(const Checked_Number<T, Policy>& x);
00572
00574
00575 template <typename T, typename Policy>
00576 size_t
00577 external_memory_in_bytes(const Checked_Number<T, Policy>& x);
00578
00580
00582
00583
00585
00586 template <typename T, typename Policy>
00587 Checked_Number<T, Policy>
00588 operator+(const Checked_Number<T, Policy>& x);
00589
00591
00592 template <typename T, typename Policy>
00593 Checked_Number<T, Policy>
00594 operator-(const Checked_Number<T, Policy>& x);
00595
00597
00598 template <typename T, typename Policy>
00599 void
00600 neg_assign(Checked_Number<T, Policy>& x);
00601
00603
00604 template <typename T, typename Policy>
00605 void
00606 add_mul_assign(Checked_Number<T, Policy>& x,
00607 const Checked_Number<T, Policy>& y,
00608 const Checked_Number<T, Policy>& z);
00609
00611
00612 template <typename T, typename Policy>
00613 void
00614 sub_mul_assign(Checked_Number<T, Policy>& x,
00615 const Checked_Number<T, Policy>& y,
00616 const Checked_Number<T, Policy>& z);
00617
00619
00620 template <typename T, typename Policy>
00621 void
00622 gcd_assign(Checked_Number<T, Policy>& x,
00623 const Checked_Number<T, Policy>& y,
00624 const Checked_Number<T, Policy>& z);
00625
00631 template <typename T, typename Policy>
00632 void
00633 gcdext_assign(Checked_Number<T, Policy>& x,
00634 const Checked_Number<T, Policy>& y,
00635 const Checked_Number<T, Policy>& z,
00636 Checked_Number<T, Policy>& s,
00637 Checked_Number<T, Policy>& t);
00638
00640
00641 template <typename T, typename Policy>
00642 void
00643 lcm_assign(Checked_Number<T, Policy>& x,
00644 const Checked_Number<T, Policy>& y,
00645 const Checked_Number<T, Policy>& z);
00646
00654 template <typename T, typename Policy>
00655 void
00656 exact_div_assign(Checked_Number<T, Policy>& x,
00657 const Checked_Number<T, Policy>& y,
00658 const Checked_Number<T, Policy>& z);
00659
00661
00662 template <typename T, typename Policy>
00663 void sqrt_assign(Checked_Number<T, Policy>& x,
00664 const Checked_Number<T, Policy>& y);
00665
00667
00668
00670
00671
00673
00674 template <typename T1, typename Policy1,
00675 typename T2, typename Policy2>
00676 bool
00677 operator==(const Checked_Number<T1, Policy1>& x,
00678 const Checked_Number<T2, Policy2>& y);
00679
00681
00682 template <typename T1, typename Policy1,
00683 typename T2, typename Policy2>
00684 bool
00685 operator!=(const Checked_Number<T1, Policy1>& x,
00686 const Checked_Number<T2, Policy2>& y);
00687
00689
00690 template <typename T1, typename Policy1,
00691 typename T2, typename Policy2>
00692 bool
00693 operator>=(const Checked_Number<T1, Policy1>& x,
00694 const Checked_Number<T2, Policy2>& y);
00695
00697
00698 template <typename T1, typename Policy1,
00699 typename T2, typename Policy2>
00700 bool
00701 operator>(const Checked_Number<T1, Policy1>& x,
00702 const Checked_Number<T2, Policy2>& y);
00703
00705
00706 template <typename T1, typename Policy1,
00707 typename T2, typename Policy2>
00708 bool
00709 operator<=(const Checked_Number<T1, Policy1>& x,
00710 const Checked_Number<T2, Policy2>& y);
00711
00713
00714 template <typename T1, typename Policy1,
00715 typename T2, typename Policy2>
00716 bool
00717 operator<(const Checked_Number<T1, Policy1>& x,
00718 const Checked_Number<T2, Policy2>& y);
00719
00726 template <typename T, typename Policy>
00727 int
00728 sgn(const Checked_Number<T, Policy>& x);
00729
00736 template <typename T1, typename Policy1,
00737 typename T2, typename Policy2>
00738 int
00739 cmp(const Checked_Number<T1, Policy1>& x,
00740 const Checked_Number<T2, Policy2>& y);
00741
00743
00745
00746
00748 template <typename T, typename Policy>
00749 Result
00750 output(std::ostream& os,
00751 const Checked_Number<T, Policy>& x,
00752 const Numeric_Format& fmt,
00753 Rounding_Dir dir);
00754
00756
00757 template <typename T, typename Policy>
00758 std::ostream&
00759 operator<<(std::ostream& os, const Checked_Number<T, Policy>& x);
00760
00762
00882 template <typename T, typename Policy>
00883 Result
00884 input(std::istream& is, Checked_Number<T, Policy>& x, Rounding_Dir dir);
00885
00887
00888 template <typename T, typename Policy>
00889 std::istream&
00890 operator>>(std::istream& is, Checked_Number<T, Policy>& x);
00891
00893
00894 void throw_result_exception(Result r);
00895
00896 template <typename T>
00897 T
00898 plus_infinity();
00899
00900 template <typename T>
00901 T
00902 minus_infinity();
00903
00904 template <typename T>
00905 T
00906 not_a_number();
00907
00908 }
00909
00910 #include "Checked_Number.inlines.hh"
00911 #include "checked_numeric_limits.hh"
00912
00913 #endif // !defined(PPL_Checked_Number_defs_hh)