4 ms·
In a case where it's likely to matter, you probably have a list of many numbers with diverse denominators. Certainly you can achieve mathematical precision wit
by recursive 11y ago
In a case where it's likely to matter, you probably have a list of many numbers with diverse denominators. Certainly you can achieve mathematical precision with rationals (if you can get them in the first place) but the trade-off is ending up with really huge big-ints as numerator and denominator.
- SagelyGuru 11y agoGood point. To reduce this problem you can (and should?) factorise and cancel out common primes. Just keeping to a pair of 64 bit integers will give you a pretty good accuracy. You can perform all arithmetic directly on the rationals and represent reals by the nearest rational approximation (after all, a float is an approximation too in those cases, just a poorer one). Stern-Brocot trees are a good systematic way of finding the best rational approximations and also for doing interval rational arithmetic.