47 const size_t num_size = mpz_sizeinbase(mpq_numref(
q_), 10);
48 const size_t den_size = mpz_sizeinbase(mpq_denref(
q_), 10);
50 return num_size + den_size + 3;
60 if( mpz_sgn( mpq_denref(
q_ ) ) != 1 )
64 mpz_gcd( g, mpq_numref(
q_), mpq_denref(
q_) );
65 const int rv = mpz_cmp_si( g, 1 );
83 explicit SOQ(
const mpq_t p_q ) {
96 SOQ(
const __int128 p_num,
const __int128 p_den ) {
103 mpq_canonicalize(
q_ );
117 SOQ(
const SOQ & ) =
delete;
119 SOQ & operator=(
const SOQ & ) =
delete;
120 SOQ & operator=(
SOQ && ) =
delete;
124 [[nodiscard]]
SOQ *
dup()
const override {
return new SOQ(
q_ ); }
130 std::string str( buffer );
139 std::string
type()
const override {
return "rationaltype"; }
144 bool equal(
const SO * p_other )
const override {
145 auto o =
dynamic_cast<const SOQ*
>( p_other );
146 return o ? mpq_equal(
q_,
o->q_) :
false;
153 auto o =
dynamic_cast<const SOQ*
>( p_other );
159 return mpq_cmp(
q_,
o->q_) > 0;
166 auto o =
dynamic_cast<const SOQ*
>( p_other );
172 return mpq_cmp(
q_,
o->q_) >= 0;
195 mpz_set( p_z, mpq_numref(
q_) );
201 mpz_set( mpq_numref(
q_), p_z );
202 mpq_canonicalize(
q_ );
226 return mpz_cmp_ui( mpq_numref(
q_), 1u ) == 0;
233 return mpz_cmp_ui( mpq_numref(
q_), 0u ) == 0;
240 return mpz_cmp_ui( mpq_denref(
q_), 1u ) == 0;
248 void divqr( mpq_t p_q, mpq_t p_r )
const {
249 mpz_set_ui( mpq_denref( p_q ), 1u );
250 mpz_set( mpq_denref( p_r ), mpq_denref(
q_ ) );
251 mpz_fdiv_qr( mpq_numref(p_q), mpq_numref(p_r), mpq_numref(
q_), mpq_denref(
q_) );
262 bool parse(
const std::string & p_str ) {
263 if( mpq_set_str(
q_, p_str.c_str(),
BASE10 ) != 0 )
265 if( mpz_sgn( mpq_denref(
q_ ) ) == 0 )
267 mpq_canonicalize(
q_ );
294 if( mpz_sgn( mpq_numref(
q_ ) ) == 0 )
void __int128_to_mpz_t(mpz_t p_dst, __int128 p_src)
Converts an __int128 into a mpz_t.
Definition adapter128.cpp:128
bool mpz_t_to___int128(__int128 &p_dst, const mpz_t p_src)
Converts a mpz_t into an __int128.
Definition adapter128.cpp:90
Adapters for 128 bit versions of standard functions.
Semantic Object Rational Number.
Definition soq.h:38
std::string opequal() const override
String representation of object for operators '=', 'cvs' and 'stack'.
Definition soq.h:127
SOQ(const mpq_t p_q)
Ctor from a mpq_t.
Definition soq.h:83
void getNumerator(mpz_t p_z) const
Getter for the numerator of q_.
Definition soq.h:194
SOQ(const __int128 p_num, const __int128 p_den)
Ctor from a numerator and a denominator.
Definition soq.h:96
void abs()
In place abs.
Definition soq.h:275
std::string type() const override
Returns a type name.
Definition soq.h:139
bool gt(OpError &p_ec, const SO *p_other) const override
Greater than.
Definition soq.h:152
~SOQ() override
Dtor.
Definition soq.h:110
OTCode ot() const override
Returns an OTCode.
Definition soq.h:136
void setNumerator(mpz_t p_z)
Setter for the numerator of q_.
Definition soq.h:200
bool isinteger() const
Checks denominator == 1.
Definition soq.h:239
bool getComponents(__int128 &p_num, __int128 &p_den) const
Getter for numerator and denominator.
Definition soq.h:213
SOQ * dup() const override
Creates a new instance as copy following the red book definition.
Definition soq.h:124
bool ge(OpError &p_ec, const SO *p_other) const override
Greater or equal.
Definition soq.h:165
void reciprocal(OpError &p_ec)
In place reciprocal.
Definition soq.h:292
void divqr(mpq_t p_q, mpq_t p_r) const
Calculates quotient and remainder.
Definition soq.h:248
size_t str_length() const
Calculates an upper limit of the maximum length of the string representation of q_.
Definition soq.h:46
bool invariant() const noexcept override
Checks class invariants.
Definition soq.h:59
void neg()
In place neg.
Definition soq.h:283
void getRational(mpq_t p_q) const
Getter for q_.
Definition soq.h:179
bool iszero() const
Checks numerator == 0.
Definition soq.h:232
void setRational(const mpq_t p_q)
Setter for the rational number.
Definition soq.h:185
__float128 float128() const
Converts the rational number to a real number.
Definition soq.cpp:30
bool parse(const std::string &p_str)
Parses a string representation of a rational number.
Definition soq.h:262
bool equal(const SO *p_other) const override
Checks if this SO is equal to another SO.
Definition soq.h:144
SOQ()
Ctor.
Definition soq.h:74
bool isunitfraction() const
Checks numerator == 1.
Definition soq.h:225
mpq_t q_
The rational number by GMP.
Definition soq.h:42
Semantic Object.
Definition so.h:60
Helpers for design by contract idioms.
#define DBC_INV_CTOR(T)
Assert for invariant checks in ctors and dtors.
Definition dbc.h:121
#define DBC_INV
Assert for invariant checks in member functions.
Definition dbc.h:113
#define DBC_PRE(XXX)
Assert for preconditions.
Definition dbc.h:103
OpError
Operator error code enum.
Definition error.h:30
@ undefinedresult
operator error undefinedresult
Definition error.h:39
@ success
No operator error.
Definition error.h:32
@ typecheck
operator error typecheck
Definition error.h:36
Miscellaneous definitions and functions.
constexpr int BASE10
We use base 10.
Definition helper.h:97
The class SO - semantic object.
OTCode
OTCode - the Object Type Code.
Definition so.h:35