Ok I need to put more thought into this. Or just look it up.
void Arbint::Division(const Arbint & div, Arbint & result, Arbint & remainder) const
{
- //TODO: Optimise?
+ if (div == Arbint(1L))
+ {
+ result = *this;
+ remainder = 0L;
+ return;
+ }
+ if (div == *this)
+ {
+ result = 1L;
+ remainder = 0L;
+ return;
+ }
+
+
+ result = 0L;
remainder = *this;
- result = 0L;
- while ((remainder -= div) > Arbint(0L))
+ Arbint next_rem(remainder);
+ while ((next_rem -= div) >= Arbint(0L))
{
- //Debug("Remainder %c%s", remainder.SignChar(), remainder.DigitStr().c_str());
- //Debug("Result %c%s + 1", result.SignChar(), result.DigitStr().c_str());
- result += 1;
+ //Debug("%li - %li = %li", digit_t(remainder), digit_t(div), digit_t(next_rem));
+ //Debug("Sign is %d", next_rem.m_sign);
+ remainder = next_rem;
+ result += 1L;
}
- remainder += div;
+
+
}
Arbint & Arbint::operator+=(const Arbint & add)
bool Arbint::operator==(const Arbint & equ) const
{
- if (m_sign != equ.m_sign) return false;
+ if (m_sign != equ.m_sign)
+ return false;
unsigned min_size = m_digits.size();
const Arbint * larger = &equ;
if (m_digits.size() > equ.m_digits.size())
inline bool operator>=(const Arbint & leq) const
{
- return (this->operator==(leq) || this->operator<(leq));
+ return (this->operator==(leq) || this->operator>(leq));
}
inline bool operator>(const Arbint & grea) const
{
template <class T>
T gcd(const T & a, const T & b)
{
- if (a == 1 || a == 0) return 1;
- if (b == 0) return a;
- if (b == a) return a;
+ Debug("Called on %li/%li", int64_t(a), int64_t(b));
+ if (a == T(1) || a == T(0)) return T(1);
+ if (b == T(0)) return a;
+ if (b == a)
+ {
+ Debug("Equal!");
+ return a;
+ }
+ Debug("Not equal!");
if (a > b) return gcd(a-b,b);
return gcd(a, b-a);
template <class T>
T gcd(const T & p, const T & q)
{
+
+
T g(1);
T big(p);
T small(q);
return g;
while ((g = big % small) > T(0))
{
+ //Debug("big = %li, small = %li", int64_t(big), int64_t(small));
big = small;
small = g;
+ //Debug("Loop %u", ++count);
}
return small;
-}
+}
+
template <class T = int64_t>
struct Rational
return;
}
T g = gcd(T(llabs(P)),T(llabs(Q)));
+ Debug("Got gcd!");
P /= g;
Q /= g;
}
assert((arb_a/arb_b).AsDigit() == a / b);
assert((arb_a%arb_b).AsDigit() == a % b);
}
- printf("All tests successful!\n");
+ printf("All single digit tests successful!\n");
+
return 0;
}
--- /dev/null
+/**
+ * @file arbrationals.cpp
+ * @brief Tests Rational<Arbint>
+ */
+
+#include "rational.h"
+#include "arbint.h"
+
+#define TEST_CASES 100
+
+using namespace std;
+using namespace IPDF;
+
+int main(int argc, char ** argv)
+{
+ for (unsigned i = 0; i < TEST_CASES; ++i)
+ {
+ Debug("Cycle %u",i);
+ Arbint p1 = 1L;//rand();
+ Arbint q1 = 2L;//rand();
+ Arbint p2 = rand();
+ Arbint q2 = rand();
+ Debug("Construct rationals");
+ Rational<Arbint> a(p1, q1);
+ Debug("Constructed a %u\n",i);
+ Rational<Arbint> b(p2, q2);
+ Debug("Constructed b %u\n",i);
+
+
+ }
+ printf("Tests done");
+}