53 const size_t num_size = mpz_sizeinbase( mpq_numref(
q_),
BASE10 );
54 const size_t den_size = mpz_sizeinbase( mpq_denref(
q_),
BASE10 );
56 return num_size + den_size + 3;
78 if( mpz_sgn( mpq_denref(
q_ ) ) != 1 )
82 mpz_gcd( g, mpq_numref(
q_), mpq_denref(
q_) );
83 const int rv = mpz_cmp_si( g, 1 );
103 explicit SOQ( std::string & p_s ) {
115 explicit SOQ(
const mpq_t p_q ) {
128 SOQ(
const __int128 p_num,
const __int128 p_den ) {
135 mpq_canonicalize(
q_ );
149 SOQ(
const SOQ & ) =
delete;
151 SOQ & operator=(
const SOQ & ) =
delete;
152 SOQ & operator=(
SOQ && ) =
delete;
156 [[nodiscard]]
SOQ *
dup()
const override {
return new SOQ(
q_ ); }
159 std::string
opequal()
const override;
165 std::string_view
type()
const override {
return "rationaltype"; }
170 bool equal(
const SO * p_other )
const override {
173 if( p_other->
ot() !=
ot() )
175 const auto o =
static_cast<const SOQ *
>( p_other );
176 return mpq_equal(
q_,
o->q_);
190 const auto o =
static_cast<const SOQ *
>( p_other );
192 return mpq_cmp(
q_,
o->q_) > 0;
206 const auto o =
static_cast<const SOQ *
>( p_other );
208 return mpq_cmp(
q_,
o->q_) >= 0;
231 mpz_set( p_z, mpq_numref(
q_) );
237 mpz_set( mpq_numref(
q_), p_z );
238 mpq_canonicalize(
q_ );
266 return mpz_cmp_ui( mpq_numref(
q_), 1u ) == 0;
273 return mpz_cmp_ui( mpq_denref(
q_), 1u ) == 0;
281 void divqr( mpq_t p_q, mpq_t p_r )
const {
282 mpz_set_ui( mpq_denref( p_q ), 1u );
283 mpz_set( mpq_denref( p_r ), mpq_denref(
q_ ) );
284 mpz_fdiv_qr( mpq_numref(p_q), mpq_numref(p_r), mpq_numref(
q_), mpq_denref(
q_) );
298 mpq_set_si(
q_, 0, 1 );
323 mpz_mul( mpq_numref(
q_), mpq_numref(
q_), mpq_numref(
q_) );
324 mpz_mul( mpq_denref(
q_), mpq_denref(
q_), mpq_denref(
q_) );
332 mpq_div_2exp(
q_,
q_, 1 );
342 if( mpz_sgn( mpq_numref(
q_ ) ) == 0 )
Semantic Object Rational Number.
Definition soq.h:43
std::string opequal() const override
String representation of object for operators '=', 'cvs' and 'stack'.
Definition soq.cpp:36
SOQ(const mpq_t p_q)
Ctor from a mpq_t.
Definition soq.h:115
void getNumerator(mpz_t p_z) const
Getter for the numerator of q_.
Definition soq.h:230
SOQ(const __int128 p_num, const __int128 p_den)
Ctor from a numerator and a denominator.
Definition soq.h:128
void neg__1()
In place neg.
Definition soq.h:314
void reciprocal__1(OpError &p_ec)
In place reciprocal.
Definition soq.h:340
bool gt(OpError &p_ec, const SO *p_other) const override
Greater than.
Definition soq.h:183
~SOQ() override
Dtor.
Definition soq.h:142
void abs__1()
In place abs.
Definition soq.h:306
OTCode ot() const override
Returns an OTCode.
Definition soq.h:162
void setNumerator(mpz_t p_z)
Setter for the numerator of q_.
Definition soq.h:236
bool isinteger() const
Checks denominator == 1.
Definition soq.h:272
SOQ(std::string &p_s)
Ctor from a string representation.
Definition soq.h:103
bool getComponents(__int128 &p_num, __int128 &p_den) const
Getter for numerator and denominator.
Definition soq.h:249
SOQ * dup() const override
Creates a new instance as copy following the red book definition.
Definition soq.h:156
bool ge(OpError &p_ec, const SO *p_other) const override
Greater or equal.
Definition soq.h:199
void divqr(mpq_t p_q, mpq_t p_r) const
Calculates quotient and remainder.
Definition soq.h:281
size_t str_length() const
Calculates an upper limit of the maximum length of the string representation of q_.
Definition soq.h:52
bool invariant() const noexcept override
Checks class invariants.
Definition soq.h:77
void half__1()
In place half value.
Definition soq.h:331
bool parse(std::string &p_str)
Parses a string representation of a rational number.
Definition soq.h:295
bool parse_work(std::string &p_str)
Parses a string representation of a rational number.
Definition soq.cpp:65
void getRational(mpq_t p_q) const
Getter for q_.
Definition soq.h:215
void setRational(const mpq_t p_q)
Setter for the rational number.
Definition soq.h:221
__float128 float128(OpError &p_ec) const
Converts the rational number to a real number.
Definition soq.cpp:48
void sqr__1()
In place sqr.
Definition soq.h:322
bool equal(const SO *p_other) const override
Checks if this SO is equal to another SO.
Definition soq.h:170
SOQ()
Ctor.
Definition soq.h:92
bool isunitfraction() const
Checks numerator == 1.
Definition soq.h:265
std::string_view type() const override
Returns a type name.
Definition soq.h:165
mpq_t q_
The rational number by GMP.
Definition soq.h:47
Semantic Object.
Definition so.h:67
virtual OTCode ot() const =0
Returns an OTCode.
Helpers for design by contract idioms.
#define DBC_INV_CTOR(T)
Assert for invariant checks in ctors and dtors.
Definition dbc.h:122
#define DBC_INV
Assert for invariant checks in member functions.
Definition dbc.h:114
#define DBC_PRE(XXX)
Assert for preconditions.
Definition dbc.h:104
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:67
bool hasRationalSyntax(const std::string &p_str)
Checks, if the string has the syntax of a rational number.
Definition algo.cpp:125
std::pair< bool, __int128 > mpz_t_to___int128(const mpz_t p_src)
Converts a mpz_t into an __int128.
Definition conversion.cpp:99
void __int128_to_mpz_t(mpz_t p_dst, __int128 p_src)
Converts an __int128 into a mpz_t.
Definition conversion.cpp:135
The class SO - semantic object.
OTCode
OTCode - the Object Type Code.
Definition so.h:38