Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r83517 - sandbox/rational
From: dansearles_at_[hidden]
Date: 2013-03-22 10:00:45


Author: mrdans
Date: 2013-03-22 10:00:45 EDT (Fri, 22 Mar 2013)
New Revision: 83517
URL: http://svn.boost.org/trac/boost/changeset/83517

Log:
Move unary +,- inside class, made assignNoNorm() private, changed negation.
Text files modified:
   sandbox/rational/rational.hpp | 84 ++++++++++++++++++++++++---------------
   1 files changed, 51 insertions(+), 33 deletions(-)

Modified: sandbox/rational/rational.hpp
==============================================================================
--- sandbox/rational/rational.hpp (original)
+++ sandbox/rational/rational.hpp 2013-03-22 10:00:45 EDT (Fri, 22 Mar 2013)
@@ -88,7 +88,7 @@
     const IntType zero(0);
    
     // Operate on positive values, and track sign for final result.
- // This requires Inttype be signed.
+ // This requires IntType be signed.
     bool sign_neg = false;
     if(f1<zero) {
         f1=-f1; sign_neg = !sign_neg;
@@ -266,6 +266,10 @@
     IntType numerator() const { return num; }
     IntType denominator() const { return den; }
 
+ // Unary plus, minus
+ rational operator+() const;
+ rational operator-() const;
+
     // Arithmetic assignment operators
     rational& operator+= (const rational& r);
     rational& operator-= (const rational& r);
@@ -307,7 +311,6 @@
     bool operator> (param_type i) const;
     bool operator== (param_type i) const;
 
- rational& assignNoNorm(param_type n, param_type d) { num = n; den = d; return *this;}
 
 private:
     // Implementation - numerator and denominator (normalized).
@@ -315,6 +318,8 @@
     IntType num;
     IntType den;
 
+ rational& assignNoNorm(param_type n, param_type d) { num = n; den = d; return *this;}
+
     // Representation note: Fractions are kept in normalized form at all
     // times. Normalized form is defined as gcd(num,den) == 1 and den > 0.
     // In particular, note that the implementation of abs() below relies
@@ -329,7 +334,7 @@
 
 
 
-// Partially specialize the class for the overflow checking.
+// Partially specialize the class for the overflow checking.
 template <typename IntType>
 class rational<IntType, rational_check_for_overflow> :
     less_than_comparable < rational<IntType, rational_check_for_overflow>,
@@ -380,6 +385,10 @@
     inline IntType numerator() const { return num; }
     inline IntType denominator() const { return den; }
 
+ // Unary plus, minus
+ rational operator+() const;
+ rational operator-() const;
+
     // Arithmetic assignment operators
     rational& operator+= (const rational& r);
     rational& operator-= (const rational& r);
@@ -421,7 +430,6 @@
     bool operator> (param_type i) const;
     bool operator== (param_type i) const;
 
- rational& assignNoNorm(param_type n, param_type d) { num = n; den = d; return *this;}
 
 private:
     // Implementation - numerator and denominator (normalized).
@@ -429,6 +437,8 @@
     IntType num;
     IntType den;
 
+ rational& assignNoNorm(param_type n, param_type d) { num = n; den = d; return *this;}
+
     // Representation note: Fractions are kept in normalized form at all
     // times. Normalized form is defined as gcd(num,den) == 1 and den > 0.
     // In particular, note that the implementation of abs() below relies
@@ -455,31 +465,39 @@
     return *this;
 }
 
-// Unary plus (for any/all CheckingMode's)
+// Unary plus
 template <typename IntType, rational_checktype CheckingMode>
-inline rational<IntType, CheckingMode> operator+ (const rational<IntType, CheckingMode>& r)
+rational<IntType, CheckingMode> rational<IntType, CheckingMode>::operator+ () const
 {
- return r;
+ rational<IntType, CheckingMode> retRational;
+ retRational.assignNoNorm(num, den);
+ return retRational;
+}
+template <typename IntType>
+rational<IntType, rational_check_for_overflow> rational<IntType, rational_check_for_overflow>::operator+ () const
+{
+ rational<IntType, rational_check_for_overflow> retRational;
+ retRational.assignNoNorm(num, den);
+ return retRational;
 }
 
 // Unary minus
 template <typename IntType, rational_checktype CheckingMode>
-rational<IntType, CheckingMode>
-operator- (const rational<IntType, CheckingMode>& r)
+rational<IntType, CheckingMode> rational<IntType, CheckingMode>::operator- () const
 {
     rational<IntType, CheckingMode> retRational;
- retRational.assignNoNorm(-r.numerator(), r.denominator());
+ retRational.assignNoNorm(-num, den);
     return retRational;
 }
 template <typename IntType>
-rational<IntType, rational_check_for_overflow> operator- (const rational<IntType, rational_check_for_overflow>& r)
+rational<IntType, rational_check_for_overflow> rational<IntType, rational_check_for_overflow>::operator- () const
 {
- IntType negnumer = -r.numerator();
- if(negnumer && (r.numerator() == negnumer))
+ IntType negnumer = -num;
+ if(negnumer && (num == negnumer))
         throw rational_overflow();
 
     rational<IntType, rational_check_for_overflow> retRational;
- retRational.assignNoNorm(negnumer, r.denominator());
+ retRational.assignNoNorm(-num, den);
     return retRational;
 }
 
@@ -489,7 +507,7 @@
 
 // Arithmetic assignment operators
 template <typename IntType, rational_checktype CheckingMode>
- rational<IntType, CheckingMode>& rational<IntType, CheckingMode>::operator+= (const rational<IntType, CheckingMode>& r)
+rational<IntType, CheckingMode>& rational<IntType, CheckingMode>::operator+= (const rational<IntType, CheckingMode>& r)
 {
     // This calculation avoids overflow, and minimises the number of expensive
     // calculations. Thanks to Nickolay Mladenov for this algorithm.
@@ -567,7 +585,7 @@
 
 template <typename IntType, rational_checktype CheckingMode>
 rational<IntType, CheckingMode>&
-rational<IntType, CheckingMode>::operator-= (const rational<IntType, CheckingMode>& r)
+ rational<IntType, CheckingMode>::operator-= (const rational<IntType, CheckingMode>& r)
 {
     // Protect against self-modification
     IntType r_num = r.num;
@@ -594,7 +612,7 @@
     }
             
     // Negate the denominator (which is safe), and use constructor 'normalize' to check for overflow.
- IntType negrden = ~r.den; ++negrden;
+ IntType negrden = -r.den;
     rational<IntType, rational_check_for_overflow> negOperand(r.num, negrden);
     return operator+= (negOperand);
 }
@@ -649,7 +667,7 @@
 }
 
 template <typename IntType, rational_checktype CheckingMode>
-rational<IntType, CheckingMode>&
+rational<IntType, CheckingMode>&
 rational<IntType, CheckingMode>::operator/= (const rational<IntType, CheckingMode>& r)
 {
     // Protect against self-modification
@@ -719,8 +737,8 @@
 
     if (den < zero)
     {
- IntType neg_den = ~den; ++neg_den;
- IntType neg_num = ~num; ++neg_num;
+ IntType neg_den = -den;
+ IntType neg_num = -num;
         if(den == neg_den) // den never zero here
             throw rational_overflow();
         if(num && (num == neg_num))
@@ -734,22 +752,22 @@
 
 // Mixed-mode operators
 template <typename IntType, rational_checktype CheckingMode>
- inline rational<IntType, CheckingMode>&
-rational<IntType, CheckingMode>::operator+= (param_type i)
+inline rational<IntType, CheckingMode>&
+ rational<IntType, CheckingMode>::operator+= (param_type i)
 {
     num += i * den;
     return *this;
 }
 template <typename IntType>
 inline rational<IntType, rational_check_for_overflow>&
-rational<IntType, rational_check_for_overflow>::operator+= (param_type i)
+ rational<IntType, rational_check_for_overflow>::operator+= (param_type i)
 {
     return operator+= (rational<IntType, rational_check_for_overflow>(i));
 }
 
 template <typename IntType, rational_checktype CheckingMode>
- inline rational<IntType, CheckingMode>&
-rational<IntType, CheckingMode>::operator-= (param_type i)
+inline rational<IntType, CheckingMode>&
+ rational<IntType, CheckingMode>::operator-= (param_type i)
 {
     num -= i * den;
     return *this;
@@ -757,7 +775,7 @@
 
 template <typename IntType>
 inline rational<IntType, rational_check_for_overflow>&
-rational<IntType, rational_check_for_overflow>::operator-= (param_type i)
+ rational<IntType, rational_check_for_overflow>::operator-= (param_type i)
 {
     return operator-= (rational<IntType, rational_check_for_overflow>(i));
 }
@@ -765,25 +783,25 @@
 
 template <typename IntType, rational_checktype CheckingMode>
 inline rational<IntType, CheckingMode>&
-rational<IntType, CheckingMode>::operator*= (param_type i)
+ rational<IntType, CheckingMode>::operator*= (param_type i)
 {
     // Avoid overflow and preserve normalization
     IntType gcd = math::gcd(i, den);
- num *= i / gcd;
+ num *= i / gcd;
     den /= gcd;
 
     return *this;
 }
 template <typename IntType>
 inline rational<IntType, rational_check_for_overflow>&
-rational<IntType, rational_check_for_overflow>::operator*= (param_type i)
+ rational<IntType, rational_check_for_overflow>::operator*= (param_type i)
 {
     return operator*= (rational<IntType, rational_check_for_overflow>(i));
 }
 
 template <typename IntType, rational_checktype CheckingMode>
 inline rational<IntType, CheckingMode>&
-rational<IntType, CheckingMode>::operator/= (param_type i)
+ rational<IntType, CheckingMode>::operator/= (param_type i)
 {
     // Avoid repeated construction
     IntType const zero(0);
@@ -806,7 +824,7 @@
 }
 template <typename IntType>
 inline rational<IntType, rational_check_for_overflow>&
-rational<IntType, rational_check_for_overflow>::operator/= (param_type i)
+ rational<IntType, rational_check_for_overflow>::operator/= (param_type i)
 {
     return operator/= (rational<IntType, rational_check_for_overflow>(i));
 }
@@ -852,7 +870,7 @@
 // Comparison operators
 template <typename IntType, rational_checktype CheckingMode>
 bool
-rational<IntType, CheckingMode>::operator< (const rational<IntType, CheckingMode>& r) const
+rational<IntType, CheckingMode>::operator< (const rational<IntType, CheckingMode>& r) const
 {
     // Avoid repeated construction
     int_type const zero( 0 );
@@ -1007,7 +1025,7 @@
 }
 
 template <typename IntType, rational_checktype CheckingMode>
- bool rational<IntType, CheckingMode>::operator== (const rational<IntType, CheckingMode>& r) const
+ bool rational<IntType, CheckingMode>::operator== (const rational<IntType, CheckingMode>& r) const
 {
     return ((num == r.num) && (den == r.den));
 }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk