0

let suppose that we have a floating-point number; (always greater than 0 and less than 1); (0 < x < 1) how could I get two numbers which division of those produce a number exactly or near two the float number;

for example: if we have x = 0.4; I want to have an algorithm to find a/b (a and b are integers) which in this case are 2/5;

and suppose 6 digits similarity is sufficient;so (if y: 0.56423348256; a/b = 0.564233778888) it is okay

thanks in advance for any idea, help, name of any methods that could help

  • 1
    note that a floating-point is already a rational number because their precision is finite. In cast of binary floating-point the denominator is always a power of 2, and there's no way to represent values like 0.4 there – phuclv Jan 03 '22 at 00:28
  • [Convert a float to a rational number that is guaranteed to convert back to the original float](https://stackoverflow.com/q/66980340/995714), [How to convert floats to human-readable fractions?](https://stackoverflow.com/q/95727/995714) – phuclv Jan 03 '22 at 00:30
  • okay, but there is no way to have a pair of integer than are: abs(x- a/b) < 10e-6? – Mike Franklin Jan 03 '22 at 00:30
  • *..naming any methods...* sure, but you have to pick a programming language. – President James K. Polk Jan 03 '22 at 00:34
  • @PresidentJamesK.Polk If I know the algorithm I could turn it to python, js, or something like that. but I have no idea about the approach; – Mike Franklin Jan 03 '22 at 00:40
  • If I knew the language I might be able to give you an easy button. For example, python has a fractions module that already does what you want. – President James K. Polk Jan 03 '22 at 00:47
  • @PresidentJamesK.Polk thanks. honestly, I couldn't use libraries, I should come up with the implementation of it with a limited set of operations, but I appreciate for mentioning it, I try to look for this function and have a look at its implementation. great, thanks – Mike Franklin Jan 03 '22 at 00:54
  • 1
    You might be interested in reading https://en.wikipedia.org/wiki/Continued_fraction#Best_rational_approximations – Igor N. Jan 03 '22 at 01:14
  • Just truncate your floating-point number to 6 digits, then rationalize it. If you don't know how to truncate to 6 digits, the way that works in almost any language is to convert it to text, and truncate the string removing everything after the first six digits after the decimal points, then convert it back into a floating-point number. Then use the linked page to turn that into a ratio of integers. – RBarryYoung Jan 03 '22 at 14:56
  • @MikeFranklin: The answer at https://stackoverflow.com/a/66983369/270986 includes a description of finding the simplest fraction in an interval. If I'm understanding your question, that's exactly what you want to do here, finding a simple fraction `a/b` in the open interval `(x-1e-6, x+1e-6)`. For example, `simplest_in_interval(0.56423348256 - 1e-6, 0.56423348256 + 1e-6)` gives a result of `549/973`. – Mark Dickinson Jan 04 '22 at 10:55

0 Answers0