12 mpq_canonicalize(
value);
19 return mpq_sgn(
value);
31 : m_is_rounded(rounded)
39 d_value = std::numeric_limits<double>::lowest();
51 d_value = std::numeric_limits<double>::lowest();
55 : d_value(other.d_value)
56 , m_is_rounded(other.m_is_rounded)
64 : d_value(other.d_value)
65 , m_is_rounded(rounded)
75 d_value = std::numeric_limits<double>::lowest();
83 : m_is_rounded(rounded)
88 mpq_set_str(tmp_r, data.c_str(), 10);
94 mpq_set_str(
value, data.c_str(), 10);
95 d_value = std::numeric_limits<double>::lowest();
171 for (
int i = 1; i <
std::abs(p); i++) {
174 if (p < 0)
return Rational(1.0,
false) / r_out;
232 if (
this == &x)
return *
this;
253 d_value = std::numeric_limits<double>::lowest();
268 int res = mpq_cmp(tmp, r1.
value);
277 int res = mpq_cmp(r.
value, tmp);
294 bool res = mpq_equal(tmp, r1.
value);
303 bool res = mpq_equal(r.
value, tmp);
320 return mpq_get_d(
value);
323 Rational::operator double()
const
348 mpq_set_str(
value, v.c_str(), 2);
357 std::string v(mpq_get_str(NULL, 2, tmp));
362 std::string v(mpq_get_str(NULL, 2,
value));
373 std::stringstream numss;
374 std::stringstream denomss;
376 for (int64_t i = 0; i < data.size(); ++i) {
377 if (data[i] ==
'/') {
384 else if (counter == 1)
387 assert(data[i] ==
'0' || data[i] ==
'1');
393 const auto num = numss.str();
394 const auto denom = denomss.str();
412 std::string tmp = num +
"/" + denom;
413 mpq_set_str(tmp_r, tmp.c_str(), 10);
420 std::string tmp = num +
"/" + denom;
421 mpq_set_str(
value, tmp.c_str(), 10);
422 d_value = std::numeric_limits<double>::lowest();
440 mpq_get_num(num, tmp);
441 std::string v(mpz_get_str(NULL, 10, num));
460 mpq_get_den(denom, tmp);
462 std::string v(mpz_get_str(NULL, 10, denom));
Rational(bool rounded=false)
std::string numerator() const
Rational & operator=(const Rational &x)
std::string serialize() const
void init_from_binary(const std::string &v)
std::string denominator() const
std::string to_binary() const
int cmp(const Rational &r, const Rational &r1)
constexpr bool operator!=(PrimitiveType a, PrimitiveType b)
constexpr bool operator==(PrimitiveType a, PrimitiveType b)
Rational abs(const Rational &r0)
constexpr PrimitiveType operator-(PrimitiveType pt, int8_t n)
Rational operator/(const Rational &x, const Rational &y)
Rational operator*(const Rational &x, const Rational &y)
Rational pow(const Rational &x, int p)
constexpr PrimitiveType operator+(PrimitiveType pt, int8_t n)
std::ostream & operator<<(std::ostream &os, const Rational &r)