52#include <lal/numeric/integer.hpp>
74 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
75 rational(
const T n,
const uint64_t d = 1) noexcept
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;
302 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;
308#if !defined __LAL_SWIG_PYTHON
314 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
324#if !defined __LAL_SWIG_PYTHON
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); }
351#if !defined __LAL_SWIG_PYTHON
365 {
return not (*
this == i); }
366#if !defined __LAL_SWIG_PYTHON
380 {
return not (*
this == r); }
388 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
392 (std::is_signed_v<T> ? mpq_cmp_si(
m_val, i, 1) : mpq_cmp_ui(
m_val, i, 1)) < 0;
394#if !defined __LAL_SWIG_PYTHON
400 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
410#if !defined __LAL_SWIG_PYTHON
424 {
return mpq_cmp(
m_val, r.m_val) < 0; }
432 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
435 (std::is_signed_v<T> ? mpq_cmp_si(
m_val, i, 1) : mpq_cmp_ui(
m_val, i, 1)) <= 0;
437#if !defined __LAL_SWIG_PYTHON
443 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
453#if !defined __LAL_SWIG_PYTHON
467 {
return mpq_cmp(
m_val, r.m_val) <= 0; }
475 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
479 (std::is_signed_v<T> ? mpq_cmp_si(
m_val, i, 1) : mpq_cmp_ui(
m_val, i, 1)) > 0;
481#if !defined __LAL_SWIG_PYTHON
487 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
497#if !defined __LAL_SWIG_PYTHON
511 {
return mpq_cmp(
m_val, r.m_val) > 0; }
519 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
522 (std::is_signed_v<T> ? mpq_cmp_si(
m_val, i, 1) : mpq_cmp_ui(
m_val, i, 1)) >= 0;
524#if !defined __LAL_SWIG_PYTHON
530 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
540#if !defined __LAL_SWIG_PYTHON
554 {
return mpq_cmp(
m_val, r.m_val) >= 0; }
562 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
564 {
rational r(*
this); r += i;
return r; }
565#if !defined __LAL_SWIG_PYTHON
571 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
580 {
rational r(*
this); r += i;
return r; }
581#if !defined __LAL_SWIG_PYTHON
596 {
rational k(*
this); k += r;
return k; }
602 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
616 { mpq_add(
m_val,
m_val, r.m_val);
return *
this; }
627 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
629 {
rational r(*
this); r -= i;
return r; }
630#if !defined __LAL_SWIG_PYTHON
636 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
645 {
rational r(*
this); r -= i;
return r; }
646#if !defined __LAL_SWIG_PYTHON
660 {
rational k(*
this); k -= r;
return k; }
666 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
680 { mpq_sub(
m_val,
m_val, r.m_val);
return *
this; }
688 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
690 {
rational r(*
this); r *= i;
return r; }
691#if !defined __LAL_SWIG_PYTHON
697 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
706 {
rational r(*
this); r *= i;
return r; }
707#if !defined __LAL_SWIG_PYTHON
721 {
rational k(*
this); k *= r;
return k; }
727 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
741 { mpq_mul(
m_val,
m_val, r.m_val);
return *
this; }
749 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
751 {
rational r(*
this); r /= i;
return r; }
752#if !defined __LAL_SWIG_PYTHON
758 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
768 {
rational r(*
this); r /= i;
return r; }
774 {
rational k(*
this); k /= r;
return k; }
780 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
782 integer I(i); *
this /= I;
return *
this;
827 [[nodiscard]] std::size_t
bytes() const noexcept;
866 buf = mpq_get_str(buf, 10,
m_val);
867 s = std::string(buf);
875 mpq_get_num(numerator,
m_val);
876 return integer(std::move(numerator));
882 mpz_init(denominator);
883 mpq_get_den(denominator,
m_val);
884 return integer(std::move(denominator));
900#if !defined __LAL_SWIG_PYTHON
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:898
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 initialized or not.
Definition rational.hpp:822
std::string to_string() const noexcept
Converts this integer to a string.
Definition rational.hpp:858
rational & operator/=(const T i) noexcept
Division operator.
Definition rational.hpp:781
void set_integer(const integer &n, const integer &d) noexcept
Overwrites the value of this rational with the value .
Definition rational.hpp:192
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:855
friend rational operator/(const T i, const rational &r) noexcept
Division operator.
Definition rational.hpp:759
rational(const std::string &s) noexcept
Constructor with string.
Definition rational.hpp:88
friend rational operator+(const T i, const rational &r) noexcept
Addition operator.
Definition rational.hpp:572
void as_string(std::string &s) const noexcept
Converts this integer to a string.
Definition rational.hpp:864
rational(const rational &r) noexcept
Copy constructor.
Definition rational.hpp:94
friend bool operator<=(const T i, const rational &r) noexcept
Less than or equal to operator.
Definition rational.hpp:444
rational & powt(const uint64_t i) noexcept
Exponentiation operator.
rational & powt(const integer &i) noexcept
Exponentiation operator.
rational operator-() const noexcept
Substraction unary operator.
Definition rational.hpp:621
rational() noexcept
Empty constructor.
Definition rational.hpp:68
rational(const T n, const uint64_t d=1) noexcept
Constructor with numerator and denominator.
Definition rational.hpp:75
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
friend bool operator>=(const T i, const rational &r) noexcept
Greater than or equal to operator.
Definition rational.hpp:531
rational & operator*=(const T i) noexcept
Multiplication operator.
Definition rational.hpp:728
friend rational operator*(const T i, const rational &r) noexcept
Multiplication operator.
Definition rational.hpp:698
friend bool operator!=(int64_t i, const rational &r) noexcept
Non-equality operator.
Definition rational.hpp:357
rational & operator=(const T i) noexcept
Assignment operator.
Definition rational.hpp:225
integer get_denominator() const noexcept
Returns the denominator of this rational number.
Definition rational.hpp:880
rational & operator+=(const T i) noexcept
Addition operator.
Definition rational.hpp:603
bool m_initialized
Is this rational initialized?
Definition rational.hpp:913
integer to_integer() const noexcept
Converts this rational to an integer value.
Definition rational.hpp:838
rational(rational &&r) noexcept
Move constructor.
Definition rational.hpp:146
friend bool operator<(const T i, const rational &r) noexcept
Less than operator.
Definition rational.hpp:401
friend bool operator>(const T i, const rational &r) noexcept
Greater than operator.
Definition rational.hpp:488
rational power(const uint64_t i) const 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:906
void set_number(const T n, const uint64_t d=1) noexcept
Overwrites the value of this rational with .
Definition rational.hpp:170
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:853
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:872
friend bool operator==(const T i, const rational &r) noexcept
Equality operator.
Definition rational.hpp:315
rational & operator-=(const T i) noexcept
Substraction operator.
Definition rational.hpp:667
int64_t get_sign() const noexcept
Returns the sign of this rational.
Definition rational.hpp:824
~rational() noexcept
Destructor.
Definition rational.hpp:160
mpq_t m_val
Structure from GMP storing the rational's value.
Definition rational.hpp:911
Main namespace of the library.
Definition basic_types.hpp:48