bool isCurious(Rational r) { r = r.abs(); unsigned a, b, x, y; // ab/xy a = r.n() / 10; b = r.n() % 10; x = r.d() / 10; y = r.d() % 10; if((a == b && x == y)) { return false; } bool curious = false; if(x) curious = curious || (r == Rational(b, x) && a == y); if(y) curious = curious || (r == Rational(a, y) && b == x); if(curious) r.print(); return curious; }
void Rational::integralityViolation(Rational& violation) const { // if denominator is 1, then there is no integrality violation for sure if( mpz_cmp_ui(mpq_denref(number), 1) == 0 ) { violation.toZero(); return; } // otherwise, we must check w.r.t. the given tolerance // first calculate the fractional part violation = (*this); violation.abs(); mpz_t r; mpz_init(r); mpz_fdiv_r(r, mpq_numref(violation.number), mpq_denref(violation.number)); mpq_set_num(violation.number, r); mpz_clear(r); // then integrality violation if( violation > Rational(1, 2) ) sub(violation, Rational(1,1), violation); }
Rational abs(const Rational& r) { return r.abs(); }