On Mon, Mar 12, 2018 at 10:08 AM, Tim van Erven via Boost-users
Ilja, Paul, Degski,
Thanks for your help. I think that Ilja's comment below explains what's going on: compilers don't strictly respect IEEE floating point when they have optimizations turned on (see also the explanation here: https://stackoverflow.com/questions/7517588/different-floating-point-result-...).
@Degski: I am not actually interested in fractions. The background is that I am using the interval library to compare the numerical accuracy of various statistical algorithms in a setting where we know that some of the methods will run out of precision for large data sets, and I am getting incorrect intervals (that do not contain the correct answer) when compiling with clang, but everything works when compiling with gcc. When trying to debug this I found that the numerical results diverged right from the first interval calculations and I could not understand why. I think I have a better handle on this now.
Indeed, getting deterministic results using floating point numbers is hard (and even more so if you move between compilers, processors, architectures, etc.). If you want to achieve it, you will want a very, very, very through test suite to feel confident about the results :-) There are some good articles/books on the topic -- for instance, you can look for advice from some multiplayer games which implemented deterministic physics simulations between machines. However, note that those don't care about the error, as long as it is the same error everywhere. If you need proper results, then you can save yourself a lot of trouble using a library that allows you to use arbitrary precision numbers (specially if later on you need higher precision for something else). See for instance the GMP (Multiple Precision Arithmetic), MPFR (Multiple Precision Floating-Point Reliable) and MPC (Multiple Precision Complex) libraries, used by GCC itself. For an example of how to implement complex algorithms ensuring *exact* results by increasing the precision as needed, see the CGAL (Computational Geometry Algorithms Library). Hope that helps! Miguel