public static KeyValuePair <int, int> RationalRatio(double x) { for (int val = 2; val < RATIO_MAX; val++) { // Do we acquire an integer? if (Utilities.CompareValues(x * val, Math.Floor(x * val))) { int gcd = Utilities.GCD(val, (int)Math.Round(x * val)); return(x < 1 ? new KeyValuePair <int, int>(val / gcd, (int)Math.Round(x * val) / gcd) : new KeyValuePair <int, int>((int)Math.Round(x * val) / gcd, val / gcd)); } } return(new KeyValuePair <int, int>(-1, -1)); }
public static KeyValuePair <int, int> RationalRatio(double x, double y) { for (int numer = 2; numer < RATIO_MAX; numer++) { for (int denom = 1; denom < RATIO_MAX; denom++) { if (numer != denom) { if (Utilities.CompareValues(x / y, (double)(numer) / denom)) { int gcd = GCD(numer, denom); return(numer > denom ? new KeyValuePair <int, int>(numer / gcd, denom / gcd) : new KeyValuePair <int, int>(denom / gcd, numer / gcd)); } } } } return(new KeyValuePair <int, int>(-1, -1)); }
public static bool IsInteger(double x) { return(Utilities.CompareValues(x, (int)x)); }
// -1 is an error public static int IntegerRatio(double x, double y) { return(Utilities.CompareValues(x / y, Math.Floor(x / y)) ? (int)Math.Floor(x / y) : -1); }