12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046 |
- #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 <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>
- #include <boost/utility/enable_if.hpp>
- #include <boost/type_traits/is_convertible.hpp>
- #include <boost/type_traits/is_class.hpp>
- #include <boost/type_traits/is_same.hpp>
- #include <boost/type_traits/is_array.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
- namespace rational_detail{
- template <class FromInt, class ToInt, typename Enable = void>
- struct is_compatible_integer;
- template <class FromInt, class ToInt>
- struct is_compatible_integer<FromInt, ToInt, typename enable_if_c<!is_array<FromInt>::value>::type>
- {
- BOOST_STATIC_CONSTANT(bool, value = ((std::numeric_limits<FromInt>::is_specialized && std::numeric_limits<FromInt>::is_integer
- && (std::numeric_limits<FromInt>::digits <= std::numeric_limits<ToInt>::digits)
- && (std::numeric_limits<FromInt>::radix == std::numeric_limits<ToInt>::radix)
- && ((std::numeric_limits<FromInt>::is_signed == false) || (std::numeric_limits<ToInt>::is_signed == true))
- && is_convertible<FromInt, ToInt>::value)
- || is_same<FromInt, ToInt>::value)
- || (is_class<ToInt>::value && is_class<FromInt>::value && is_convertible<FromInt, ToInt>::value));
- };
- template <class FromInt, class ToInt>
- struct is_compatible_integer<FromInt, ToInt, typename enable_if_c<is_array<FromInt>::value>::type>
- {
- BOOST_STATIC_CONSTANT(bool, value = false);
- };
- template <class FromInt, class ToInt, typename Enable = void>
- struct is_backward_compatible_integer;
- template <class FromInt, class ToInt>
- struct is_backward_compatible_integer<FromInt, ToInt, typename enable_if_c<!is_array<FromInt>::value>::type>
- {
- BOOST_STATIC_CONSTANT(bool, value = (std::numeric_limits<FromInt>::is_specialized && std::numeric_limits<FromInt>::is_integer
- && !is_compatible_integer<FromInt, ToInt>::value
- && (std::numeric_limits<FromInt>::radix == std::numeric_limits<ToInt>::radix)
- && is_convertible<FromInt, ToInt>::value));
- };
- template <class FromInt, class ToInt>
- struct is_backward_compatible_integer<FromInt, ToInt, typename enable_if_c<is_array<FromInt>::value>::type>
- {
- BOOST_STATIC_CONSTANT(bool, value = false);
- };
- }
- 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
- {
-
- 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) {}
- template <class T>//, typename enable_if_c<!is_array<T>::value>::type>
- BOOST_CONSTEXPR rational(const T& n, typename enable_if_c<
- rational_detail::is_compatible_integer<T, IntType>::value
- >::type const* = 0) : num(n), den(1) {}
- template <class T, class U>
- BOOST_CXX14_CONSTEXPR rational(const T& n, const U& d, typename enable_if_c<
- rational_detail::is_compatible_integer<T, IntType>::value && rational_detail::is_compatible_integer<U, IntType>::value
- >::type const* = 0) : num(n), den(d) {
- normalize();
- }
- template < typename NewType >
- BOOST_CONSTEXPR explicit
- rational(rational<NewType> const &r, typename enable_if_c<rational_detail::is_compatible_integer<NewType, IntType>::value>::type const* = 0)
- : 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)){}
- template < typename NewType >
- BOOST_CONSTEXPR explicit
- rational(rational<NewType> const &r, typename disable_if_c<rational_detail::is_compatible_integer<NewType, IntType>::value>::type const* = 0)
- : num(r.numerator()), den(is_normalized(int_type(r.numerator()),
- int_type(r.denominator())) && is_safe_narrowing_conversion(r.denominator()) && is_safe_narrowing_conversion(r.numerator()) ? r.denominator() :
- (BOOST_THROW_EXCEPTION(bad_rational("bad rational: denormalized conversion")), 0)){}
-
-
- template <class T>
- BOOST_CXX14_CONSTEXPR typename enable_if_c<
- rational_detail::is_compatible_integer<T, IntType>::value, rational &
- >::type operator=(const T& n) { return assign(static_cast<IntType>(n), static_cast<IntType>(1)); }
-
- template <class T, class U>
- BOOST_CXX14_CONSTEXPR typename enable_if_c<
- rational_detail::is_compatible_integer<T, IntType>::value && rational_detail::is_compatible_integer<U, IntType>::value, rational &
- >::type assign(const T& n, const U& d)
- {
- return *this = rational<IntType>(static_cast<IntType>(n), static_cast<IntType>(d));
- }
-
-
-
-
-
-
-
-
- template <class T>//, typename enable_if_c<!is_array<T>::value>::type>
- BOOST_CXX14_CONSTEXPR rational(const T& n, typename enable_if_c<
- rational_detail::is_backward_compatible_integer<T, IntType>::value
- >::type const* = 0)
- {
- assign(n, static_cast<T>(1));
- }
- template <class T, class U>
- BOOST_CXX14_CONSTEXPR rational(const T& n, const U& d, typename enable_if_c<
- (!rational_detail::is_compatible_integer<T, IntType>::value
- || !rational_detail::is_compatible_integer<U, IntType>::value)
- && std::numeric_limits<T>::is_specialized && std::numeric_limits<T>::is_integer
- && (std::numeric_limits<T>::radix == std::numeric_limits<IntType>::radix)
- && is_convertible<T, IntType>::value &&
- std::numeric_limits<U>::is_specialized && std::numeric_limits<U>::is_integer
- && (std::numeric_limits<U>::radix == std::numeric_limits<IntType>::radix)
- && is_convertible<U, IntType>::value
- >::type const* = 0)
- {
- assign(n, d);
- }
- template <class T>
- BOOST_CXX14_CONSTEXPR typename enable_if_c<
- std::numeric_limits<T>::is_specialized && std::numeric_limits<T>::is_integer
- && !rational_detail::is_compatible_integer<T, IntType>::value
- && (std::numeric_limits<T>::radix == std::numeric_limits<IntType>::radix)
- && is_convertible<T, IntType>::value,
- rational &
- >::type operator=(const T& n) { return assign(n, static_cast<T>(1)); }
- template <class T, class U>
- BOOST_CXX14_CONSTEXPR typename enable_if_c<
- (!rational_detail::is_compatible_integer<T, IntType>::value
- || !rational_detail::is_compatible_integer<U, IntType>::value)
- && std::numeric_limits<T>::is_specialized && std::numeric_limits<T>::is_integer
- && (std::numeric_limits<T>::radix == std::numeric_limits<IntType>::radix)
- && is_convertible<T, IntType>::value &&
- std::numeric_limits<U>::is_specialized && std::numeric_limits<U>::is_integer
- && (std::numeric_limits<U>::radix == std::numeric_limits<IntType>::radix)
- && is_convertible<U, IntType>::value,
- rational &
- >::type assign(const T& n, const U& d)
- {
- if(!is_safe_narrowing_conversion(n) || !is_safe_narrowing_conversion(d))
- BOOST_THROW_EXCEPTION(bad_rational());
- return *this = rational<IntType>(static_cast<IntType>(n), static_cast<IntType>(d));
- }
-
- BOOST_CONSTEXPR
- const IntType& numerator() const { return num; }
- BOOST_CONSTEXPR
- const IntType& denominator() const { return den; }
-
- BOOST_CXX14_CONSTEXPR rational& operator+= (const rational& r);
- BOOST_CXX14_CONSTEXPR rational& operator-= (const rational& r);
- BOOST_CXX14_CONSTEXPR rational& operator*= (const rational& r);
- BOOST_CXX14_CONSTEXPR rational& operator/= (const rational& r);
- template <class T>
- BOOST_CXX14_CONSTEXPR typename boost::enable_if_c<rational_detail::is_compatible_integer<T, IntType>::value, rational&>::type operator+= (const T& i)
- {
- num += i * den;
- return *this;
- }
- template <class T>
- BOOST_CXX14_CONSTEXPR typename boost::enable_if_c<rational_detail::is_compatible_integer<T, IntType>::value, rational&>::type operator-= (const T& i)
- {
- num -= i * den;
- return *this;
- }
- template <class T>
- BOOST_CXX14_CONSTEXPR typename boost::enable_if_c<rational_detail::is_compatible_integer<T, IntType>::value, rational&>::type operator*= (const T& i)
- {
-
- IntType gcd = integer::gcd(static_cast<IntType>(i), den);
- num *= i / gcd;
- den /= gcd;
- return *this;
- }
- template <class T>
- BOOST_CXX14_CONSTEXPR typename boost::enable_if_c<rational_detail::is_compatible_integer<T, IntType>::value, rational&>::type operator/= (const T& i)
- {
-
- IntType const zero(0);
- if(i == zero) BOOST_THROW_EXCEPTION(bad_rational());
- if(num == zero) return *this;
-
- IntType const gcd = integer::gcd(num, static_cast<IntType>(i));
- num /= gcd;
- den *= i / gcd;
- if(den < zero) {
- num = -num;
- den = -den;
- }
- return *this;
- }
-
- BOOST_CXX14_CONSTEXPR const rational& operator++() { num += den; return *this; }
- BOOST_CXX14_CONSTEXPR const rational& operator--() { num -= den; return *this; }
- BOOST_CXX14_CONSTEXPR rational operator++(int)
- {
- rational t(*this);
- ++(*this);
- return t;
- }
- BOOST_CXX14_CONSTEXPR rational operator--(int)
- {
- rational t(*this);
- --(*this);
- return t;
- }
-
- 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
-
- BOOST_CXX14_CONSTEXPR bool operator< (const rational& r) const;
- BOOST_CXX14_CONSTEXPR bool operator> (const rational& r) const { return r < *this; }
- BOOST_CONSTEXPR
- bool operator== (const rational& r) const;
- template <class T>
- BOOST_CXX14_CONSTEXPR typename boost::enable_if_c<rational_detail::is_compatible_integer<T, IntType>::value, bool>::type operator< (const T& 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 <class T>
- BOOST_CXX14_CONSTEXPR typename boost::enable_if_c<rational_detail::is_compatible_integer<T, IntType>::value, bool>::type operator>(const T& i) const
- {
- return operator==(i) ? false : !operator<(i);
- }
- template <class T>
- BOOST_CONSTEXPR typename boost::enable_if_c<rational_detail::is_compatible_integer<T, IntType>::value, bool>::type operator== (const T& i) const
- {
- return ((den == IntType(1)) && (num == i));
- }
- 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; }
-
-
-
-
- BOOST_CXX14_CONSTEXPR bool test_invariant() const;
- BOOST_CXX14_CONSTEXPR 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 <class T>
- BOOST_CONSTEXPR static typename boost::enable_if_c<(std::numeric_limits<T>::digits > std::numeric_limits<IntType>::digits) && (std::numeric_limits<T>::is_signed == false), bool>::type is_safe_narrowing_conversion(const T& val)
- {
- return val < (T(1) << std::numeric_limits<IntType>::digits);
- }
-
-
-
- template <class T>
- BOOST_CONSTEXPR static typename boost::enable_if_c<(std::numeric_limits<T>::digits > std::numeric_limits<IntType>::digits) && (std::numeric_limits<T>::is_signed == true) && (std::numeric_limits<IntType>::is_signed == true), bool>::type is_safe_narrowing_conversion(const T& val)
- {
-
-
-
-
- return (val < (T(1) << std::numeric_limits<IntType>::digits)) && (val >= -(T(1) << std::numeric_limits<IntType>::digits));
- }
-
-
-
- template <class T>
- BOOST_CONSTEXPR static typename boost::enable_if_c<(std::numeric_limits<T>::digits > std::numeric_limits<IntType>::digits) && (std::numeric_limits<T>::is_signed == true) && (std::numeric_limits<IntType>::is_signed == false), bool>::type is_safe_narrowing_conversion(const T& val)
- {
- return (val < (T(1) << std::numeric_limits<IntType>::digits)) && (val >= 0);
- }
-
-
-
- template <class T>
- BOOST_CONSTEXPR static typename boost::enable_if_c<(std::numeric_limits<T>::digits <= std::numeric_limits<IntType>::digits) && (std::numeric_limits<T>::is_signed == true) && (std::numeric_limits<IntType>::is_signed == false), bool>::type is_safe_narrowing_conversion(const T& val)
- {
- return val >= 0;
- }
-
-
-
- template <class T>
- BOOST_CONSTEXPR static typename boost::enable_if_c<(std::numeric_limits<T>::digits <= std::numeric_limits<IntType>::digits) && (std::numeric_limits<T>::is_signed == false) && (std::numeric_limits<IntType>::is_signed == true), bool>::type is_safe_narrowing_conversion(const T&)
- {
- return true;
- }
-
-
-
- template <class T>
- BOOST_CONSTEXPR static typename boost::enable_if_c<(std::numeric_limits<T>::digits <= std::numeric_limits<IntType>::digits) && (std::numeric_limits<T>::is_signed == false) && (std::numeric_limits<IntType>::is_signed == false), bool>::type is_safe_narrowing_conversion(const T&)
- {
- return true;
- }
-
-
-
- template <class T>
- BOOST_CONSTEXPR static typename boost::enable_if_c<(std::numeric_limits<T>::digits <= std::numeric_limits<IntType>::digits) && (std::numeric_limits<T>::is_signed == true) && (std::numeric_limits<IntType>::is_signed == true), bool>::type is_safe_narrowing_conversion(const T&)
- {
- return true;
- }
- };
- template <typename IntType>
- BOOST_CONSTEXPR
- inline rational<IntType> operator+ (const rational<IntType>& r)
- {
- return r;
- }
- template <typename IntType>
- BOOST_CXX14_CONSTEXPR
- inline rational<IntType> operator- (const rational<IntType>& r)
- {
- return rational<IntType>(static_cast<IntType>(-r.numerator()), r.denominator());
- }
- template <typename IntType>
- BOOST_CXX14_CONSTEXPR 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>
- BOOST_CXX14_CONSTEXPR 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>
- BOOST_CXX14_CONSTEXPR 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>
- BOOST_CXX14_CONSTEXPR 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 <class IntType, class Arg>
- BOOST_CXX14_CONSTEXPR
- inline typename boost::enable_if_c <
- rational_detail::is_compatible_integer<Arg, IntType>::value || is_same<rational<IntType>, Arg>::value, rational<IntType> >::type
- operator + (const rational<IntType>& a, const Arg& b)
- {
- rational<IntType> t(a);
- return t += b;
- }
- template <class Arg, class IntType>
- BOOST_CXX14_CONSTEXPR
- inline typename boost::enable_if_c <
- rational_detail::is_compatible_integer<Arg, IntType>::value, rational<IntType> >::type
- operator + (const Arg& b, const rational<IntType>& a)
- {
- rational<IntType> t(a);
- return t += b;
- }
- template <class IntType, class Arg>
- BOOST_CXX14_CONSTEXPR
- inline typename boost::enable_if_c <
- rational_detail::is_compatible_integer<Arg, IntType>::value || is_same<rational<IntType>, Arg>::value, rational<IntType> >::type
- operator - (const rational<IntType>& a, const Arg& b)
- {
- rational<IntType> t(a);
- return t -= b;
- }
- template <class Arg, class IntType>
- BOOST_CXX14_CONSTEXPR
- inline typename boost::enable_if_c <
- rational_detail::is_compatible_integer<Arg, IntType>::value, rational<IntType> >::type
- operator - (const Arg& b, const rational<IntType>& a)
- {
- rational<IntType> t(a);
- return -(t -= b);
- }
- template <class IntType, class Arg>
- BOOST_CXX14_CONSTEXPR
- inline typename boost::enable_if_c <
- rational_detail::is_compatible_integer<Arg, IntType>::value || is_same<rational<IntType>, Arg>::value, rational<IntType> >::type
- operator * (const rational<IntType>& a, const Arg& b)
- {
- rational<IntType> t(a);
- return t *= b;
- }
- template <class Arg, class IntType>
- BOOST_CXX14_CONSTEXPR
- inline typename boost::enable_if_c <
- rational_detail::is_compatible_integer<Arg, IntType>::value, rational<IntType> >::type
- operator * (const Arg& b, const rational<IntType>& a)
- {
- rational<IntType> t(a);
- return t *= b;
- }
- template <class IntType, class Arg>
- BOOST_CXX14_CONSTEXPR
- inline typename boost::enable_if_c <
- rational_detail::is_compatible_integer<Arg, IntType>::value || is_same<rational<IntType>, Arg>::value, rational<IntType> >::type
- operator / (const rational<IntType>& a, const Arg& b)
- {
- rational<IntType> t(a);
- return t /= b;
- }
- template <class Arg, class IntType>
- BOOST_CXX14_CONSTEXPR
- inline typename boost::enable_if_c <
- rational_detail::is_compatible_integer<Arg, IntType>::value, rational<IntType> >::type
- operator / (const Arg& b, const rational<IntType>& a)
- {
- rational<IntType> t(b);
- return t /= a;
- }
- template <class IntType, class Arg>
- BOOST_CXX14_CONSTEXPR
- inline typename boost::enable_if_c <
- rational_detail::is_compatible_integer<Arg, IntType>::value || is_same<rational<IntType>, Arg>::value, bool>::type
- operator <= (const rational<IntType>& a, const Arg& b)
- {
- return !a.operator>(b);
- }
- template <class Arg, class IntType>
- BOOST_CXX14_CONSTEXPR
- inline typename boost::enable_if_c <
- rational_detail::is_compatible_integer<Arg, IntType>::value, bool>::type
- operator <= (const Arg& b, const rational<IntType>& a)
- {
- return a >= b;
- }
- template <class IntType, class Arg>
- BOOST_CXX14_CONSTEXPR
- inline typename boost::enable_if_c <
- rational_detail::is_compatible_integer<Arg, IntType>::value || is_same<rational<IntType>, Arg>::value, bool>::type
- operator >= (const rational<IntType>& a, const Arg& b)
- {
- return !a.operator<(b);
- }
- template <class Arg, class IntType>
- BOOST_CXX14_CONSTEXPR
- inline typename boost::enable_if_c <
- rational_detail::is_compatible_integer<Arg, IntType>::value, bool>::type
- operator >= (const Arg& b, const rational<IntType>& a)
- {
- return a <= b;
- }
- template <class IntType, class Arg>
- BOOST_CONSTEXPR
- inline typename boost::enable_if_c <
- rational_detail::is_compatible_integer<Arg, IntType>::value || is_same<rational<IntType>, Arg>::value, bool>::type
- operator != (const rational<IntType>& a, const Arg& b)
- {
- return !a.operator==(b);
- }
- template <class Arg, class IntType>
- BOOST_CONSTEXPR
- inline typename boost::enable_if_c <
- rational_detail::is_compatible_integer<Arg, IntType>::value, bool>::type
- operator != (const Arg& b, const rational<IntType>& a)
- {
- return !(b == a);
- }
- template <class Arg, class IntType>
- BOOST_CXX14_CONSTEXPR
- inline typename boost::enable_if_c <
- rational_detail::is_compatible_integer<Arg, IntType>::value, bool>::type
- operator < (const Arg& b, const rational<IntType>& a)
- {
- return a.operator>(b);
- }
- template <class Arg, class IntType>
- BOOST_CXX14_CONSTEXPR
- inline typename boost::enable_if_c <
- rational_detail::is_compatible_integer<Arg, IntType>::value, bool>::type
- operator > (const Arg& b, const rational<IntType>& a)
- {
- return a.operator<(b);
- }
- template <class Arg, class IntType>
- BOOST_CONSTEXPR
- inline typename boost::enable_if_c <
- rational_detail::is_compatible_integer<Arg, IntType>::value, bool>::type
- operator == (const Arg& b, const rational<IntType>& a)
- {
- return a.operator==(b);
- }
- template <typename IntType>
- BOOST_CXX14_CONSTEXPR
- 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>
- BOOST_CONSTEXPR
- inline bool rational<IntType>::operator== (const rational<IntType>& r) const
- {
- return ((num == r.num) && (den == r.den));
- }
- template <typename IntType>
- BOOST_CXX14_CONSTEXPR
- 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>
- BOOST_CXX14_CONSTEXPR 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 < -(std::numeric_limits<IntType>::max)()) {
- BOOST_THROW_EXCEPTION(bad_rational("bad rational: non-zero singular denominator"));
- }
-
- if (den < zero) {
- num = -num;
- den = -den;
- }
- 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)
- {
-
- std::ostringstream ss;
- ss.copyfmt( os );
- ss.tie( NULL );
- ss.exceptions( std::ios::goodbit );
- ss.width( 0 );
- ss << std::noshowpos << std::noshowbase << '/' << r.denominator();
-
- std::string const tail = ss.str();
- std::streamsize const w =
- os.width() - static_cast<std::streamsize>( tail.size() );
- ss.clear();
- ss.str( "" );
- ss.flags( os.flags() );
- ss << std::setw( w < 0 || (os.flags() & std::ios::adjustfield) !=
- std::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>
- BOOST_CXX14_CONSTEXPR
- 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
|