2 ms·
Using rational numbers is another good idea how to combat/reduce floating point errors. For example, you can represent and calculate with 1/3 exactly, whereas i
by SagelyGuru 11y ago
Using rational numbers is another good idea how to combat/reduce floating point errors. For example, you can represent and calculate with 1/3 exactly, whereas in binary (floating point) you can not, no matter how many bits you use.
- recursive 11y agoIn 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.