52#include <lal/numeric/integer.hpp>
74 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
103 m_val[0]._mp_num = *i.m_val;
105 mpz_init_set_ui(&
m_val[0]._mp_den, 1);
107 mpq_canonicalize(
m_val);
110 i.m_val->_mp_alloc = 0;
111 i.m_val->_mp_size = 0;
112 i.m_val->_mp_d =
nullptr;
113 i.m_initialized =
false;
123 m_val[0]._mp_num = *n.m_val;
125 m_val[0]._mp_den = *d.m_val;
127 mpq_canonicalize(
m_val);
130 n.m_val->_mp_alloc = 0;
131 n.m_val->_mp_size = 0;
132 n.m_val->_mp_d =
nullptr;
133 n.m_initialized =
false;
136 d.m_val->_mp_alloc = 0;
137 d.m_val->_mp_size = 0;
138 d.m_val->_mp_d =
nullptr;
139 d.m_initialized =
false;
150 r.m_val->_mp_num._mp_alloc = 0;
151 r.m_val->_mp_num._mp_size = 0;
152 r.m_val->_mp_num._mp_d =
nullptr;
153 r.m_val->_mp_den._mp_alloc = 0;
154 r.m_val->_mp_den._mp_size = 0;
155 r.m_val->_mp_den._mp_d =
nullptr;
156 r.m_initialized =
false;
169 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
172 if constexpr (std::is_signed_v<T>) { mpq_set_si(
m_val, n, d); }
173 else { mpq_set_ui(
m_val, n, d); }
174 mpq_canonicalize(
m_val);
183 mpq_set_str(
m_val, s.c_str(), 10);
184 mpq_canonicalize(
m_val);
194 mpq_set_num(
m_val, n.get_raw_value());
195 mpq_set_den(
m_val, d.get_raw_value());
196 mpq_canonicalize(
m_val);
205 mpq_set(
m_val, r.m_val);
224 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
257 m_val[0]._mp_num = *i.m_val;
259 mpz_init_set_ui(&
m_val[0]._mp_den, 1);
261 mpq_canonicalize(
m_val);
264 i.m_val->_mp_alloc = 0;
265 i.m_val->_mp_size = 0;
266 i.m_val->_mp_d =
nullptr;
267 i.m_initialized =
false;
285 r.m_val->_mp_num._mp_alloc = 0;
286 r.m_val->_mp_num._mp_size = 0;
287 r.m_val->_mp_num._mp_d =
nullptr;
288 r.m_val->_mp_den._mp_alloc = 0;
289 r.m_val->_mp_den._mp_size = 0;
290 r.m_val->_mp_den._mp_d =
nullptr;
291 r.m_initialized =
false;
303 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
306 (std::is_signed_v<T> ? mpq_cmp_si(
m_val, i, 1) : mpq_cmp_ui(
m_val, i, 1)) == 0;
314 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
339 return mpq_equal(
m_val, r.m_val);
348 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
350 {
return not (*
this == i); }
365 {
return not (*
this == i); }
380 {
return not (*
this == r); }
388 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
391 (std::is_signed_v<T> ? mpq_cmp_si(
m_val, i, 1) : mpq_cmp_ui(
m_val, i, 1)) < 0;
399 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
423 {
return mpq_cmp(
m_val, r.m_val) < 0; }
431 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
434 (std::is_signed_v<T> ? mpq_cmp_si(
m_val, i, 1) : mpq_cmp_ui(
m_val, i, 1)) <= 0;
442 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
466 {
return mpq_cmp(
m_val, r.m_val) <= 0; }
474 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
477 (std::is_signed_v<T> ? mpq_cmp_si(
m_val, i, 1) : mpq_cmp_ui(
m_val, i, 1)) > 0;
485 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
509 {
return mpq_cmp(
m_val, r.m_val) > 0; }
517 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
520 (std::is_signed_v<T> ? mpq_cmp_si(
m_val, i, 1) : mpq_cmp_ui(
m_val, i, 1)) >= 0;
528 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
552 {
return mpq_cmp(
m_val, r.m_val) >= 0; }
560 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
562 {
rational r(*
this); r += i;
return r; }
569 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
578 {
rational r(*
this); r += i;
return r; }
593 {
rational k(*
this); k += r;
return k; }
599 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
613 { mpq_add(
m_val,
m_val, r.m_val);
return *
this; }
624 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
626 {
rational r(*
this); r -= i;
return r; }
633 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
642 {
rational r(*
this); r -= i;
return r; }
657 {
rational k(*
this); k -= r;
return k; }
663 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
677 { mpq_sub(
m_val,
m_val, r.m_val);
return *
this; }
685 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
687 {
rational r(*
this); r *= i;
return r; }
694 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
703 {
rational r(*
this); r *= i;
return r; }
718 {
rational k(*
this); k *= r;
return k; }
724 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
738 { mpq_mul(
m_val,
m_val, r.m_val);
return *
this; }
746 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
748 {
rational r(*
this); r /= i;
return r; }
755 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
765 {
rational r(*
this); r /= i;
return r; }
771 {
rational k(*
this); k /= r;
return k; }
777 template <
typename T,std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
779 integer I(i); *
this /= I;
return *
this;
863 buf = mpq_get_str(buf, 10,
m_val);
864 s = std::string(buf);
872 mpq_get_num(numerator,
m_val);
873 return integer(std::move(numerator));
879 mpz_init(denominator);
880 mpq_get_den(denominator,
m_val);
881 return integer(std::move(denominator));
Arbitrary precision integer.
Definition: integer.hpp:60
Exact rational number.
Definition: rational.hpp:63
rational(integer &&n, integer &&d) noexcept
Move constructor with numerator and denominator.
Definition: rational.hpp:121
rational power(const integer &i) const noexcept
Exponentiation operator.
void swap(rational &r) noexcept
Swaps the value of this rational with rational r's value.
Definition: rational.hpp:895
void set_str(const std::string &s) noexcept
Overwrites the value in the string s.
Definition: rational.hpp:181
bool is_initialized() const noexcept
Returns whether this object is initialised or not.
Definition: rational.hpp:819
std::string to_string() const noexcept
Converts this integer to a string.
Definition: rational.hpp:855
rational & operator+=(T i) noexcept
Addition operator.
Definition: rational.hpp:600
void set_integer(const integer &n, const integer &d) noexcept
Overwrites the value of this rational with the value .
Definition: rational.hpp:192
friend bool operator==(T i, const rational &r) noexcept
Equality operator.
Definition: rational.hpp:315
rational(integer &&i) noexcept
Move constructor.
Definition: rational.hpp:101
void as_double(double &d) const noexcept
Converts this rational to a double-precision floating-point value.
Definition: rational.hpp:852
friend bool operator<(T i, const rational &r) noexcept
Less than operator.
Definition: rational.hpp:400
rational(const std::string &s) noexcept
Constructor with string.
Definition: rational.hpp:88
void as_string(std::string &s) const noexcept
Converts this integer to a string.
Definition: rational.hpp:861
rational & operator-=(T i) noexcept
Substraction operator.
Definition: rational.hpp:664
rational(const rational &r) noexcept
Copy constructor.
Definition: rational.hpp:94
rational & powt(const integer &i) noexcept
Exponentiation operator.
rational operator-() const noexcept
Substraction unary operator.
Definition: rational.hpp:618
rational() noexcept
Empty constructor.
Definition: rational.hpp:68
void as_integer(integer &i) const noexcept
Converts this rational to an integer value.
void set_rational(const rational &r) noexcept
Overwrites the value of this rational with the value .
Definition: rational.hpp:203
rational & operator/=(T i) noexcept
Division operator.
Definition: rational.hpp:778
rational(T n, uint64_t d=1) noexcept
Constructor with numerator and denominator.
Definition: rational.hpp:75
friend bool operator!=(int64_t i, const rational &r) noexcept
Non-equality operator.
Definition: rational.hpp:357
rational & operator*=(T i) noexcept
Multiplication operator.
Definition: rational.hpp:725
friend rational operator*(T i, const rational &r) noexcept
Multiplication operator.
Definition: rational.hpp:695
friend bool operator>=(T i, const rational &r) noexcept
Greater than or equal to operator.
Definition: rational.hpp:529
integer get_denominator() const noexcept
Returns the denominator of this rational number.
Definition: rational.hpp:877
rational power(uint64_t i) const noexcept
Exponentiation operator.
friend rational operator/(T i, const rational &r) noexcept
Division operator.
Definition: rational.hpp:756
bool m_initialized
Is this rational initialised?
Definition: rational.hpp:910
friend rational operator+(T i, const rational &r) noexcept
Addition operator.
Definition: rational.hpp:570
integer to_integer() const noexcept
Converts this rational to an integer value.
Definition: rational.hpp:835
rational(rational &&r) noexcept
Move constructor.
Definition: rational.hpp:146
rational & powt(uint64_t i) noexcept
Exponentiation operator.
void invert() noexcept
Changes numerator and denominator.
Definition: rational.hpp:214
friend void swap(rational &r1, rational &r2) noexcept
Swaps two rationals.
Definition: rational.hpp:903
std::size_t bytes() const noexcept
Returns the amount of bytes this integer occupies.
double to_double() const noexcept
Converts this rational to a double-precision floating-point value.
Definition: rational.hpp:850
void set_number(T n, uint64_t d=1) noexcept
Overwrites the value of this rational with .
Definition: rational.hpp:170
rational(const integer &n, const integer &d=1) noexcept
Constructor with numerator and denominator.
Definition: rational.hpp:82
integer get_numerator() const noexcept
Returns the numerator of this rational number.
Definition: rational.hpp:869
friend bool operator>(T i, const rational &r) noexcept
Greater than operator.
Definition: rational.hpp:486
int64_t get_sign() const noexcept
Returns the sign of this rational.
Definition: rational.hpp:821
~rational() noexcept
Destructor.
Definition: rational.hpp:160
rational & operator=(T i) noexcept
Assignment operator.
Definition: rational.hpp:225
friend bool operator<=(T i, const rational &r) noexcept
Less than or equal to operator.
Definition: rational.hpp:443
mpq_t m_val
Structure from GMP storing the rational's value.
Definition: rational.hpp:908
Main namespace of the library.
Definition: basic_types.hpp:50