Leonardus
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
SOQ Class Reference

Semantic Object Rational Number. More...

#include <soq.h>

+ Inheritance diagram for SOQ:
+ Collaboration diagram for SOQ:

Public Member Functions

 SOQ ()
 Ctor.
 
 SOQ (std::string &p_s)
 Ctor from a string representation.
 
 SOQ (const mpq_t p_q)
 Ctor from a mpq_t.
 
 SOQ (const __int128 p_num, const __int128 p_den)
 Ctor from a numerator and a denominator.
 
 ~SOQ () override
 Dtor.
 
 SOQ (const SOQ &)=delete
 
 SOQ (SOQ &&)=delete
 
SOQ & operator= (const SOQ &)=delete
 
SOQ & operator= (SOQ &&)=delete
 
SOQ * dup () const override
 Creates a new instance as copy following the red book definition.
 
std::string opequal () const override
 String representation of object for operators '=', 'cvs' and 'stack'.
 
OTCode ot () const override
 Returns an OTCode.
 
std::string_view type () const override
 Returns a type name.
 
bool equal (const SO *p_other) const override
 Checks if this SO is equal to another SO.
 
bool gt (OpError &p_ec, const SO *p_other) const override
 Greater than.
 
bool ge (OpError &p_ec, const SO *p_other) const override
 Greater or equal.
 
void getRational (mpq_t p_q) const
 Getter for q_.
 
void setRational (const mpq_t p_q)
 Setter for the rational number.
 
void getNumerator (mpz_t p_z) const
 Getter for the numerator of q_.
 
void setNumerator (mpz_t p_z)
 Setter for the numerator of q_.
 
bool getComponents (__int128 &p_num, __int128 &p_den) const
 Getter for numerator and denominator.
 
bool isunitfraction () const
 Checks numerator == 1.
 
bool isinteger () const
 Checks denominator == 1.
 
void divqr (mpq_t p_q, mpq_t p_r) const
 Calculates quotient and remainder.
 
bool parse (std::string &p_str)
 Parses a string representation of a rational number.
 
void abs__1 ()
 In place abs.
 
void neg__1 ()
 In place neg.
 
void sqr__1 ()
 In place sqr.
 
void half__1 ()
 In place half value.
 
void reciprocal__1 (OpError &p_ec)
 In place reciprocal.
 
__float128 float128 (OpError &p_ec) const
 Converts the rational number to a real number.
 
- Public Member Functions inherited from SO
 SO ()
 Ctor.
 
 SO (bool p_exec)
 Ctor.
 
 SO (const SO &p_other)
 Copy ctor.
 
virtual ~SO ()=default
 Virtual dtor.
 
 SO (SO &&)=delete
 
SO & operator= (const SO &)=delete
 
SO & operator= (SO &&)=delete
 
bool getExec () const
 Getter for exec_.
 
void setExec (bool p_exec)
 Setter for exec_.
 
virtual size_t getSize () const
 Returns the size of the SO.
 
virtual SO * clone () const
 Creates a new instance as copy with deep cloning.
 
virtual std::string opequalequal () const
 String representation for operators '==' and 'pstack'.
 
- Public Member Functions inherited from Counter< SO >
 Counter (Counter< SO > &&)=delete
 
Counter< SO > & operator= (const Counter< SO > &)=delete
 
Counter< SO > & operator= (Counter< SO > &&)=delete
 

Protected Member Functions

bool invariant () const noexcept override
 Checks class invariants.
 
- Protected Member Functions inherited from Counter< SO >
 Counter ()
 Ctor.
 
 Counter (const Counter< SO > &)
 Copy ctor.
 
 ~Counter ()
 Dtor.
 
- Protected Member Functions inherited from DbC
 ~DbC ()=default
 Protected dtor.
 

Private Member Functions

size_t str_length () const
 Calculates an upper limit of the maximum length of the string representation of q_.
 
bool parse_work (std::string &p_str)
 Parses a string representation of a rational number.
 

Private Attributes

mpq_t q_ {}
 The rational number by GMP.
 

Additional Inherited Members

- Static Public Member Functions inherited from Counter< SO >
static size_t getTotalCounter ()
 Static getter for totaly created objects.
 
static size_t getAliveCounter ()
 Static getter for objects alive.
 
static size_t getWatermarkCounter ()
 Static getter for the object counter watermark.
 

Detailed Description

Semantic Object Rational Number.

A multiple precicion rational number.

Invariant
denominator > 0
gcd of numerator and denominator == 1

Constructor & Destructor Documentation

◆ SOQ() [1/3]

SOQ::SOQ ( std::string &  p_s)
inlineexplicit

Ctor from a string representation.

Precondition
p_s format is a valid rational number: [+-]?[0-9]+:[0-9]+ but not [+-]?[0-9]:0+
Parameters
[in]p_sThe string to parse as rational number.

◆ SOQ() [2/3]

SOQ::SOQ ( const mpq_t  p_q)
inlineexplicit

Ctor from a mpq_t.

Parameters
[in]p_qThe value to be used.

◆ SOQ() [3/3]

SOQ::SOQ ( const __int128  p_num,
const __int128  p_den 
)
inline

Ctor from a numerator and a denominator.

Precondition
p_den != 0
Parameters
[in]p_numThe numerator of the rational number.
[in]p_denThe denominator of the rational number.

Member Function Documentation

◆ divqr()

void SOQ::divqr ( mpq_t  p_q,
mpq_t  p_r 
) const
inline

Calculates quotient and remainder.

Parameters
[out]p_qThe quotient as rational number with denominator 1.
[out]p_rThe remainder as rational number 0<=Q<1 thus a proper fraction.

◆ dup()

SOQ * SOQ::dup ( ) const
inlineoverridevirtual

Creates a new instance as copy following the red book definition.

Returns
SO * ... A copy.

Implements SO.

◆ equal()

bool SOQ::equal ( const SO *  p_other) const
inlineoverridevirtual

Checks if this SO is equal to another SO.

Equality means:
SOL und SOM are always equal.
For SOD, SOF and SOK, share_ptr<> points to the same object.
For SOA, share_ptr<> points to the same object, but empty arrays are equal nonetheless.
For SOO and SOo the function pointer must be the same.
Otherwise the value must be the same, even for SOS with shared_ptr<>.

Precondition
parameter != nullptr
Returns
bool ... true if the SO's are of the same type and equal
See also
Will be used directly and unchanged as operator eq
Parameters
[in]p_otherThe object to compare with.

Implements SO.

◆ float128()

__float128 SOQ::float128 ( OpError &  p_ec) const

Converts the rational number to a real number.

Postcondition
The result is finit.
Parameters
[out]p_ecOpError::success or an error code of the conversion.
Returns
__float128 ... The representation as R.

◆ ge()

bool SOQ::ge ( OpError &  p_ec,
const SO *  p_other 
) const
inlineoverridevirtual

Greater or equal.

The redundancy between equal(), gt() and ge() is a matter of performance.

Parameters
[out]p_ecError code.
Returns
bool ... true is comparable and ge.
Precondition
p_other != nullptr
Parameters
[in]p_otherThe object to compare with.

Reimplemented from SO.

◆ getComponents()

bool SOQ::getComponents ( __int128 &  p_num,
__int128 &  p_den 
) const
inline

Getter for numerator and denominator.

Parameters
[out]p_numGets the the numerator of q_ converted into __int128.
[out]p_denGets the the denominator of q_ converted into __int128.
Returns
bool ... true if the components of the fraction fit into __int128.

◆ getNumerator()

void SOQ::getNumerator ( mpz_t  p_z) const
inline

Getter for the numerator of q_.

Parameters
[out]p_zGets the the numerator of q_

◆ getRational()

void SOQ::getRational ( mpq_t  p_q) const
inline

Getter for q_.

Parameters
[out]p_qGets the value of q_

◆ gt()

bool SOQ::gt ( OpError &  p_ec,
const SO *  p_other 
) const
inlineoverridevirtual

Greater than.

The redundancy between equal(), gt() and ge() is a matter of performance.

Parameters
[out]p_ecError code.
Returns
bool ... true is comparable and gt.
Precondition
p_other != nullptr
Parameters
[in]p_otherThe object to compare with.

Reimplemented from SO.

◆ invariant()

bool SOQ::invariant ( ) const
inlineoverrideprotectedvirtualnoexcept

Checks class invariants.

Returns
bool ... Indicates whether the class invariants are maintained.

Reimplemented from DbC.

◆ isinteger()

bool SOQ::isinteger ( ) const
inline

Checks denominator == 1.

Returns
bool ... true, if denominator == 1.

◆ isunitfraction()

bool SOQ::isunitfraction ( ) const
inline

Checks numerator == 1.

Returns
bool ... true, if numerator == 1.

◆ opequal()

std::string SOQ::opequal ( ) const
overridevirtual

String representation of object for operators '=', 'cvs' and 'stack'.

Returns
std::string ... The representation.

Implements SO.

◆ ot()

OTCode SOQ::ot ( ) const
inlineoverridevirtual

Returns an OTCode.

A "manual" implementation of polymorphism, where C++-polymorphisms fail

Returns
OTCode ... The OTCode of the instance.

Implements SO.

◆ parse()

bool SOQ::parse ( std::string &  p_str)
inline

Parses a string representation of a rational number.

Accepts two integers separated by a ':' or a '/'.
Accepts a single integer without delimiting characters.
Ignores whitespaces completely.

Parameters
[in]p_strThe string to parse.
Returns
bool ... true if parsing succeeds

◆ parse_work()

bool SOQ::parse_work ( std::string &  p_str)
private

Parses a string representation of a rational number.

Accepts two integers separated by a ':' or a '/'.
Accepts a single integer without delimiting characters.
Ignores whitespaces completely.

This function is separated from parse() to allow a call from the ctor.

Parameters
[in]p_strThe string to parse.
Returns
bool ... true if parsing succeeds

◆ reciprocal__1()

void SOQ::reciprocal__1 ( OpError &  p_ec)
inline

In place reciprocal.

Parameters
[out]p_ecSuccess or error code.

◆ setRational()

void SOQ::setRational ( const mpq_t  p_q)
inline

Setter for the rational number.


◆ str_length()

size_t SOQ::str_length ( ) const
inlineprivate

Calculates an upper limit of the maximum length of the string representation of q_.

Returns
size_t ... The maximum size.

◆ type()

std::string_view SOQ::type ( ) const
inlineoverridevirtual

Returns a type name.

Returns
std::string_view ... The type name of the instance.

Implements SO.

Member Data Documentation

◆ q_

mpq_t SOQ::q_ {}
private

The rational number by GMP.

The initialization is a dead store.


The documentation for this class was generated from the following files: