Given two arbitrarily precise decimal numbers 0 ≤ x < y ≤ 1, compute the shortest (in digits) binary number b such that x ≤ b < y.
Output the binary digits of b after the binary point as an array or a string of zeroes and ones. Note that the empty array means 0.0, by virtue of deleting trailing zeroes. This also makes sure that there is a unique correct answer for any range.
If you are unfamiliar with binary fractional numbers, they work just like decimal numbers:
Base 10 0.625 = 0.6 + 0.02 + 0.005 = 6 x 10^-1 + 2 x 10^-2 + 5 x 10^-3
Base 2 0.101 = 0.1 + 0.00 + 0.001 = 1 x 2^-1 + 0 x 2^-2 + 1 x 2^-3
| | |
v v v
Base 10 0.625 = 0.5 + 0 + 0.125
Built-ins that trivialize this problem are disallowed.
Examples:
0.0, 1.0 -> ""
0.1, 1.0 -> "1"
0.5, 0.6 -> "1"
0.4, 0.5 -> "0111"
Shortest code wins.
(0.98983459823945792125172638374187268447126843298479182647, 0.98983459823945792125172638374187268447126843298479182648)
? Also, test cases would be helpful. \$\endgroup\$