rational_example.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // rational number example program ----------------------------------------//
  2. // (C) Copyright Paul Moore 1999. Permission to copy, use, modify, sell
  3. // and distribute this software is granted provided this copyright notice
  4. // appears in all copies. This software is provided "as is" without express or
  5. // implied warranty, and with no claim as to its suitability for any purpose.
  6. // boostinspect:nolicense (don't complain about the lack of a Boost license)
  7. // (Paul Moore hasn't been in contact for years, so there's no way to change the
  8. // license.)
  9. // Revision History
  10. // 14 Dec 99 Initial version
  11. #include <iostream>
  12. #include <cassert>
  13. #include <cstdlib>
  14. #include <boost/config.hpp>
  15. #ifndef BOOST_NO_LIMITS
  16. #include <limits>
  17. #else
  18. #include <limits.h>
  19. #endif
  20. #include <exception>
  21. #include <boost/rational.hpp>
  22. using std::cout;
  23. using std::endl;
  24. using boost::rational;
  25. #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
  26. // This is a nasty hack, required because MSVC does not implement "Koenig
  27. // Lookup". Basically, if I call abs(r), the C++ standard says that the
  28. // compiler should look for a definition of abs in the namespace which
  29. // contains r's class (in this case boost) - among other places.
  30. // Koenig Lookup is a relatively recent feature, and other compilers may not
  31. // implement it yet. If so, try including this line.
  32. using boost::abs;
  33. #endif
  34. int main ()
  35. {
  36. rational<int> half(1,2);
  37. rational<int> one(1);
  38. rational<int> two(2);
  39. // Some basic checks
  40. assert(half.numerator() == 1);
  41. assert(half.denominator() == 2);
  42. assert(boost::rational_cast<double>(half) == 0.5);
  43. // Arithmetic
  44. assert(half + half == one);
  45. assert(one - half == half);
  46. assert(two * half == one);
  47. assert(one / half == two);
  48. // With conversions to integer
  49. assert(half+half == 1);
  50. assert(2 * half == one);
  51. assert(2 * half == 1);
  52. assert(one / half == 2);
  53. assert(1 / half == 2);
  54. // Sign handling
  55. rational<int> minus_half(-1,2);
  56. assert(-half == minus_half);
  57. assert(abs(minus_half) == half);
  58. // Do we avoid overflow?
  59. #ifndef BOOST_NO_LIMITS
  60. int maxint = (std::numeric_limits<int>::max)();
  61. #else
  62. int maxint = INT_MAX;
  63. #endif
  64. rational<int> big(maxint, 2);
  65. assert(2 * big == maxint);
  66. // Print some of the above results
  67. cout << half << "+" << half << "=" << one << endl;
  68. cout << one << "-" << half << "=" << half << endl;
  69. cout << two << "*" << half << "=" << one << endl;
  70. cout << one << "/" << half << "=" << two << endl;
  71. cout << "abs(" << minus_half << ")=" << half << endl;
  72. cout << "2 * " << big << "=" << maxint
  73. << " (rational: " << rational<int>(maxint) << ")" << endl;
  74. // Some extras
  75. rational<int> pi(22,7);
  76. cout << "pi = " << boost::rational_cast<double>(pi) << " (nearly)" << endl;
  77. // Exception handling
  78. try {
  79. rational<int> r; // Forgot to initialise - set to 0
  80. r = 1/r; // Boom!
  81. }
  82. catch (const boost::bad_rational &e) {
  83. cout << "Bad rational, as expected: " << e.what() << endl;
  84. }
  85. catch (...) {
  86. cout << "Wrong exception raised!" << endl;
  87. }
  88. return 0;
  89. }