88 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
90 if constexpr (std::is_signed_v<T>) { mpz_init_set_si(
m_val, i); }
91 else { mpz_init_set_ui(
m_val, i); }
97 integer(
const std::string& s)
noexcept { mpz_init_set_str(
m_val, s.c_str(), 10); }
109 i.m_val->_mp_alloc = 0;
110 i.m_val->_mp_size = 0;
111 i.m_val->_mp_d =
nullptr;
112 i.m_initialized =
false;
129 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
132 if constexpr (std::is_signed_v<T>) { mpz_set_si(
m_val, i); }
133 else { mpz_set_ui(
m_val, i); }
143 mpz_set(
m_val, i.m_val);
152 mpz_set_str(
m_val, s.c_str(), 10);
164 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
191 i.m_val->_mp_alloc = 0;
192 i.m_val->_mp_size = 0;
193 i.m_val->_mp_d =
nullptr;
194 i.m_initialized =
false;
205 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
208 (std::is_signed_v<T> ? mpz_cmp_si(
m_val,i) : mpz_cmp_ui(
m_val,i)) == 0;
210#if !defined __LAL_SWIG_PYTHON
216 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
225 {
return mpz_cmp(
m_val, i.m_val) == 0; }
233 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
235 {
return not (*
this == i); }
236#if !defined __LAL_SWIG_PYTHON
242 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
251 {
return not (*
this == i); }
259 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
260 [[nodiscard]]
bool operator< (
const T i)
const noexcept {
262 (std::is_signed_v<T> ? mpz_cmp_si(
m_val, i) : mpz_cmp_ui(
m_val, i)) < 0;
264#if !defined __LAL_SWIG_PYTHON
270 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
279 {
return mpz_cmp(
m_val, i.m_val) < 0; }
287 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
290 (std::is_signed_v<T> ? mpz_cmp_si(
m_val, i) : mpz_cmp_ui(
m_val, i)) <= 0;
292#if !defined __LAL_SWIG_PYTHON
298 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
307 {
return mpz_cmp(
m_val, i.m_val) <= 0; }
315 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
316 [[nodiscard]]
bool operator> (
const T i)
const noexcept {
318 (std::is_signed_v<T> ? mpz_cmp_si(
m_val, i) : mpz_cmp_ui(
m_val, i)) > 0;
320#if !defined __LAL_SWIG_PYTHON
326 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
335 {
return mpz_cmp(
m_val, i.m_val) > 0; }
343 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
346 (std::is_signed_v<T> ? mpz_cmp_si(
m_val, i) : mpz_cmp_ui(
m_val, i)) >= 0;
348#if !defined __LAL_SWIG_PYTHON
354 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
363 {
return mpz_cmp(
m_val, i.m_val) >= 0; }
373 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
375 {
integer a(*
this); a += i;
return a; }
376#if !defined __LAL_SWIG_PYTHON
382 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
391 {
integer a(*
this); a += i;
return a; }
396 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
398 if constexpr (not std::is_signed_v<T>) { mpz_add_ui(
m_val,
m_val, i); }
400 if (i > 0) {mpz_add_ui(
m_val,
m_val,
static_cast<uint64_t
>(i)); }
401 else { mpz_sub_ui(
m_val,
m_val,
static_cast<uint64_t
>(-i));}
410 { mpz_add(
m_val,
m_val, i.m_val);
return *
this; }
421 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
423 {
integer a(*
this); a -= i;
return a; }
424#if !defined __LAL_SWIG_PYTHON
430 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
439 {
integer a(*
this); a -= i;
return a; }
444 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
446 if constexpr (not std::is_signed_v<T>) { mpz_sub_ui(
m_val,
m_val, i); }
448 if (i > 0) {mpz_sub_ui(
m_val,
m_val,
static_cast<uint64_t
>(i)); }
449 else { mpz_add_ui(
m_val,
m_val,
static_cast<uint64_t
>(-i));}
458 { mpz_sub(
m_val,
m_val, i.m_val);
return *
this; }
466 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
468 {
integer a(*
this); a *= i;
return a; }
469#if !defined __LAL_SWIG_PYTHON
475 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
485 {
integer a(*
this); a *= i;
return a; }
490 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
492 if constexpr (std::is_signed_v<T>) { mpz_mul_si(
m_val,
m_val, i); }
501 { mpz_mul(
m_val,
m_val, i.m_val);
return *
this; }
509 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
511 {
integer a(*
this); a /= i;
return a; }
512#if !defined __LAL_SWIG_PYTHON
518 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
527 {
integer a(*
this); a /= i;
return a; }
532 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
534 if constexpr (not std::is_signed_v<T>) {
538 mpz_fdiv_q_ui(
m_val,
m_val,
static_cast<uint64_t
>(i<0 ? -i : i));
548 { mpz_div(
m_val,
m_val, i.m_val);
return *
this; }
572 { mpz_pow_ui(
m_val,
m_val, i);
return *
this; }
587 [[nodiscard]] uint64_t
operator% (
const uint64_t i)
const noexcept {
590 const uint64_t m = mpz_mod_ui(r,
m_val, i);
612 [[nodiscard]] std::size_t
bytes() const noexcept;
619 [[nodiscard]] int64_t
to_int() const noexcept {
return mpz_get_si(
m_val); }
621 [[nodiscard]] uint64_t
to_uint() const noexcept {
return mpz_get_ui(
m_val); }
637 buf = mpz_get_str(buf, 10,
m_val);
638 s = std::string(buf);
657#if !defined __LAL_SWIG_PYTHON
Arbitrary precision integer.
Definition integer.hpp:60
integer & operator+=(const T i) noexcept
Addition operator.
Definition integer.hpp:397
friend bool operator>(const T i, const integer &ii) noexcept
Greater than operator.
Definition integer.hpp:327
int64_t to_int() const noexcept
Converts this integer to a signed 64-bit integer.
Definition integer.hpp:619
void swap(integer &i) noexcept
Swaps the value of this integer with integer i's value.
Definition integer.hpp:655
int64_t get_sign() const noexcept
Returns the sign of this integer.
Definition integer.hpp:609
integer & operator*=(const T i) noexcept
Product operator.
Definition integer.hpp:491
friend bool operator>=(const T i, const integer &ii) noexcept
Greater than or equal to operator.
Definition integer.hpp:355
void as_string(std::string &s) const noexcept
Converts this integer to a string.
Definition integer.hpp:635
integer operator-() const noexcept
Minus unary operator. Returns a new object of type 'integer'.
Definition integer.hpp:415
friend bool operator==(const T i, const integer &ii) noexcept
Equality operator.
Definition integer.hpp:217
integer() noexcept
Empty constructor.
Definition integer.hpp:67
integer(const std::string &s) noexcept
Constructor with string.
Definition integer.hpp:97
integer & powt(const integer &i) noexcept
Exponentiation operator.
friend integer operator*(const T i, const integer &ii) noexcept
Product operator.
Definition integer.hpp:476
integer(const integer &i) noexcept
Copy constructor.
Definition integer.hpp:119
friend void swap(integer &i, integer &j) noexcept
Swaps two integers.
Definition integer.hpp:663
friend bool operator<=(const T i, const integer &ii) noexcept
Less than or equal to operator.
Definition integer.hpp:299
integer & operator/=(const T i) noexcept
Division operator.
Definition integer.hpp:533
void set_str(const std::string &s) noexcept
Overwrites the value of this integer with s.
Definition integer.hpp:150
friend bool operator!=(const T i, const integer &ii) noexcept
Non-equality operator.
Definition integer.hpp:243
mpz_t m_val
Structure from GMP storing the integer's value.
Definition integer.hpp:670
integer power(const uint64_t i) const noexcept
Exponentiation operator.
Definition integer.hpp:556
uint64_t to_uint() const noexcept
Converts this integer to an unsigned 64-bit integer.
Definition integer.hpp:621
integer(mpz_t &&raw) noexcept
Constructor with mpz_t.
Definition integer.hpp:73
bool m_initialized
Is this integer initialized?
Definition integer.hpp:672
std::string to_string() const noexcept
Converts this integer to a string.
Definition integer.hpp:626
integer(const T i) noexcept
Constructor with unsigned integer value.
Definition integer.hpp:89
friend int64_t operator/(const T i, const integer &ii) noexcept
Division operator.
Definition integer.hpp:519
integer(integer &&i) noexcept
Move constructor.
Definition integer.hpp:104
integer power(const integer &i) const noexcept
Exponentiation operator.
Definition integer.hpp:562
integer & powt(const uint64_t i) noexcept
Exponentiation operator.
Definition integer.hpp:571
std::size_t bytes() const noexcept
Returns the amount of bytes this integer occupies.
void set_integer(const integer &i) noexcept
Overwrites the value of this integer with i.
Definition integer.hpp:141
~integer() noexcept
Destructor.
Definition integer.hpp:121
friend integer operator+(const T i, const integer &ii) noexcept
Addition operator.
Definition integer.hpp:383
double to_double() const noexcept
Converts this integer to a double-precision floating-point value.
Definition integer.hpp:623
const mpz_t & get_raw_value() const noexcept
Returns the underlying gmp data structure.
Definition integer.hpp:614
integer & operator-=(const T i) noexcept
Substraction operator.
Definition integer.hpp:445
uint64_t operator%(const uint64_t i) const noexcept
Modulus operator.
Definition integer.hpp:587
constexpr bool is_initialized() const noexcept
Returns whether this object is initialized or not.
Definition integer.hpp:607
void set_number(const T i) noexcept
Overwrites the value of this integer with i.
Definition integer.hpp:130
integer & operator=(const T i) noexcept
Assignment operator.
Definition integer.hpp:165
friend bool operator<(const T i, const integer &ii) noexcept
Less operator.
Definition integer.hpp:271
Exact rational number.
Definition rational.hpp:63
Main namespace of the library.
Definition basic_types.hpp:48