From: Sam Moore Date: Mon, 7 Jul 2014 08:46:12 +0000 (+0800) Subject: Change Rational -> Rational X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=commitdiff_plain;h=e8e6ff92ef0978cbec24dd69a85dbf1bd81681ad;hp=8c88a35a1fb0dfc9e542bb1a1c13c5f3e36eb3e5 Change Rational -> Rational Works better (not surprising). Can change back @real.h:52 --- diff --git a/src/gmpint.h b/src/gmpint.h index 11b53c5..4f45231 100644 --- a/src/gmpint.h +++ b/src/gmpint.h @@ -18,9 +18,9 @@ class Gmpint virtual ~Gmpint() {} //TODO: Do we need to delete m_op somehow? - operator int64_t() {return mpz_get_si(m_op);} - operator uint64_t() {return mpz_get_ui(m_op);} - operator double() {return mpz_get_d(m_op);} + operator int64_t() const {return mpz_get_si(m_op);} + operator uint64_t() const {return mpz_get_ui(m_op);} + operator double() const {return mpz_get_d(m_op);} std::string Str(int base = 10) const { //TODO: Make less hacky, if we care. @@ -40,8 +40,7 @@ class Gmpint Gmpint operator-(const Gmpint & sub) const {Gmpint a(*this); a -= sub; return a;} Gmpint operator*(const Gmpint & mul) const {Gmpint a(*this); a *= mul; return a;} Gmpint operator/(const Gmpint & div) const {Gmpint a(*this); a /= div; return a;} - Gmpint operator%(const Gmpint & div) const - {Gmpint a(*this); mpz_mod(a.m_op, a.m_op, div.m_op); return a;} + Gmpint operator%(const Gmpint & div) const {Gmpint a(*this); mpz_mod(a.m_op, a.m_op, div.m_op); return a;} Gmpint operator-() const {return (Gmpint(0L)-*this);} diff --git a/src/main.cpp b/src/main.cpp index 79d8208..91e362c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -97,7 +97,7 @@ int main(int argc, char ** argv) //doc.Add(BEZIER, Rect(0.2+x-4.0, 0.2+y-4.0, 0.6,0.6), (x^y)%3); } } - doc.Add(CIRCLE_FILLED, Rect(0.1,0.1,0.8,0.8), 0); + doc.Add(BEZIER, Rect(0.1,0.1,0.8,0.8), 0); } Debug("Start!"); Rect bounds(b[0],b[1],b[2],b[3]); diff --git a/src/rational.h b/src/rational.h index 61a38ab..6bc5aec 100644 --- a/src/rational.h +++ b/src/rational.h @@ -9,6 +9,7 @@ #include #include #include "arbint.h" +#include "gmpint.h" namespace IPDF { @@ -18,7 +19,7 @@ template T Tabs(const T & a) return abs(a); } template <> Arbint Tabs(const Arbint & a); - +template <> Gmpint Tabs(const Gmpint & a); /* Recursive version of GCD template diff --git a/src/real.cpp b/src/real.cpp index 83b3fb8..ce2f976 100644 --- a/src/real.cpp +++ b/src/real.cpp @@ -11,5 +11,10 @@ namespace IPDF "Rational", "Rational" }; + + template <> Gmpint Tabs(const Gmpint & a) + { + return a.Abs(); + } } diff --git a/src/real.h b/src/real.h index 8dea953..798ea0b 100644 --- a/src/real.h +++ b/src/real.h @@ -26,6 +26,7 @@ #if REAL == REAL_RATIONAL_ARBINT #include "rational.h" #include "arbint.h" + #include "gmpint.h" #endif //REAL namespace IPDF @@ -48,14 +49,16 @@ namespace IPDF inline float Float(const Real & r) {return (float)r.ToDouble();} inline double Double(const Real & r) {return r.ToDouble();} #elif REAL == REAL_RATIONAL_ARBINT - typedef Rational Real; + #define ARBINT Gmpint // Set to Gmpint or Arbint here + + typedef Rational Real; inline float Float(const Real & r) {return (float)r.ToDouble();} inline double Double(const Real & r) {return r.ToDouble();} - inline Rational pow(const Rational & a, const Rational & b) + inline Rational pow(const Rational & a, const Rational & b) { - Arbint P(std::pow(static_cast(a.P), b.ToDouble())); - Arbint Q(std::pow(static_cast(a.Q), b.ToDouble())); - return Rational(P,Q); + ARBINT P(std::pow(static_cast(a.P), b.ToDouble())); + ARBINT Q(std::pow(static_cast(a.Q), b.ToDouble())); + return Rational(P,Q); } #else #error "Type of Real unspecified." diff --git a/src/tests/arbint_vs_gmpint.cpp b/src/tests/arbint_vs_gmpint.cpp index 2393d3a..306bbb7 100644 --- a/src/tests/arbint_vs_gmpint.cpp +++ b/src/tests/arbint_vs_gmpint.cpp @@ -26,9 +26,12 @@ int main(int argc, char ** argv) { arb_a *= b; gmp_a *= b; - - Debug("Arbint - %s", arb_a.Str().c_str()); - Debug("Gmpint - %s", gmp_a.Str().c_str()); + } + + for (unsigned j = 0; j < 5; ++j) + { + arb_a /= b; + gmp_a /= b; }