Leonardus
Loading...
Searching...
No Matches
soq.h
Go to the documentation of this file.
1
17#pragma once
18
19// Inc Library
20#include <string>
21#include <gmp.h>
22
23// Inc HAA
24#include "adapter128.h"
25#include "dbc.h"
26#include "helper.h"
27#include "error.h"
28
29// Inc Rich
30#include "so.h"
31
32
38class SOQ : public SO {
39
40 mpq_t q_;
43 size_t str_length() const {
44 const size_t num_size = mpz_sizeinbase(mpq_numref(q_), 10);
45 const size_t den_size = mpz_sizeinbase(mpq_denref(q_), 10);
46 // +3 for opional '-' and '/' and '\0'
47 return num_size + den_size + 3;
48 }
49
50protected:
51#ifndef DBC_IS_VOID
55 bool invariant() const noexcept override { /* LCOV_EXCL_START */
56 if( mpz_sgn( mpq_denref( q_ ) ) != 1 )
57 return false;
58 mpz_t g;
59 mpz_init( g );
60 mpz_gcd( g, mpq_numref(q_), mpq_denref(q_) );
61 int rv = mpz_cmp_si( g, 1 );
62 mpz_clear( g );
63 return rv == 0;
64 } /* LCOV_EXCL_STOP */
65#endif
66
67public:
69 SOQ() {
70 mpq_init( q_ );
71
73 }
74
76 SOQ( const mpq_t p_q ) {
77 mpq_init( q_ );
78 mpq_set( q_, p_q );
79
81 }
82
84 SOQ( const __int128 p_num, const __int128 p_den ) {
85 mpq_init( q_ );
86
87 if( p_den == 0 )
88 opErrExit( undefined ); // IDEA: how to handle the problem without an exit() in the ctor?
89 __int128_to_mpz_t( mpq_numref( q_ ), p_num );
90 __int128_to_mpz_t( mpq_denref( q_ ), p_den );
91 mpq_canonicalize( q_ );
92
94 }
95
97 ~SOQ() {
98 DBC_INV;
99
100 mpq_clear( q_ );
101 }
102
103public: /* virtual */
104 [[nodiscard]] SOQ * dup() const override { return new SOQ( q_ ); }
105
106 std::string opequal() const override {
107 char * buffer = new char[ str_length() ];
108 mpq_get_str( buffer, 10, q_ );
109 std::string str( buffer );
110 delete[] buffer;
111 return str;
112 }
113
114 OTCode ot() const override { return OTCode::Q; }
115
116 std::string type() const override { return "rationaltype"; }
117
118 bool equal( const SO * p_other ) const override {
119 auto o = dynamic_cast<const SOQ*>( p_other );
120 return o ? mpq_equal( q_, o->q_) : false;
121 }
122
123 bool gt( const SO * p_other ) const override {
124 auto o = dynamic_cast<const SOQ*>( p_other );
125 if( o == nullptr )
127 return mpq_cmp( q_, o->q_) > 0;
128 }
129
130 bool ge( const SO * p_other ) const override {
131 auto o = dynamic_cast<const SOQ*>( p_other );
132 if( o == nullptr )
134 return mpq_cmp( q_, o->q_) >= 0;
135 }
136
137public: /* accessor */
139 void getRational( mpq_t p_q ) const {
140 mpq_set( p_q, q_ );
141 }
142
144 void setRational( const mpq_t p_q ) {
145 mpq_set( q_, p_q );
146
147 DBC_INV;
148 }
149
151 void getNumerator( mpz_t p_z ) const {
152 mpz_set( p_z, mpq_numref(q_) );
153 }
154
156 void setNumerator( mpz_t p_z ) {
157 mpz_set( mpq_numref(q_), p_z );
158 mpq_canonicalize( q_ );
159
160 DBC_INV;
161 }
162
166 bool getComponents( __int128 & p_num, __int128 & p_den ) const {
167 if( ! mpz_t_to___int128( p_num, mpq_numref( q_ ) ) )
168 return false;
169 if( ! mpz_t_to___int128( p_den, mpq_denref( q_ ) ) )
170 return false;
171 return true;
172 }
173
174public: /* other */
177 bool isunitfraction() const {
178 return mpz_cmp_ui( mpq_numref(q_), 1u ) == 0;
179 }
180
183 bool isinteger() const {
184 return mpz_cmp_ui( mpq_denref(q_), 1u ) == 0;
185 }
186
189 void divqr( mpq_t p_q, mpq_t p_r ) const {
190 mpz_set_ui( mpq_denref( p_q ), 1u ); // set the denominator of p_q = 1
191 mpz_set( mpq_denref( p_r ), mpq_denref( q_ ) ); // set the denominator of p_r = denominator of q_
192 mpz_fdiv_qr( mpq_numref(p_q), mpq_numref(p_r), mpq_numref(q_), mpq_denref(q_) );
193 }
194
201 bool parse( std::string p_str ) {
202 if( mpq_set_str( q_, p_str.c_str(), 10 ) != 0 )
203 return false;
204 if( mpz_sgn( mpq_denref( q_ ) ) == 0 )
205 return false;
206 mpq_canonicalize( q_ );
207
208 DBC_INV;
209 return true;
210 }
211
213 void abs() {
214 mpq_abs( q_, q_ );
215
216 DBC_INV;
217 }
218
220 void neg() {
221 mpq_neg( q_, q_ );
222
223 DBC_INV;
224 }
225
227 void reciprocal() {
228 if( mpz_sgn( mpq_numref( q_ ) ) == 0 )
230 mpq_inv( q_, q_ );
231
232 DBC_INV;
233 }
234
238 __float128 flt128() const;
239};
void __int128_to_mpz_t(mpz_t p_dst, __int128 p_src)
Converts an __int128 into a mpz_t.
Definition adapter128.cpp:184
bool mpz_t_to___int128(__int128 &p_dst, const mpz_t p_src)
Converts a mpz_t into an __int128.
Definition adapter128.cpp:146
Adapters for 128 bit versions of standard functions.
Semantic Object Rational Number.
Definition soq.h:38
std::string opequal() const override
For operators '=', 'cvs' and 'stack'.
Definition soq.h:106
SOQ(const mpq_t p_q)
Ctor.
Definition soq.h:76
void getNumerator(mpz_t p_z) const
Set the parameter to our numerator value.
Definition soq.h:151
SOQ(const __int128 p_num, const __int128 p_den)
Ctor.
Definition soq.h:84
void reciprocal()
Inplace reciprocal.
Definition soq.h:227
void abs()
Inplace abs.
Definition soq.h:213
bool gt(const SO *p_other) const override
Greater than.
Definition soq.h:123
std::string type() const override
Returns a type name.
Definition soq.h:116
OTCode ot() const override
Returns an OTCode.
Definition soq.h:114
void setNumerator(mpz_t p_z)
Replaces numerator.
Definition soq.h:156
bool isinteger() const
Checks denominator == 1.
Definition soq.h:183
bool getComponents(__int128 &p_num, __int128 &p_den) const
Set the parameters to numerator and denominator.
Definition soq.h:166
SOQ * dup() const override
Creates a new instance as copy following the red book definition.
Definition soq.h:104
bool parse(std::string p_str)
Parses the string for a presentation of a rational.
Definition soq.h:201
~SOQ()
Dtor.
Definition soq.h:97
void divqr(mpq_t p_q, mpq_t p_r) const
Calculates quotient and remainder.
Definition soq.h:189
bool ge(const SO *p_other) const override
Greater or equal.
Definition soq.h:130
size_t str_length() const
Size of the string representation in characters.
Definition soq.h:43
bool invariant() const noexcept override
Checks class invariants.
Definition soq.h:55
void neg()
Inplace neg.
Definition soq.h:220
void getRational(mpq_t p_q) const
Set the parameter to our value.
Definition soq.h:139
void setRational(const mpq_t p_q)
Replaces the rational by the parameter.
Definition soq.h:144
bool equal(const SO *p_other) const override
Equality.
Definition soq.h:118
SOQ()
Ctor.
Definition soq.h:69
bool isunitfraction() const
Checks numerator == 1.
Definition soq.h:177
mpq_t q_
The rational number by GMP.
Definition soq.h:40
__float128 flt128() const
A real representation of the rational.
Definition soq.cpp:30
Semantic Object.
Definition so.h:58
Helpers for design by contract idioms.
#define DBC_INV_CTOR(T)
Assert for invariant checks in ctors and dtors.
Definition dbc.h:89
#define DBC_INV
Assert for invariant checks in member functions.
Definition dbc.h:84
void opErrExit(OpError p_err, const std::string &p_details, const std::source_location p_location)
Operator error message to interpreter cout_ and exit( EC_OPERATOR ).
Definition error.cpp:31
Error handling.
@ undefinedresult
PS operator error undefinedresult.
Definition error.h:37
@ typecheck
PS operator error typecheck.
Definition error.h:34
@ undefined
PS operator error undefined.
Definition error.h:38
Miscellaneous definitions and functions.
The class SO - semantic object.
OTCode
OTCode - the Object Type Code.
Definition so.h:33
@ o
SOo.
@ Q
SOQ.