Ticket #5721: rational_mod.patch

File rational_mod.patch, 3.5 KB (added by mlang@…, 11 years ago)

The proposed patch

  • boost/rational.hpp

     
    105105
    106106template <typename IntType>
    107107class rational :
    108     less_than_comparable < rational<IntType>,
    109     equality_comparable < rational<IntType>,
    110     less_than_comparable2 < rational<IntType>, IntType,
    111     equality_comparable2 < rational<IntType>, IntType,
    112     addable < rational<IntType>,
    113     subtractable < rational<IntType>,
    114     multipliable < rational<IntType>,
    115     dividable < rational<IntType>,
    116     addable2 < rational<IntType>, IntType,
    117     subtractable2 < rational<IntType>, IntType,
    118     subtractable2_left < rational<IntType>, IntType,
    119     multipliable2 < rational<IntType>, IntType,
    120     dividable2 < rational<IntType>, IntType,
    121     dividable2_left < rational<IntType>, IntType,
    122     incrementable < rational<IntType>,
    123     decrementable < rational<IntType>
    124     > > > > > > > > > > > > > > > >
     108    ordered_euclidean_ring_operators < rational<IntType>,
     109    ordered_euclidean_ring_operators2 < rational<IntType>, IntType,
     110    unit_steppable < rational<IntType>
     111    > > >
    125112{
    126113    // Class-wide pre-conditions
    127114    BOOST_STATIC_ASSERT( ::std::numeric_limits<IntType>::is_specialized );
     
    155142    rational& operator-= (const rational& r);
    156143    rational& operator*= (const rational& r);
    157144    rational& operator/= (const rational& r);
     145    rational& operator%= (const rational& r);
    158146
    159147    rational& operator+= (param_type i);
    160148    rational& operator-= (param_type i);
    161149    rational& operator*= (param_type i);
    162150    rational& operator/= (param_type i);
     151    rational& operator%= (param_type i);
    163152
    164153    // Increment and decrement
    165154    const rational& operator++();
     
    205194    void normalize();
    206195};
    207196
     197// Type conversion
     198template <typename T, typename IntType>
     199inline T rational_cast(
     200    const rational<IntType>& src BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
     201{
     202    return static_cast<T>(src.numerator())/static_cast<T>(src.denominator());
     203}
     204
    208205// Assign in place
    209206template <typename IntType>
    210207inline rational<IntType>& rational<IntType>::assign(param_type n, param_type d)
     
    327324    return *this;
    328325}
    329326
     327template <typename IntType>
     328rational<IntType>& rational<IntType>::operator%= (const rational<IntType>& r)
     329{
     330    return *this -= r * rational_cast<IntType>(*this / r);
     331}
     332
    330333// Mixed-mode operators
    331334template <typename IntType>
    332335inline rational<IntType>&
     
    356359    return operator/= (rational<IntType>(i));
    357360}
    358361
     362template <typename IntType>
     363inline rational<IntType>&
     364rational<IntType>::operator%= (param_type i)
     365{
     366    return operator%= (rational<IntType>(i));
     367}
     368
    359369// Increment and decrement
    360370template <typename IntType>
    361371inline const rational<IntType>& rational<IntType>::operator++()
     
    583593    return os;
    584594}
    585595
    586 // Type conversion
    587 template <typename T, typename IntType>
    588 inline T rational_cast(
    589     const rational<IntType>& src BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
    590 {
    591     return static_cast<T>(src.numerator())/static_cast<T>(src.denominator());
    592 }
    593 
    594596// Do not use any abs() defined on IntType - it isn't worth it, given the
    595597// difficulties involved (Koenig lookup required, there may not *be* an abs()
    596598// defined, etc etc).
    597599template <typename IntType>
    598600inline rational<IntType> abs(const rational<IntType>& r)
    599601{
    600     if (r.numerator() >= IntType(0))
    601         return r;
    602 
    603     return rational<IntType>(-r.numerator(), r.denominator());
     602    return r.numerator() >= IntType(0)? r: -r;
    604603}
    605604
    606605} // namespace boost