13 void canonicalize() { mpq_canonicalize(value); }
14 int get_sign() {
return mpq_sgn(value); }
21 void init_from_bin(
const std::string& bin) { mpq_set_str(value, bin.c_str(), 2); }
46 mpq_set(value, other.value);
55 mpq_add(r_out.value, x.value, y.value);
62 mpq_sub(r_out.value, x.value, y.value);
70 mpq_neg(r_out.value, x.value);
77 for (
int i = 1; i < std::abs(p); i++) {
80 if (p < 0)
return 1 / r_out;
87 mpq_mul(r_out.value, x.value, y.value);
94 mpq_div(r_out.value, x.value, y.value);
100 if (
this == &x)
return *
this;
101 mpq_set(value, x.value);
115 return mpq_cmp(r.value, r1.value) < 0;
120 return mpq_cmp(r.value, r1.value) > 0;
125 return mpq_cmp(r.value, r1.value) <= 0;
130 return mpq_cmp(r.value, r1.value) >= 0;
135 return mpq_equal(r.value, r1.value);
140 return !mpq_equal(r.value, r1.value);
144 double to_double()
const {
return mpq_get_d(value); }
145 explicit operator double()
const {
return to_double(); }
148 std::string get_str()
150 char* s = mpq_get_str(NULL, 10, value);
157 std::string get_num_str()
const
161 mpq_get_num(num, value);
162 char* s = mpz_get_str(NULL, 10, num);
170 std::string get_den_str()
const
174 mpq_get_den(den, value);
175 char* s = mpz_get_str(NULL, 10, den);
185 mpq_abs(r.value, r0.value);
190 friend std::ostream& operator<<(std::ostream& os,
const Rational& r)
192 os << mpq_get_d(r.value);