// implementation of fractions (rationals) in C#, to b used in F# program using System; public class rational { public int n; // numerator public int d; // denominator public rational(int a, int b) { n = a; if (b!=0) d=b; else throw new Exception("zero denominator"); reduce(); } internal static int gcd(int a, int b) // returns greatest common divisor { if (a==0) return b; else return gcd(b%a,a); } // internal means not visible outside of .dll public void reduce() // reduce the fraction { int c = gcd(n,d); n = n/c; d = d/c; } public bool Equals(rational B) // rational equality { return (n*B.d == d*B.n);; } public rational add(rational B) // non-destructive addition { int an = n*B.d + B.n*d; int ad = d * B.d; return new rational(an,ad); } public rational mult(rational B) // non-destructive multiplication { return new rational(n*B.n,d*B.d); } public void accum(rational B) // destructive addtion; { int an = n*B.d + B.n*d; int ad = d * B.d; n = an; d = ad; reduce(); }//accum public void invert() // destructive inverse { if (n!=0) { int temp = n; n = d; d = temp; } }//invert // allows Console.WriteLine to print rationals, integers printed as such. public override string ToString() { if (n%d==0) return (n/d)+""; else return n + "/" + d; } }// rational;