Multi-Precision Arithmetic by C++ with no
use of assembler
SN library Copyright (C) 1999-2018 K.Tsuru
Reference of class functions part 9
11. SRational class's member and friend functions
The prototype declarations are given in "sratio.h".
This is a multi-precision rational class with the radix BRADIX = 32,768, using
"SInteger" arithmetic. This class almost the same construction
as that of SFraction class. For rather short number this class is faster a
little. For a long number SFraction class will be faster, if you switch
into the Newton's division by "SLong::UsesNewtonLLDiv();" which
is used in the reduction.
Firstly the list of unusable substitution operators and constructors is given.
private://cannot use and has no body
SRational& operator=(const SDouble& a);
SRational& operator=(const SDecimal& a);
SRational& operator=(const SLong& a);
SRational(const SLong& n);
SRational(const SLong& n, const SLong& d); This
constructor is not provided. Please use "Set()" function below.
SRational(const SFraction& n);
private members
form : SInteger num, den;
comment
"f .num" holds the numerator of f, "f .den" the denominator. "num" has the sign
(1, 0, or -1) and "den" is always positive.
Constructors
form
SRational(); default constructor
SRational(double n); It sets a fraction "n/1".
SRational(double n, double d); It sets "n/d " by double,
int, long, etc. Both n and d must be integers.
SRational(const char* s); It sets by a literal such as
"12345/56789" which must be in a decimal system.
SRational(const SInteger& n); It sets "n/1 " by SInteger.
SRational(const SInteger& n, const SInteger& d); It sets "n/d " by SInteger.
SRational(const SRational& a); copy constructor
ReduceSize, Reduce, ReduceDone
function These have the same functions as those of SFraction class.
form
inline static uint SInteger::ReduceSize(); It returns "reduceSize" below.
inline void SInteger::Reduce(); "f .Reduce();" reduces fraction
f .
inline bool SInteger::ReduceDone() const; It returns reduced (1) or not (0).
SetDouble, SetLong, Set, SetZero
function "f .SetXXX(n, d );" sets f =n/d
.
form
void SRational::SetDouble(double n, double d = 1.0L); by double values
void SRational::SetLong(long n, long d = 1L); by long values
void SRational::Set(const SLong& n, const SLong& d );
by SLong or SInteger values
void SRational::SetZero(); It sets f = 0/1.
comment In "f .Set(n ,d );" both n and
d must have the same type, either SLong or SInteger. If
they are SLong variables, it calls radix conversion into SInteger
ones.
substitution operators
form
SRational& SRational::operator=(const char *s); s must have a decimal representation, not binary radix.
SRational& SRational::operator=(double d); "f =d
;" sets "f = d/1".
SRational& operator=(const SRational& a);
SRational& operator=(const SInteger& a);
For details see the SFraction class.
Num, Den, NumNR, DenNR
function "f .Num();" or "f .NumNR();" returns the numerator of
f and "f .Den();" or "f .DenNR();" the denominator.
form
inline SInteger SRational::Num();
inline SInteger SRational::Den();
inline SInteger SRational::NumNR() const;
inline SInteger SRational::DenNR() const;
For details see the SFraction class.
fundamental operators +, -, *, / and compound operators +=, -=, *=, /=
These operators are overloaded. All the objects which are usable in the constructors
can be used as operands.
relation operators >, <, ==, !=, >=, <=
These operators are overloaded.
Sign
function "f .Sign();" returns the sign of f which is
held by f .num.
form : inline int SRational::Sign(int id= 80) const;
ChangeSign
function "f .ChangeSign();" changes the sign of f , i.e.
f = -f ;.
form inline void SRational::ChangeSign(int id = 81);
[Other related functions]
Rabs
function "Rabs(f );" returns the absolute value of SRational
f, i.e. |f |.
form SRational Rabs(const SRational& f );
RRCompare
function "RRCompare(m , n );" compares the absolute value of
m with that of n , and returns
1 if |m|>|n|,
0 if |m|==|n|,
-1 if |m|<|n|.
form
int RRCompare(const SRational& m, const SRational& n);