"Compressing" rationals given error bounds - Computer Science Stack Exchange most recent 30 from cs.stackexchange.com 2020-01-23T15:20:52Z https://cs.stackexchange.com/feeds/question/45076 https://creativecommons.org/licenses/by-sa/4.0/rdf https://cs.stackexchange.com/q/45076 4 "Compressing" rationals given error bounds Jake https://cs.stackexchange.com/users/19265 2015-08-07T01:33:55Z 2015-08-08T03:20:22Z <p>I'm working on implementing some exact real arithmetic operations for fun. I've got the rough outline of how I want to do things as well and have figured out how to write most of the important algorithms (though I have not properly implemented them just yet).</p> <p>Going against the "pre-mature optimization is bad, m'kay" philosophy I am wondering about a few things. So reducing fractions is one way to reduce the size of rationals, sure, but it isn't perfect. If you knew you only had to be within a certain range you could do better by changing the value. For instance say I need to add $1$ and $\frac{1}{8}$ to with in $\frac{1}{2}$. I don't need to full precision, I can just return 0 in fact. More beneficially say I had something like $\frac{2^{16}+1}{2^{32}}$, if I only need say something like $\frac{1}{2^{15}}$ precision, I can approximate this rational as $\frac{2^{16}}{2^{32}}$ and then reduce that to $\frac{1}{2^{16}}$.</p> <p>This are some contrived cases but I think there is a generally useful optimization to be done here. Are there algorithms which, given an error bound, can find the smallest (in bits) rational number within the specified bounds of another given rational number?</p> <p>Most of my algorithms tend to produce rationals with power of two denominators so I am particularly interested in that case. </p> https://cs.stackexchange.com/questions/45076/-/45079#45079 4 Answer by D.W. for "Compressing" rationals given error bounds D.W. https://cs.stackexchange.com/users/755 2015-08-07T05:28:07Z 2015-08-08T03:20:22Z <p><a href="https://en.wikipedia.org/wiki/Continued_fraction" rel="nofollow noreferrer">Continued fractions</a> are an efficient way to enumerate rational numbers that are a good approximation of your number $x$. In particular, given a real number $x$, you can generate a sequence of truncated continued fractions $a_i/b_i$, where each rational number $a_i/b_i$ is a better approximation to $x$ than any other fraction whose denominator is $\le b_i$. So, a reasonably good algorithm for your problem is to compute the sequence of truncated continued fractions, and keep the last one whose denominator is smaller than your bound. This doesn't find the best rational approximation for a given number of bits, but it does give you a best rational approximation for a given upper bound on the size of the denominator.</p> <p>To compare to <a href="https://cs.stackexchange.com/a/45080/755">KWillets' excellent answer</a>: KWillets' method takes something like $\Theta(a+b)$ steps, but finds the optimal solution (measured by bits). Continued fractions takes something like $\Theta(\lg b)$ steps, but isn't guaranteed to find the optimal answer (measured by number of bits); it's only guaranteed to find the optimal answer if you measure by the size of the denominator. KWillets' method seems better if $a,b$ are not too large. However, if they are very large, then a continued fractions-based method might become more attractive.</p> https://cs.stackexchange.com/questions/45076/-/45080#45080 3 Answer by KWillets for "Compressing" rationals given error bounds KWillets https://cs.stackexchange.com/users/31112 2015-08-07T06:46:55Z 2015-08-07T06:46:55Z <p>A brute force solution is to trace the line ay=bx in integer coordinates from the origin to (a,b). Any pair with a smaller number of bits will be along that line segment. This method allows finding either the minimum distance or the smallest (x,y) within the error bounds. It requires o(a+b) steps, and there's plenty of redundancy possible, since eg both (x,y) and (kx,ky) might be found for k >1. </p> <p>Tracing the line refers to iteratively moving either up or right according to which move minimizes distance to the line; it's a common method for graphics. </p>