Multi-Precision Arithmetic by C++ with no use of assembler
SN library Copyright (C) 1999-2018 K.Tsuru

Reference of class functions part 8


10. SFraction class's member and friend functions
The prototype declarations are given in "sfract.h".
This is a multi-precision fraction class with the radix DRADIX = 10,000, using "SLong" arithmetic.

private members
form : SLong 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
SFraction(); default constructor
SFraction(double n); It sets a fraction "n/1".
SFraction(double n, double d ); It sets "n/d " by double, int, long, etc. Both n and d must be integers.
SFraction(const char* s ); It sets by a literal such as "12345/56789".
SFraction(const SLong& n); This provides a method to convert SF*SL to SF*(SL/1.0), not SD*SD.
SFraction(const SLong& n, const SLong& d ); It sets "n/d " by SLong.
SFraction(const SFraction& a ); copy constructor


ReduceSize, Reduce, ReduceDone
function These provide the method and information of reduction.
form
inline static uint SFraction::ReduceSize();  It returns "reduceSize" below.
inline void SFraction::Reduce(); "f .Reduce();" reduces fraction f .
inline bool SFraction::ReduceDone() const; It returns reduced (1) or not (0).

[About the timing of reduction]
If the reduction would be done every time after an operation, it takes much time. Then the reduction is called when
1. maximum figure between numerator and denominator exceeds a threshold value "reduceSize".
2. output function is called.
3. "Reduce()" function is explicitly called.

SetDouble, SetLong, Set, SetZero
function "f .SetXXX(n, d );" sets f =n/d .
form
void SFraction::SetDouble(double n, double d = 1.0L); by double values
void SFraction::SetLong(long n , long d = 1L); by long values
void SFraction::Set(const SLong& n , const SLong& d ); by SLong values
void SFraction::SetZero(); "f .SetZero();" It sets f = 0/1.
comment Usually you can use "f .Set(n, d );", but it calls SLong constructors and has a little overhead. Then in a large "for" loop it is better another function. In "SetDouble(n, d );" both n and d must be integers. In the first three functions give numerator to first argument, denominator to second one.

substitution operators
form
inline SFraction& SFraction::operator=(const char *s ); s has a form "nnnn/dddd"
inline SFraction& SFraction::operator=(double d ); "f =d ;" sets "f = d/1".
SFraction& SFraction::operator=(const SFraction& a );
SFraction& SFraction::operator=(const SLong& a ); "f =a ;" sets "f =a/1".
comment These are overloaded. For a double value its decimal part is omitted, e.g. SF = 2.5; ---> 2/1 not 5/2.

Num, Den, NumNR, DenNR
function "f .Num();" or "f .NumNR();" returns the numerator of and "f .Den();" or "f .DenNR();" the denominator.
form
inline SLong SFraction::Num();
inline SLong SFraction::Den();
inline SLong SFraction::NumNR() const;
inline SLong SFraction::DenNR() const;
comment The first two functions return an irreducible fraction, other two return a maybe reducible (Not Reduced) fraction.

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 SFraction::Sign(int id = 70) const;

ChangeSign
function "f .ChangeSign();" changes the sign of f , i.e. f = -f ;.
form inline void SFraction::ChangeSign(int id = 71);

[Other related functions]
Fabs
function "Fabs(f );" returns the absolute value of SFraction f, i.e. |f |.
form SFraction Fabs(const SFraction& f );

FFCompare
function "FFCompare(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 FFCompare(const SFraction& m, const SFraction& n);