private void Normalize() { if (this.Numerator == 0) { this.Denominator = 1; } else { var g = NormalizedRational.GCD(this.Numerator, this.Denominator); this.Numerator = this.Numerator / g; this.Denominator = this.Denominator / g; } }
static void Main(string[] args) { var r1 = new Rational(10, 5); Console.WriteLine("r1 truncated = {0}", r1.Truncate()); var r2 = new PositiveDenominatorRational(5, 3); r2.Divide(-5); var r3 = new PositiveDenominatorRational(5, 0); var r4 = new NormalizedRational(10, 2); r4.Divide(0); Console.WriteLine("r1 truncated = {0}", r3.Truncate()); }
static void Main(string[] args) { var r1 = new Rational(10, 5); Console.WriteLine("r1 truncated = {0}", r1.Truncate()); var r2 = new PositiveDenominatorRational(5, 3); r2.Divide(-5); var r3 = new PositiveDenominatorRational(5, 0); var r4 = new NormalizedRational(10, 2); r4.Divide(0); Console.WriteLine("r1 truncated = {0}", r3.Truncate()); }
private static int GCD(int x, int y) { // find greatest common divisor of x and y int ans; int z; if (x < y) { ans = NormalizedRational.GCD(y, x); } else if (x % y == 0) { ans = y; } else { z = x % y; ans = NormalizedRational.GCD(y, z); } return(ans); }
private void NormalizedInvariant() { Contract.Invariant(NormalizedRational.GCD(this.Numerator, this.Denominator) == 1); }