// Apply power to the extent that it works out to an integer ratio. // This CRatio is always an integer, so the denominator is always 1 in this application. // Return true if it works out evenly (set ratioPower = 1/1) // else return false and set ratioPower to the unapplied portion. // Ignore overflow cases because they will not come up in this application. public bool ApplyPower(ref CRatio ratioPower) { bool bSuccess = false; ratioPower.NormalizeSign(); if (this._numerator > 256 || ratioPower.Numerator > 16) { // Avoid overflow bSuccess = false; } if (ratioPower.Numerator == 0) { Set(1); bSuccess = true; } else if (ratioPower.Numerator > 0) { bSuccess = false; // Apply power numerator int intOrigNumerator = _numerator; for (int intPower = ratioPower.Numerator - 1; intPower > 0; --intPower) { bSuccess = true; _numerator *= intOrigNumerator; } ratioPower.SetNumerator(1); // Apply power denominator. if (ratioPower.Denominator != 1) { if (_numerator == 1) { bSuccess = true; ratioPower.Set(1); } else { // calculate root CPrimeFactorList thisPrimeFactorList = new CPrimeFactorList(_numerator); bSuccess = thisPrimeFactorList.ApplyRoot(ratioPower.Denominator); if (bSuccess) { _numerator = thisPrimeFactorList.Value; ratioPower.Set(1); // power = 1/1 } } } } else // negative numerator { bSuccess = false; } return(bSuccess); }
// Reduce numerator and denominator by lowest common public void Normalize() { NormalizeSign(); if (_numerator == _denominator) { if (_numerator != 1) { Set(1); } } else { CPrimeFactorList factorListNumerator = new CPrimeFactorList(_numerator); factorListNumerator.CalcLowestCommon(ref _denominator); _numerator = factorListNumerator.Value; } }