Re:Improving Floating Point Accuracy: A Look at Sums
by SagelyGuru on Monday October 19, 2015 @08:22AM
(#10412402)
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.
[ Parent
| on HN ]
Re:Improving Floating Point Accuracy: A Look at Sums
by recursive on Monday October 19, 2015 @01:59PM
(#10414509)
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.
Re:Improving Floating Point Accuracy: A Look at Sums
by SagelyGuru on Monday October 19, 2015 @03:49PM
(#10415216)
Good 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.