Coefficient::Coefficient(QString s) : fractionRep{s} { getNumer(fractionRep, numerInt, hasDenom); if(hasDenom) getDenom(fractionRep, denomInt); }
void fraction::calcOverflowPts() const { if(getNumer() == 0) interimMultOverflowPt = I64_MAX; else interimMultOverflowPt = I64_MAX / getNumer(); fraction recip; recip.setRaw(getDenom(), getNumer()); if(recip.getDenom()==0 || recip.getD() > 1.0) finalMultOverflowPt = I64_MAX; else finalMultOverflowPt = recip.multNoInterimOverflow(I64_MAX); }
int64_t fraction::multReturnInt64(int64_t b) const { int64_t ret = 0; if(b < getInterimMultOverflowPt()) { // b < interimMultOverflowPt fraction fres(b * getNumer(), getDenom()); ret = fres.getI(); } else if(b <= getFinalMultOverflowPt()) { // interimMultOverflowPt <= b <= finalMultInterimPt ret = multNoInterimOverflow(b); } else { // finalMultInterimPt < b cerr << "fraction::multReturnInt64- a final overflow has occurred\n"; return I64_MAX; } return ret; }
rational rational::operator /(const rational& second) const { return rational(getNum() * second.getDenom(), getDenom() * second.getNum()); }
rational rational::operator -(const rational& second) const { int n1 = getNum(), d1 = getDenom(), n2 = second.getNum(), d2 = second.getDenom(); int denom = d1 * d2 / gcd(d1, d2); int num = n1 * denom / d1 - n2 * denom / d2; return rational(num, denom); }