// Not tested or even compiled :-). Assumes you are handling sign
// in: a - the decimal to convert
// limit - the largest denominator you will allow
// out: outN - Numerator
// outD Denominator
#include <math.h>
void d2f(double a, int limit, int& outN, int& outD) {
double z;
int dPrev, d, n;
a = fabs(a);
z = a;
d = 1;
n = a;
dPrev = 0;
while (a - (double)(n/d) != 0 && z != floor(z)) {
z = 1 / (z - floor(z));
int tmp = d;
d = d * (int)floor(z) + dPrev;
if (d > limit) {
d = tmp;
break;
}
dPrev = tmp;
n = floor(a * d + 0.5);
}
outN = n;
outD = d;
}
Надеюсь, что поможет / работает: -)