We have two integers, $n$ and $d$. They are coprime (the only positive integer that is a divisor of both of them is $1$). They may be implemented as something that fits in a machine register, or they may be "big integers". Together, $n$ and $d$ represent the numerator and denominator of a rational number.
The goal is to convert the rational number into the corresponding floating-point number in a fixed-width binary floating-point format (for example, 32-bit binary IEEE 754 floating-point, known as
float in the C programming language).
As long as using "big floats" (arbitrary-precision floating-point, for example with the MPFR C library) for implementing the algorithm is allowed, the solution is easy:
- convert both $n$ and $d$ to big floats with a precision that is high enough so both integers would be exactly represented
- divide the big float numerator with the big float denominator, yielding a big float number
- convert the above big float number to the required fixed-precision floating-point format
I wonder, however, what would a more from-first-principles solution look like, one that wouldn't use big floats?