From 924cb525f587c58440c43bcae6ccfd9b2e41c99c Mon Sep 17 00:00:00 2001 From: David Gow Date: Sun, 6 Jul 2014 21:51:10 +0800 Subject: [PATCH] Rational now passes realops.test --- src/arbint.cpp | 5 +++++ src/arbint.h | 21 +++++++++++++++++++-- src/rational.h | 11 +++++++++-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/arbint.cpp b/src/arbint.cpp index 4bde578..ac39942 100644 --- a/src/arbint.cpp +++ b/src/arbint.cpp @@ -114,6 +114,11 @@ void Arbint::Division(const Arbint & div, Arbint & result, Arbint & remainder) c { 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; diff --git a/src/arbint.h b/src/arbint.h index 0f047c9..06008e0 100644 --- a/src/arbint.h +++ b/src/arbint.h @@ -23,7 +23,7 @@ namespace IPDF 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; @@ -60,6 +60,13 @@ namespace IPDF 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 { @@ -124,7 +131,17 @@ namespace IPDF } 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());} diff --git a/src/rational.h b/src/rational.h index 53f7d02..2c98e1a 100644 --- a/src/rational.h +++ b/src/rational.h @@ -15,9 +15,10 @@ namespace IPDF template T Tabs(const T & a) { - return llabs(a); + return abs(a); } +template <> Arbint Tabs(const Arbint & a); /* Recursive version of GCD @@ -97,6 +98,12 @@ struct Rational Q = T(1); return; } + if (P == Q) + { + P = Q = T(1); + return; + } + 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) {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;} -- 2.20.1