Ticket #5721: rational_mod.patch
File rational_mod.patch, 3.5 KB (added by , 11 years ago) |
---|
-
boost/rational.hpp
105 105 106 106 template <typename IntType> 107 107 class 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 > > > 125 112 { 126 113 // Class-wide pre-conditions 127 114 BOOST_STATIC_ASSERT( ::std::numeric_limits<IntType>::is_specialized ); … … 155 142 rational& operator-= (const rational& r); 156 143 rational& operator*= (const rational& r); 157 144 rational& operator/= (const rational& r); 145 rational& operator%= (const rational& r); 158 146 159 147 rational& operator+= (param_type i); 160 148 rational& operator-= (param_type i); 161 149 rational& operator*= (param_type i); 162 150 rational& operator/= (param_type i); 151 rational& operator%= (param_type i); 163 152 164 153 // Increment and decrement 165 154 const rational& operator++(); … … 205 194 void normalize(); 206 195 }; 207 196 197 // Type conversion 198 template <typename T, typename IntType> 199 inline 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 208 205 // Assign in place 209 206 template <typename IntType> 210 207 inline rational<IntType>& rational<IntType>::assign(param_type n, param_type d) … … 327 324 return *this; 328 325 } 329 326 327 template <typename IntType> 328 rational<IntType>& rational<IntType>::operator%= (const rational<IntType>& r) 329 { 330 return *this -= r * rational_cast<IntType>(*this / r); 331 } 332 330 333 // Mixed-mode operators 331 334 template <typename IntType> 332 335 inline rational<IntType>& … … 356 359 return operator/= (rational<IntType>(i)); 357 360 } 358 361 362 template <typename IntType> 363 inline rational<IntType>& 364 rational<IntType>::operator%= (param_type i) 365 { 366 return operator%= (rational<IntType>(i)); 367 } 368 359 369 // Increment and decrement 360 370 template <typename IntType> 361 371 inline const rational<IntType>& rational<IntType>::operator++() … … 583 593 return os; 584 594 } 585 595 586 // Type conversion587 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 594 596 // Do not use any abs() defined on IntType - it isn't worth it, given the 595 597 // difficulties involved (Koenig lookup required, there may not *be* an abs() 596 598 // defined, etc etc). 597 599 template <typename IntType> 598 600 inline rational<IntType> abs(const rational<IntType>& r) 599 601 { 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; 604 603 } 605 604 606 605 } // namespace boost