ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Rational.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File Rational.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
46/*--------------------------------------------------------------------------*/
47/*----------------------------- DEFINITIONS --------------------------------*/
48/*--------------------------------------------------------------------------*/
49
50#ifndef __ADS_Rational
51 #define __ADS_Rational
52/* self-identification: #endif constant */
53
54/*--------------------------------------------------------------------------*/
55/*------------------------------ INCLUDES ----------------------------------*/
56/*--------------------------------------------------------------------------*/
57
58#include "ADS/Contract.h"
60
61#include <compare>
62#include <concepts>
63#include <cstddef>
64#include <string>
65#include <utility>
66
67// the backend of GMP, with ADS_WITH_GMP: the operations go to the mpq
68// layer of GMP, wherever BigInteger is on GMP
69#ifndef ADS_RATIONAL_GMP
70 #define ADS_RATIONAL_GMP ADS_BIGINT_GMP
71#endif
72
73/*--------------------------------------------------------------------------*/
74/*--------------------------- NAMESPACE ------------------------------------*/
75/*--------------------------------------------------------------------------*/
76
77namespace ADS
78{
79
80/*--------------------------------------------------------------------------*/
81/*---------------------------- CLASS Rational ------------------------------*/
82/*--------------------------------------------------------------------------*/
83
86{
87
88/*--------------------------------------------------------------------------*/
89 public:
94
96 [[nodiscard]] const BigInteger & num( void ) const { return( f_num ); }
98 [[nodiscard]] const BigInteger & den( void ) const { return( f_den ); }
99
101 [[nodiscard]] std::string toString( void ) const;
102
103#if defined( ADS_WITH_GMP )
107 static Rational fromGmp( mpq_srcptr q );
108
111 template< class Q >
112 requires requires( const Q & q ) {
113 { q.get_mpq_t() } -> std::convertible_to< mpq_srcptr >;
114 }
115 explicit Rational( const Q & q ) : Rational( fromGmp( q.get_mpq_t() ) ) { }
116
119 void toGmp( mpq_ptr q ) const;
120
123 template< class Q >
124 requires requires( Q & q ) {
125 { q.get_mpq_t() } -> std::convertible_to< mpq_ptr >;
126 }
127 [[nodiscard]] Q toGmp( void ) const {
128 Q q;
129 toGmp( q.get_mpq_t() );
130 return( q );
131 }
132#endif
133
150 friend Rational operator+( const Rational & a , const Rational & b );
152 friend Rational operator-( const Rational & a , const Rational & b );
168 friend Rational operator*( const Rational & a , const Rational & b );
169
172 friend Rational operator/( const Rational & a , const Rational & b );
173
175 Rational operator-( void ) const;
176
178 friend bool operator==( const Rational & a , const Rational & b ) = default;
181 friend std::strong_ordering operator<=>( const Rational & a ,
182 const Rational & b );
183
184/*--------------------------------------------------------------------------*/
185 private:
186 struct Reduced
187 { };
188
191 Rational( BigInteger num , BigInteger den , Reduced );
192
194 [[nodiscard]] bool lowest( void ) const;
195
196#if ADS_RATIONAL_GMP
198 static mpq_srcptr gmpView( mpq_t q , const Rational & a );
199
202 static Rational gmpApply( const Rational & a , const Rational & b ,
203 void ( *op )( mpq_ptr , mpq_srcptr , mpq_srcptr ) );
204
206 static int gmpCompare( const Rational & a , const Rational & b );
207#endif
208
209 BigInteger f_num ,
210 f_den;
211
212 }; // end( class Rational )
213
214/*--------------------------------------------------------------------------*/
215
216} // end( namespace ADS )
217
218/*--------------------------------------------------------------------------*/
219/*-------------------------- IMPLEMENTATION --------------------------------*/
220/*--------------------------------------------------------------------------*/
221
222#include "ADS/numeric/impl/Rational.tpp"
223
224/*--------------------------------------------------------------------------*/
225
226#endif /* Rational.h included */
227
228/*--------------------------------------------------------------------------*/
229/*-------------------------- End File Rational.h ---------------------------*/
230/*--------------------------------------------------------------------------*/
The integers of any length of Knuth (4.3.1): a sign and the magnitude as a vector of digits (limbs) i...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
an integer of any length, sign and magnitude in base
Definition BigInteger.h:348
a fraction in lowest terms, the denominator positive
Definition Rational.h:86
friend std::strong_ordering operator<=>(const Rational &a, const Rational &b)
the order of and , by the signs and then by against
friend Rational operator+(const Rational &a, const Rational &b)
the sum , by the gcds of the smaller numbers
friend Rational operator*(const Rational &a, const Rational &b)
the product , by the gcds that cancel crosswise
friend Rational operator/(const Rational &a, const Rational &b)
the quotient , as times the inverse of
const BigInteger & den(void) const
the denominator
Definition Rational.h:98
const BigInteger & num(void) const
the numerator
Definition Rational.h:96
friend bool operator==(const Rational &a, const Rational &b)=default
true if the two fractions are equal, i.e., their terms are
Rational operator-(void) const
the opposite
Rational(BigInteger num=0, BigInteger den=1)
the fraction , brought to lowest terms with a positive denominator by one gcd
friend Rational operator-(const Rational &a, const Rational &b)
the difference , as
std::string toString(void) const
the text u/u', or u when
Definition ADT.h:96