Rational<Arbint> now passes realops.test
authorDavid Gow <david@ingeniumdigital.com>
Sun, 6 Jul 2014 13:51:10 +0000 (21:51 +0800)
committerDavid Gow <david@ingeniumdigital.com>
Sun, 6 Jul 2014 13:51:10 +0000 (21:51 +0800)
src/arbint.cpp
src/arbint.h
src/rational.h

index 4bde578..ac39942 100644 (file)
@@ -114,6 +114,11 @@ void Arbint::Division(const Arbint & div, Arbint & result, Arbint & remainder) c
 {
        remainder = 0;
        result = 0;
 {
        remainder = 0;
        result = 0;
+       if (div.IsZero())
+       {
+               result = *this;
+               return;
+       }
        for (int i = 8*sizeof(digit_t)*m_digits.size(); i >= 0; --i)
        {
                remainder <<= 1;
        for (int i = 8*sizeof(digit_t)*m_digits.size(); i >= 0; --i)
        {
                remainder <<= 1;
index 0f047c9..06008e0 100644 (file)
@@ -23,7 +23,7 @@ namespace IPDF
                        virtual ~Arbint() {}
                        Arbint(const Arbint & cpy);
                        
                        virtual ~Arbint() {}
                        Arbint(const Arbint & cpy);
                        
-                       int64_t AsDigit() const 
+                       int64_t AsDigit() const
                        {
                                int64_t digit = (m_digits.size() == 1) ? m_digits[0] : 0x7FFFFFFFFFFFFFFF;
                                return (m_sign) ? -digit : digit;
                        {
                                int64_t digit = (m_digits.size() == 1) ? m_digits[0] : 0x7FFFFFFFFFFFFFFF;
                                return (m_sign) ? -digit : digit;
@@ -60,6 +60,13 @@ namespace IPDF
                                a -= add;
                                return a;
                        }
                                a -= add;
                                return a;
                        }
+
+                       inline Arbint operator-()
+                       {
+                               Arbint a(*this);
+                               a.m_sign = !a.m_sign;
+                               return a;
+                       }
                        
                        inline Arbint operator*(const Arbint & mul) const
                        {
                        
                        inline Arbint operator*(const Arbint & mul) const
                        {
@@ -124,7 +131,17 @@ namespace IPDF
                        }
                        bool IsZero() const;
                        
                        }
                        bool IsZero() const;
                        
-                       //inline operator double() const {return double(AsDigit());}
+                       inline operator double() const 
+                       {
+                               double acc = 0;
+                               for(int i = m_digits.size()-1; i >= 0; --i)
+                               {
+                                       acc += (double)m_digits[i];
+                                       acc *= (double)UINT64_MAX + 1.0;
+                               }
+                               if (m_sign) acc *= -1;
+                               return acc;
+                       }
                        inline operator int64_t() const {return AsDigit();}
                        //inline operator int() const {return int(AsDigit());}
                        
                        inline operator int64_t() const {return AsDigit();}
                        //inline operator int() const {return int(AsDigit());}
                        
index 53f7d02..2c98e1a 100644 (file)
@@ -15,9 +15,10 @@ namespace IPDF
        
 template <class T> T Tabs(const T & a)
 {
        
 template <class T> T Tabs(const T & a)
 {
-       return llabs(a);
+       return abs(a);
 }
 
 }
 
+template <> Arbint Tabs(const Arbint & a);
 
 
 /* Recursive version  of GCD
 
 
 /* Recursive version  of GCD
@@ -97,6 +98,12 @@ struct Rational
                        Q = T(1);
                        return;
                }
                        Q = T(1);
                        return;
                }
+               if (P == Q)
+               {
+                       P = Q = T(1);
+                       return;
+               }
+               
                T g = gcd(Tabs(P), Tabs(Q));
                //Debug("Got gcd!");
                P /= g;
                T g = gcd(Tabs(P), Tabs(Q));
                //Debug("Got gcd!");
                P /= g;
@@ -156,7 +163,7 @@ struct Rational
        //Rational operator*(const Rational & r) const {return Rational(ToDouble()*r.ToDouble());}
        //Rational operator/(const Rational & r) const {return Rational(ToDouble()/r.ToDouble());}
 
        //Rational operator*(const Rational & r) const {return Rational(ToDouble()*r.ToDouble());}
        //Rational operator/(const Rational & r) const {return Rational(ToDouble()/r.ToDouble());}
 
-       Rational & operator=(const Rational & r) {P = r.P; Q = r.Q; return *this;}
+       Rational & operator=(const Rational & r) {P = r.P; Q = r.Q; Simplify(); return *this;}
        Rational & operator+=(const Rational & r) {this->operator=(*this+r); return *this;}
        Rational & operator-=(const Rational & r) {this->operator=(*this-r); return *this;}
        Rational & operator*=(const Rational & r) {this->operator=(*this*r); return *this;}
        Rational & operator+=(const Rational & r) {this->operator=(*this+r); return *this;}
        Rational & operator-=(const Rational & r) {this->operator=(*this-r); return *this;}
        Rational & operator*=(const Rational & r) {this->operator=(*this*r); return *this;}

UCC git Repository :: git.ucc.asn.au