123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709 |
- #ifndef BOOST_RATIONAL_HPP
- #define BOOST_RATIONAL_HPP
- #include <boost/config.hpp>
- #ifndef BOOST_NO_IOSTREAM
- #include <iomanip>
- #include <ios>
- #include <istream>
- #include <ostream>
- #include <sstream>
- #endif
- #include <cstddef>
- #include <stdexcept>
- #include <string>
- #include <boost/operators.hpp>
- #include <cstdlib>
- #include <boost/call_traits.hpp>
- #include <boost/detail/workaround.hpp>
- #include <boost/assert.hpp>
- #include <boost/integer/common_factor_rt.hpp>
- #include <limits>
- #include <boost/static_assert.hpp>
- #include <boost/throw_exception.hpp>
- #ifndef BOOST_CONTROL_RATIONAL_HAS_GCD
- #define BOOST_CONTROL_RATIONAL_HAS_GCD 1
- #endif
- namespace boost {
- #if BOOST_CONTROL_RATIONAL_HAS_GCD
- template <typename IntType>
- IntType gcd(IntType n, IntType m)
- {
-
- return integer::gcd( n, m );
- }
- template <typename IntType>
- IntType lcm(IntType n, IntType m)
- {
-
- return integer::lcm( n, m );
- }
- #endif
- class bad_rational : public std::domain_error
- {
- public:
- explicit bad_rational() : std::domain_error("bad rational: zero denominator") {}
- explicit bad_rational( char const *what ) : std::domain_error( what ) {}
- };
- template <typename IntType>
- class rational :
- less_than_comparable < rational<IntType>,
- equality_comparable < rational<IntType>,
- less_than_comparable2 < rational<IntType>, IntType,
- equality_comparable2 < rational<IntType>, IntType,
- addable < rational<IntType>,
- subtractable < rational<IntType>,
- multipliable < rational<IntType>,
- dividable < rational<IntType>,
- addable2 < rational<IntType>, IntType,
- subtractable2 < rational<IntType>, IntType,
- subtractable2_left < rational<IntType>, IntType,
- multipliable2 < rational<IntType>, IntType,
- dividable2 < rational<IntType>, IntType,
- dividable2_left < rational<IntType>, IntType,
- incrementable < rational<IntType>,
- decrementable < rational<IntType>
- > > > > > > > > > > > > > > > >
- {
-
- BOOST_STATIC_ASSERT( ::std::numeric_limits<IntType>::is_specialized );
-
- typedef typename boost::call_traits<IntType>::param_type param_type;
- struct helper { IntType parts[2]; };
- typedef IntType (helper::* bool_type)[2];
- public:
-
- typedef IntType int_type;
- BOOST_CONSTEXPR
- rational() : num(0), den(1) {}
- BOOST_CONSTEXPR
- rational(param_type n) : num(n), den(1) {}
- rational(param_type n, param_type d) : num(n), den(d) { normalize(); }
- #ifndef BOOST_NO_MEMBER_TEMPLATES
- template < typename NewType >
- BOOST_CONSTEXPR explicit
- rational(rational<NewType> const &r)
- : num(r.numerator()), den(is_normalized(int_type(r.numerator()),
- int_type(r.denominator())) ? r.denominator() :
- (BOOST_THROW_EXCEPTION(bad_rational("bad rational: denormalized conversion")), 0)){}
- #endif
-
-
- rational& operator=(param_type i) { num = i; den = 1; return *this; }
-
- rational& assign(param_type n, param_type d);
-
- BOOST_CONSTEXPR
- const IntType& numerator() const { return num; }
- BOOST_CONSTEXPR
- const IntType& denominator() const { return den; }
-
- rational& operator+= (const rational& r);
- rational& operator-= (const rational& r);
- rational& operator*= (const rational& r);
- rational& operator/= (const rational& r);
- rational& operator+= (param_type i) { num += i * den; return *this; }
- rational& operator-= (param_type i) { num -= i * den; return *this; }
- rational& operator*= (param_type i);
- rational& operator/= (param_type i);
-
- const rational& operator++() { num += den; return *this; }
- const rational& operator--() { num -= den; return *this; }
-
- BOOST_CONSTEXPR
- bool operator!() const { return !num; }
-
-
- #if BOOST_WORKAROUND(__MWERKS__,<=0x3003)
-
-
-
- #pragma parse_mfunc_templ off
- #endif
- BOOST_CONSTEXPR
- operator bool_type() const { return operator !() ? 0 : &helper::parts; }
- #if BOOST_WORKAROUND(__MWERKS__,<=0x3003)
- #pragma parse_mfunc_templ reset
- #endif
-
- bool operator< (const rational& r) const;
- BOOST_CONSTEXPR
- bool operator== (const rational& r) const;
- bool operator< (param_type i) const;
- bool operator> (param_type i) const;
- BOOST_CONSTEXPR
- bool operator== (param_type i) const;
- private:
-
-
- IntType num;
- IntType den;
-
- static BOOST_CONSTEXPR
- int_type inner_gcd( param_type a, param_type b, int_type const &zero =
- int_type(0) )
- { return b == zero ? a : inner_gcd(b, a % b, zero); }
- static BOOST_CONSTEXPR
- int_type inner_abs( param_type x, int_type const &zero = int_type(0) )
- { return x < zero ? -x : +x; }
-
-
-
-
- bool test_invariant() const;
- void normalize();
- static BOOST_CONSTEXPR
- bool is_normalized( param_type n, param_type d, int_type const &zero =
- int_type(0), int_type const &one = int_type(1) )
- {
- return d > zero && ( n != zero || d == one ) && inner_abs( inner_gcd(n,
- d, zero), zero ) == one;
- }
- };
- template <typename IntType>
- inline rational<IntType>& rational<IntType>::assign(param_type n, param_type d)
- {
- return *this = rational( n, d );
- }
- template <typename IntType>
- BOOST_CONSTEXPR
- inline rational<IntType> operator+ (const rational<IntType>& r)
- {
- return r;
- }
- template <typename IntType>
- inline rational<IntType> operator- (const rational<IntType>& r)
- {
- return rational<IntType>(-r.numerator(), r.denominator());
- }
- template <typename IntType>
- rational<IntType>& rational<IntType>::operator+= (const rational<IntType>& r)
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- IntType r_num = r.num;
- IntType r_den = r.den;
- IntType g = integer::gcd(den, r_den);
- den /= g;
- num = num * (r_den / g) + r_num * den;
- g = integer::gcd(num, g);
- num /= g;
- den *= r_den/g;
- return *this;
- }
- template <typename IntType>
- rational<IntType>& rational<IntType>::operator-= (const rational<IntType>& r)
- {
-
- IntType r_num = r.num;
- IntType r_den = r.den;
-
-
- IntType g = integer::gcd(den, r_den);
- den /= g;
- num = num * (r_den / g) - r_num * den;
- g = integer::gcd(num, g);
- num /= g;
- den *= r_den/g;
- return *this;
- }
- template <typename IntType>
- rational<IntType>& rational<IntType>::operator*= (const rational<IntType>& r)
- {
-
- IntType r_num = r.num;
- IntType r_den = r.den;
-
- IntType gcd1 = integer::gcd(num, r_den);
- IntType gcd2 = integer::gcd(r_num, den);
- num = (num/gcd1) * (r_num/gcd2);
- den = (den/gcd2) * (r_den/gcd1);
- return *this;
- }
- template <typename IntType>
- rational<IntType>& rational<IntType>::operator/= (const rational<IntType>& r)
- {
-
- IntType r_num = r.num;
- IntType r_den = r.den;
-
- IntType zero(0);
-
- if (r_num == zero)
- BOOST_THROW_EXCEPTION(bad_rational());
- if (num == zero)
- return *this;
-
- IntType gcd1 = integer::gcd(num, r_num);
- IntType gcd2 = integer::gcd(r_den, den);
- num = (num/gcd1) * (r_den/gcd2);
- den = (den/gcd2) * (r_num/gcd1);
- if (den < zero) {
- num = -num;
- den = -den;
- }
- return *this;
- }
- template <typename IntType>
- inline rational<IntType>&
- rational<IntType>::operator*= (param_type i)
- {
-
- IntType gcd = integer::gcd(i, den);
- num *= i / gcd;
- den /= gcd;
- return *this;
- }
- template <typename IntType>
- rational<IntType>&
- rational<IntType>::operator/= (param_type i)
- {
-
- IntType const zero(0);
- if(i == zero) BOOST_THROW_EXCEPTION(bad_rational());
- if (num == zero) return *this;
-
- IntType const gcd = integer::gcd(num, i);
- num /= gcd;
- den *= i / gcd;
- if (den < zero) {
- num = -num;
- den = -den;
- }
- return *this;
- }
- template <typename IntType>
- bool rational<IntType>::operator< (const rational<IntType>& r) const
- {
-
- int_type const zero( 0 );
-
-
-
-
- BOOST_ASSERT( this->den > zero );
- BOOST_ASSERT( r.den > zero );
-
-
- struct { int_type n, d, q, r; }
- ts = { this->num, this->den, static_cast<int_type>(this->num / this->den),
- static_cast<int_type>(this->num % this->den) },
- rs = { r.num, r.den, static_cast<int_type>(r.num / r.den),
- static_cast<int_type>(r.num % r.den) };
- unsigned reverse = 0u;
-
-
-
-
-
- while ( ts.r < zero ) { ts.r += ts.d; --ts.q; }
- while ( rs.r < zero ) { rs.r += rs.d; --rs.q; }
-
- for ( ;; )
- {
-
-
-
- if ( ts.q != rs.q )
- {
-
-
-
- return reverse ? ts.q > rs.q : ts.q < rs.q;
- }
-
- reverse ^= 1u;
- if ( (ts.r == zero) || (rs.r == zero) )
- {
-
- break;
- }
- ts.n = ts.d; ts.d = ts.r;
- ts.q = ts.n / ts.d; ts.r = ts.n % ts.d;
- rs.n = rs.d; rs.d = rs.r;
- rs.q = rs.n / rs.d; rs.r = rs.n % rs.d;
- }
-
- if ( ts.r == rs.r )
- {
-
-
-
- return false;
- }
- else
- {
- #ifdef BOOST_MSVC
- #pragma warning(push)
- #pragma warning(disable:4800)
- #endif
-
-
-
-
- return ( ts.r != zero ) != static_cast<bool>( reverse );
- #ifdef BOOST_MSVC
- #pragma warning(pop)
- #endif
- }
- }
- template <typename IntType>
- bool rational<IntType>::operator< (param_type i) const
- {
-
- int_type const zero( 0 );
-
- BOOST_ASSERT( this->den > zero );
- int_type q = this->num / this->den, r = this->num % this->den;
- while ( r < zero ) { r += this->den; --q; }
-
-
-
-
- return q < i;
- }
- template <typename IntType>
- bool rational<IntType>::operator> (param_type i) const
- {
- return operator==(i)? false: !operator<(i);
- }
- template <typename IntType>
- BOOST_CONSTEXPR
- inline bool rational<IntType>::operator== (const rational<IntType>& r) const
- {
- return ((num == r.num) && (den == r.den));
- }
- template <typename IntType>
- BOOST_CONSTEXPR
- inline bool rational<IntType>::operator== (param_type i) const
- {
- return ((den == IntType(1)) && (num == i));
- }
- template <typename IntType>
- inline bool rational<IntType>::test_invariant() const
- {
- return ( this->den > int_type(0) ) && ( integer::gcd(this->num, this->den) ==
- int_type(1) );
- }
- template <typename IntType>
- void rational<IntType>::normalize()
- {
-
- IntType zero(0);
- if (den == zero)
- BOOST_THROW_EXCEPTION(bad_rational());
-
- if (num == zero) {
- den = IntType(1);
- return;
- }
- IntType g = integer::gcd(num, den);
- num /= g;
- den /= g;
-
- if (den < zero) {
- num = -num;
- den = -den;
- }
-
-
-
-
- if (den < zero)
- BOOST_THROW_EXCEPTION(bad_rational("bad rational: non-zero singular denominator"));
- BOOST_ASSERT( this->test_invariant() );
- }
- #ifndef BOOST_NO_IOSTREAM
- namespace detail {
-
-
- struct resetter {
- resetter(std::istream& is) : is_(is), f_(is.flags()) {}
- ~resetter() { is_.flags(f_); }
- std::istream& is_;
- std::istream::fmtflags f_;
- };
- }
- template <typename IntType>
- std::istream& operator>> (std::istream& is, rational<IntType>& r)
- {
- using std::ios;
- IntType n = IntType(0), d = IntType(1);
- char c = 0;
- detail::resetter sentry(is);
- if ( is >> n )
- {
- if ( is.get(c) )
- {
- if ( c == '/' )
- {
- if ( is >> std::noskipws >> d )
- try {
- r.assign( n, d );
- } catch ( bad_rational & ) {
- try { is.setstate(ios::failbit); }
- catch ( ... ) {}
- if ( is.exceptions() & ios::failbit )
- throw;
-
- }
- }
- else
- is.setstate( ios::failbit );
- }
- }
- return is;
- }
- template <typename IntType>
- std::ostream& operator<< (std::ostream& os, const rational<IntType>& r)
- {
- using namespace std;
-
- ostringstream ss;
- ss.copyfmt( os );
- ss.tie( NULL );
- ss.exceptions( ios::goodbit );
- ss.width( 0 );
- ss << noshowpos << noshowbase << '/' << r.denominator();
-
- string const tail = ss.str();
- streamsize const w = os.width() - static_cast<streamsize>( tail.size() );
- ss.clear();
- ss.str( "" );
- ss.flags( os.flags() );
- ss << setw( w < 0 || (os.flags() & ios::adjustfield) != ios::internal ? 0 :
- w ) << r.numerator();
- return os << ss.str() + tail;
- }
- #endif
- template <typename T, typename IntType>
- BOOST_CONSTEXPR
- inline T rational_cast(const rational<IntType>& src)
- {
- return static_cast<T>(src.numerator())/static_cast<T>(src.denominator());
- }
- template <typename IntType>
- inline rational<IntType> abs(const rational<IntType>& r)
- {
- return r.numerator() >= IntType(0)? r: -r;
- }
- namespace integer {
- template <typename IntType>
- struct gcd_evaluator< rational<IntType> >
- {
- typedef rational<IntType> result_type,
- first_argument_type, second_argument_type;
- result_type operator() ( first_argument_type const &a
- , second_argument_type const &b
- ) const
- {
- return result_type(integer::gcd(a.numerator(), b.numerator()),
- integer::lcm(a.denominator(), b.denominator()));
- }
- };
- template <typename IntType>
- struct lcm_evaluator< rational<IntType> >
- {
- typedef rational<IntType> result_type,
- first_argument_type, second_argument_type;
- result_type operator() ( first_argument_type const &a
- , second_argument_type const &b
- ) const
- {
- return result_type(integer::lcm(a.numerator(), b.numerator()),
- integer::gcd(a.denominator(), b.denominator()));
- }
- };
- }
- }
- #endif
|