Sorry if this question is either obvious or ignorant. I am a high school student with only the computer science knowledge I have taught myself.
Calculators have a function that can convert numbers from decimal to fractional equivalents. For example, 0.5 becomes 1/2, and 0.666666667 becomes 2/3.
I doubt that the calculator has these values kept in storage to look up, so there must be an algorithm for this to be determined.
The simplest possible algorithm I can imagine would resemble this:
for (double i = 0; i < someLimit; i++){
for (double j = 0; j < someLimit; j += 1){
if (i / j == decimal){ //return these values }
}
}
This seems far too inefficient to be the algorithm used by calculators. Even on low power scientific calculators, this operation completes quickly.
How do calculators calculate the fractional equivalent of a decimal?