static inline Fractional< Natural > ReducingDivide(const Fractional< Natural > &left, const Fractional< Natural > &right) { const Natural lnrn_divisor = GreatestCommonDivisor(left.numerator, right.numerator), ldrd_divisor = GreatestCommonDivisor(left.denominator, right.denominator); const Fractional< Natural > result = { (left.numerator / lnrn_divisor) * (right.denominator / ldrd_divisor), (left.denominator / ldrd_divisor) * (right.numerator / lnrn_divisor) }; return result; }
int GreatestCommonDivisor (int i1, int i2) { if (i1 > i2) { if (i2 == 0) return (i1); return GreatestCommonDivisor (i2, i1 % i2); } else { if (i1 == 0) return (i2); return GreatestCommonDivisor (i1, i2 % i1); } }
static inline Natural LeastCommonMultiple(const Natural &a, const Natural &b) { const Natural divisor = GreatestCommonDivisor(a, b); if (a > b) return a / divisor * b; return b / divisor * a; }
/** * Compute least common multiple (lcm) of arguments \a a and \a b, the smallest * integer value that is a multiple of both \a a and \a b. * @param a First number. * @param b second number. * @return Least common multiple of values \a a and \a b. * * @note This function only works for non-negative values of \a a and \a b. */ int LeastCommonMultiple(int a, int b) { if (a == 0 || b == 0) return 0; // By definition. if (a == 1 || a == b) return b; if (b == 1) return a; return a * b / GreatestCommonDivisor(a, b); }
fraction fraction::operator /(const fraction& a) const { int tmpNumerator=numerator*a.denominator; int tmpDenominator=denominator*a.numerator; int GCD; GCD=GreatestCommonDivisor(tmpNumerator,tmpDenominator); if(GCD==1) return fraction(tmpNumerator,tmpDenominator); else return fraction(tmpNumerator/GCD,tmpDenominator/GCD); }
static inline Fractional< Natural > Reduce(const Fractional< Natural > &fraction) { Natural divisor; Fractional< Natural > result; if (fraction.numerator == 0) { result.numerator = 0; result.denominator = 1; } else { divisor = GreatestCommonDivisor(fraction.numerator, fraction.denominator); result.numerator = fraction.numerator / divisor; result.denominator = fraction.denominator / divisor; } return result; }
/* ============= R_SetSkyFrame ============== */ void R_SetSkyFrame (void) { int g, s1, s2; float temp; skyspeed = iskyspeed; skyspeed2 = iskyspeed2; g = GreatestCommonDivisor (iskyspeed, iskyspeed2); s1 = iskyspeed / g; s2 = iskyspeed2 / g; temp = SKYSIZE * s1 * s2; skytime = cl.time - ((int)(cl.time / temp) * temp); }
fraction fraction::operator -(const fraction& a) const { int tmpNumerator; int tmpDenominator; int GCD; int cnt1,cnt2; tmpDenominator=LeastCommonMultiple(denominator,a.denominator); cnt1=tmpDenominator/denominator; cnt2=tmpDenominator/a.denominator; tmpNumerator=cnt1*numerator-cnt2*a.numerator; GCD=GreatestCommonDivisor(tmpNumerator,tmpDenominator); if(GCD==1) return fraction(tmpNumerator,tmpDenominator); else return fraction(tmpNumerator/GCD,tmpDenominator/GCD); }
fraction::fraction(const int a,const int b) { if(b==0) exit(0); int GCD=GreatestCommonDivisor(a,b); if(GCD==1) { numerator=a; denominator=b; } else { numerator=a/GCD; denominator=b/GCD; } }
void R_SetSkyFrameFPM (void) { int g, s1, s2; float temp; skyspeed = (float)iskyspeed; skyspeed2 = (float)iskyspeed2; g = GreatestCommonDivisor (iskyspeed, iskyspeed2); s1 = iskyspeed / g; s2 = iskyspeed2 / g; temp = (float)(SKYSIZE * s1 * s2); skytime = (float)(clFPM.time - ((int)(clFPM.time / temp) * temp)); r_skymade = 0; }
void Rational::Reduce() { long GCD = GreatestCommonDivisor(numerator, denominator); numerator = numerator / GCD; denominator = denominator / GCD; }