I have a double input "a". My goal is to find an integer "b" such that "a * b" will produce an integer, plus some allowable amount of error. For example "100.227273 * 22 = 2205 (+ 0.000006 error)", where the answer I want to find is "22".
I have already studdied this post, but I only partially understand the top answer. I could really use some help creating an algorithm that accomplishes this task. I have some code below that works for some cases, but not all.
private int FindSmallestMultiplier(double input)
{
int numerator = 1;
int temp;
double output = input;
double invert = input;
int denominator = 1;
List<int> whole = new List<int>();
double dec = input;
int i = -1;
while (Math.Abs(Math.Round(numerator * input)-numerator*input) > 0.001)
{
i = i + 1;
//seperate out the whole and decimal portions of the input
whole.Add(Convert.ToInt32(Math.Floor(invert)));
dec = output - whole[i];
//get the decimal portion of the input, and invert
invert = 1 / dec;
//create nested fraction to determine how far off from a whole integer we are
denominator = 1;
numerator = 1;
for(int j = whole.Count-1; j >= 0; j--)
{
temp = whole[j] * denominator + numerator;
numerator = denominator;
denominator = temp;
}
}
return numerator;
}
The code above works for many input cases, such as 0.3333, 0.5. An example of where it doesn't work is 0.75, or 0.101, just to name couple out of infinity. Please help me to figure out what is wrong with my code or provide a code example that will produce the desired result. Thank you!