LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
rational.hpp
1/*********************************************************************
2 *
3 * Linear Arrangement Library - A library that implements a collection
4 * algorithms for linear arrangments of graphs.
5 *
6 * Copyright (C) 2019 - 2024
7 *
8 * This file is part of Linear Arrangement Library. The full code is available
9 * at:
10 * https://github.com/LAL-project/linear-arrangement-library.git
11 *
12 * Linear Arrangement Library is free software: you can redistribute it
13 * and/or modify it under the terms of the GNU Affero General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * Linear Arrangement Library is distributed in the hope that it will be
18 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public License
23 * along with Linear Arrangement Library. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * Contact:
26 *
27 * LluĂ­s Alemany Puig (lluis.alemany.puig@upc.edu)
28 * LQMC (Quantitative, Mathematical, and Computational Linguisitcs)
29 * CQL (Complexity and Quantitative Linguistics Lab)
30 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
31 * Webpage: https://cqllab.upc.edu/people/lalemany/
32 *
33 * Ramon Ferrer i Cancho (rferrericancho@cs.upc.edu)
34 * LQMC (Quantitative, Mathematical, and Computational Linguisitcs)
35 * CQL (Complexity and Quantitative Linguistics Lab)
36 * Office 220, Omega building
37 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
38 * Webpage: https://cqllab.upc.edu/people/rferrericancho/
39 *
40 ********************************************************************/
41
42#pragma once
43
44// C includes
45#include <gmp.h>
46
47// C++ includes
48#include <cstdint>
49#include <string>
50
51// lal includes
52#include <lal/numeric/integer.hpp>
53
54namespace lal {
55namespace numeric {
56
63class rational {
64public:
65 /* CONSTRUCTORS */
66
68 rational() noexcept { mpq_init(m_val); }
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
76 { mpq_init(m_val); set_number(n, d); }
82 rational(const integer& n, const integer& d = 1) noexcept
83 { mpq_init(m_val); set_integer(n, d); }
88 rational(const std::string& s) noexcept
89 { mpq_init(m_val); set_str(s); }
94 rational(const rational& r) noexcept
95 { mpq_init(m_val); mpq_set(m_val, r.m_val); }
96
101 rational(integer&& i) noexcept {
102 // move i's contents into numerator
103 m_val[0]._mp_num = *i.m_val;
104 // set the denominator to 1
105 mpz_init_set_ui(&m_val[0]._mp_den, 1);
106 // we must canonicalize
107 mpq_canonicalize(m_val);
108
109 // invalidate i's contents
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;
114 }
121 rational(integer&& n, integer&& d) noexcept {
122 // move n's contents into numerator
123 m_val[0]._mp_num = *n.m_val;
124 // move d's contents into denominator
125 m_val[0]._mp_den = *d.m_val;
126 // we must canonicalize
127 mpq_canonicalize(m_val);
128
129 // invalidate n's contents
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;
134
135 // invalidate d's contents
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;
140 }
146 rational(rational&& r) noexcept {
147 *m_val = *r.m_val;
148
149 // invalidate r's contents
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;
157 }
158
160 ~rational() noexcept { mpq_clear(m_val); }
161
162 /* SETTERS */
163
169 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
170 void set_number(const T n, const uint64_t d = 1) noexcept {
171 if (not is_initialized()) { mpq_init(m_val); }
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);
175 m_initialized = true;
176 }
181 void set_str(const std::string& s) noexcept {
182 if (not is_initialized()) { mpq_init(m_val); }
183 mpq_set_str(m_val, s.c_str(), 10);
184 mpq_canonicalize(m_val);
185 m_initialized = true;
186 }
192 void set_integer(const integer& n, const integer& d) noexcept {
193 if (not is_initialized()) { mpq_init(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);
197 m_initialized = true;
198 }
203 void set_rational(const rational& r) noexcept {
204 if (not is_initialized()) { mpq_init(m_val); }
205 mpq_set(m_val, r.m_val);
206 }
207
214 void invert() noexcept { mpq_inv(m_val, m_val); }
215
216 /* OPERATORS */
217
218 // -- ASSIGNMENT
219
224 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
225 rational& operator= (const T i) noexcept
226 { set_number(i); return *this; }
231 rational& operator= (const std::string& s) noexcept
232 { set_str(s); return *this; }
237 rational& operator= (const integer& i) noexcept
238 { set_integer(i, 1); return *this; }
243 rational& operator= (const rational& r) noexcept
244 { set_rational(r); return *this; }
245
251 rational& operator= (integer&& i) noexcept {
252 // clear this's contents
253 mpq_clear(m_val);
254 m_initialized = true;
255
256 // move i's contents into numerator
257 m_val[0]._mp_num = *i.m_val;
258 // set the denominator to 1
259 mpz_init_set_ui(&m_val[0]._mp_den, 1);
260 // we must canonicalize
261 mpq_canonicalize(m_val);
262
263 // invalidate i's contents
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;
268
269 return *this;
270 }
276 rational& operator= (rational&& r) noexcept {
277 // clear this's contents
278 mpq_clear(m_val);
279 m_initialized = true;
280
281 // move r's contents into this's
282 *m_val = *r.m_val;
283
284 // invalidate r's contents
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;
292
293 return *this;
294 }
295
296 // -- EQUALITY
297
302 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
303 [[nodiscard]] bool operator== (const T i) const noexcept
304 {
305 return
306 (std::is_signed_v<T> ? mpq_cmp_si(m_val, i, 1) : mpq_cmp_ui(m_val, i, 1)) == 0;
307 }
308#if !defined __LAL_SWIG_PYTHON
314 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
315 [[nodiscard]] friend bool operator== (const T i, const rational& r) noexcept
316 { return r == i; }
317#endif
322 [[nodiscard]] bool operator== (const integer& i) const noexcept
323 { rational r(i); return mpq_equal(m_val, r.m_val); }
324#if !defined __LAL_SWIG_PYTHON
330 [[nodiscard]] friend bool operator== (const integer& i, const rational& r) noexcept
331 { return r == i; }
332#endif
337 [[nodiscard]] bool operator== (const rational& r) const noexcept {
338 // this function returns non-zero if parameters are equal!
339 return mpq_equal(m_val, r.m_val);
340 }
341
342 // -- NON-EQUALITY
343
348 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
349 [[nodiscard]] bool operator!= (const T i) const noexcept
350 { return not (*this == i); }
351#if !defined __LAL_SWIG_PYTHON
357 [[nodiscard]] friend bool operator!= (int64_t i, const rational& r) noexcept
358 { return r != i; }
359#endif
364 [[nodiscard]] bool operator!= (const integer& i) const noexcept
365 { return not (*this == i); }
366#if !defined __LAL_SWIG_PYTHON
372 [[nodiscard]] friend bool operator!= (const integer& i, const rational& r) noexcept
373 { return r != i; }
374#endif
379 [[nodiscard]] bool operator!= (const rational& r) const noexcept
380 { return not (*this == r); }
381
382 // -- LESS THAN
383
388 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
389 [[nodiscard]] bool operator< (const T i) const noexcept
390 {
391 return
392 (std::is_signed_v<T> ? mpq_cmp_si(m_val, i, 1) : mpq_cmp_ui(m_val, i, 1)) < 0;
393 }
394#if !defined __LAL_SWIG_PYTHON
400 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
401 [[nodiscard]] friend bool operator< (const T i, const rational& r) noexcept
402 { return r > i; }
403#endif
408 [[nodiscard]] bool operator< (const integer& i) const noexcept
409 { rational r(i); return mpq_cmp(m_val, r.m_val) < 0; }
410#if !defined __LAL_SWIG_PYTHON
416 [[nodiscard]] friend bool operator< (const integer& i, const rational& r) noexcept
417 { return r > i; }
418#endif
423 [[nodiscard]] bool operator< (const rational& r) const noexcept
424 { return mpq_cmp(m_val, r.m_val) < 0; }
425
426 // -- LESS THAN OR EQUAL TO
427
432 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
433 [[nodiscard]] bool operator<= (const T i) const noexcept {
434 return
435 (std::is_signed_v<T> ? mpq_cmp_si(m_val, i, 1) : mpq_cmp_ui(m_val, i, 1)) <= 0;
436 }
437#if !defined __LAL_SWIG_PYTHON
443 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
444 [[nodiscard]] friend bool operator<= (const T i, const rational& r) noexcept
445 { return r >= i; }
446#endif
451 [[nodiscard]] bool operator<= (const integer& i) const noexcept
452 { rational r(i); return mpq_cmp(m_val, r.m_val) <= 0; }
453#if !defined __LAL_SWIG_PYTHON
459 [[nodiscard]] friend bool operator<= (const integer& i, const rational& r) noexcept
460 { return r >= i; }
461#endif
466 [[nodiscard]] bool operator<= (const rational& r) const noexcept
467 { return mpq_cmp(m_val, r.m_val) <= 0; }
468
469 // -- GREATER THAN
470
475 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
476 [[nodiscard]] bool operator> (const T i) const noexcept
477 {
478 return
479 (std::is_signed_v<T> ? mpq_cmp_si(m_val, i, 1) : mpq_cmp_ui(m_val, i, 1)) > 0;
480 }
481#if !defined __LAL_SWIG_PYTHON
487 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
488 [[nodiscard]] friend bool operator> (const T i, const rational& r) noexcept
489 { return r < i; }
490#endif
495 [[nodiscard]] bool operator> (const integer& i) const noexcept
496 { rational r(i); return mpq_cmp(m_val, r.m_val) > 0; }
497#if !defined __LAL_SWIG_PYTHON
503 [[nodiscard]] friend bool operator> (const integer& i, const rational& r) noexcept
504 { return r < i; }
505#endif
510 [[nodiscard]] bool operator> (const rational& r) const noexcept
511 { return mpq_cmp(m_val, r.m_val) > 0; }
512
513 // -- GREATER THAN OR EQUAL TO
514
519 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
520 [[nodiscard]] bool operator>= (const T i) const noexcept {
521 return
522 (std::is_signed_v<T> ? mpq_cmp_si(m_val, i, 1) : mpq_cmp_ui(m_val, i, 1)) >= 0;
523 }
524#if !defined __LAL_SWIG_PYTHON
530 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
531 [[nodiscard]] friend bool operator>= (const T i, const rational& r) noexcept
532 { return r <= i; }
533#endif
538 [[nodiscard]] bool operator>= (const integer& i) const noexcept
539 { rational r(i); return mpq_cmp(m_val, r.m_val) >= 0; }
540#if !defined __LAL_SWIG_PYTHON
546 [[nodiscard]] friend bool operator>= (const integer& i, const rational& r) noexcept
547 { return r <= i; }
548#endif
553 [[nodiscard]] bool operator>= (const rational& r) const noexcept
554 { return mpq_cmp(m_val, r.m_val) >= 0; }
555
556 // -- ADDITION
557
562 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
563 [[nodiscard]] rational operator+ (const T i) const noexcept
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>
572 [[nodiscard]] friend rational operator+ (const T i, const rational& r) noexcept
573 { return r + i; }
574#endif
579 [[nodiscard]] rational operator+ (const integer& i) const noexcept
580 { rational r(*this); r += i; return r; }
581#if !defined __LAL_SWIG_PYTHON
587 [[nodiscard]] friend rational operator+ (const integer& i, const rational& r)
588 noexcept
589 { return r + i; }
590#endif
595 [[nodiscard]] rational operator+ (const rational& r) const noexcept
596 { rational k(*this); k += r; return k; }
597
602 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
603 rational& operator+= (const T i) noexcept
604 { rational r(i); mpq_add(m_val, m_val, r.m_val); return *this; }
609 rational& operator+= (const integer& i) noexcept
610 { rational r(i); mpq_add(m_val, m_val, r.m_val); return *this; }
615 rational& operator+= (const rational& r) noexcept
616 { mpq_add(m_val, m_val, r.m_val); return *this; }
617
618 // -- SUBSTRACTION
619
621 [[nodiscard]] rational operator- () const noexcept
622 { rational r(*this); mpq_neg(r.m_val, r.m_val); return r; }
627 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
628 [[nodiscard]] rational operator- (const T i) const noexcept
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>
637 [[nodiscard]] friend rational operator- (const T i, const rational& r) noexcept
638 { return -r + i; }
639#endif
644 [[nodiscard]] rational operator- (const integer& i) const noexcept
645 { rational r(*this); r -= i; return r; }
646#if !defined __LAL_SWIG_PYTHON
652 [[nodiscard]] friend rational operator- (const integer& i, const rational& r) noexcept
653 { return -r + i; }
654#endif
659 [[nodiscard]] rational operator- (const rational& r) const noexcept
660 { rational k(*this); k -= r; return k; }
661
666 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
667 rational& operator-= (const T i) noexcept
668 { rational r(i); mpq_sub(m_val, m_val, r.m_val); return *this; }
673 rational& operator-= (const integer& i) noexcept
674 { rational r(i); mpq_sub(m_val, m_val, r.m_val); return *this; }
679 rational& operator-= (const rational& r) noexcept
680 { mpq_sub(m_val, m_val, r.m_val); return *this; }
681
682 // -- MULTIPLICATION
683
688 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
689 [[nodiscard]] rational operator* (const T i) const noexcept
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>
698 [[nodiscard]] friend rational operator* (const T i, const rational& r) noexcept
699 { return r*i; }
700#endif
705 [[nodiscard]] rational operator* (const integer& i) const noexcept
706 { rational r(*this); r *= i; return r; }
707#if !defined __LAL_SWIG_PYTHON
713 [[nodiscard]] friend rational operator* (const integer& i, const rational& r) noexcept
714 { return r*i; }
715#endif
720 [[nodiscard]] rational operator* (const rational& r) const noexcept
721 { rational k(*this); k *= r; return k; }
722
727 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
728 rational& operator*= (const T i) noexcept
729 { rational r(i); mpq_mul(m_val, m_val, r.m_val); return *this; }
734 rational& operator*= (const integer& i) noexcept
735 { rational r(i); mpq_mul(m_val, m_val, r.m_val); return *this; }
740 rational& operator*= (const rational& r) noexcept
741 { mpq_mul(m_val, m_val, r.m_val); return *this; }
742
743 // -- DIVISION
744
749 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
750 [[nodiscard]] rational operator/ (const T i) const noexcept
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>
759 [[nodiscard]] friend rational operator/ (const T i, const rational& r) noexcept
760 { rational inv(r); inv.invert(); return inv*i; }
761#endif
762
767 [[nodiscard]] rational operator/ (const integer& i) const noexcept
768 { rational r(*this); r /= i; return r; }
773 [[nodiscard]] rational operator/ (const rational& r) const noexcept
774 { rational k(*this); k /= r; return k; }
775
780 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
781 rational& operator/= (const T i) noexcept {
782 integer I(i); *this /= I; return *this;
783 }
788 rational& operator/= (const integer& i) noexcept;
793 rational& operator/= (const rational& r) noexcept;
794
795 // -- EXPONENTIATION
796
801 [[nodiscard]] rational power(const uint64_t i) const noexcept;
806 [[nodiscard]] rational power(const integer& i) const noexcept;
807
812 rational& powt(const uint64_t i) noexcept;
817 rational& powt(const integer& i) noexcept;
818
819 /* GETTERS */
820
822 [[nodiscard]] bool is_initialized() const noexcept { return m_initialized; }
824 [[nodiscard]] int64_t get_sign() const noexcept { return mpq_sgn(m_val); }
825
827 [[nodiscard]] std::size_t bytes() const noexcept;
828
829 /* CONVERTERS */
830
838 [[nodiscard]] integer to_integer() const noexcept {
839 integer i;
840 as_integer(i);
841 return i;
842 }
850 void as_integer(integer& i) const noexcept;
851
853 [[nodiscard]] double to_double() const noexcept { return mpq_get_d(m_val); }
855 void as_double(double& d) const noexcept { d = mpq_get_d(m_val); }
856
858 [[nodiscard]] std::string to_string() const noexcept {
859 std::string k;
860 as_string(k);
861 return k;
862 }
864 void as_string(std::string& s) const noexcept {
865 char *buf = nullptr;
866 buf = mpq_get_str(buf, 10, m_val);
867 s = std::string(buf);
868 free(buf);
869 }
870
872 [[nodiscard]] integer get_numerator() const noexcept {
873 mpz_t numerator;
874 mpz_init(numerator);
875 mpq_get_num(numerator, m_val);
876 return integer(std::move(numerator));
877 }
878
880 [[nodiscard]] integer get_denominator() const noexcept {
881 mpz_t denominator;
882 mpz_init(denominator);
883 mpq_get_den(denominator, m_val);
884 return integer(std::move(denominator));
885 }
886
887 /* OTHERS */
888
898 void swap(rational& r) noexcept { mpq_swap(m_val, r.m_val); }
899
900#if !defined __LAL_SWIG_PYTHON
906 friend void swap(rational& r1, rational& r2) noexcept { r1.swap(r2); }
907#endif
908
909private:
911 mpq_t m_val;
913 bool m_initialized = true;
914};
915
916} // -- namespace numeric
917} // -- namespace lal
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