Mathematical functions in the standard library are non-deterministic?
This is something I learned today the hard way. See here: https://doc.rust-lang.org/std/primitive.f64.html#method.ln
As stated in the docs, this method is non-deterministic, which apparently means its result can vary by platform, Rust version, or even within the same execution from one invocation to the next.
In fact, it’s not just the logarithm, it’s many mathematical functions in the standard library! As a physicist by trade, this is wild to me, maybe it’s obvious for computer scientists, but this really isn’t something I would’ve expected.
The issue I’m having is that this actually impacts the results of a project I’m working on. Essentially I have a large dataset, I compute some value for every event in the dataset (a likelihood) and then minimize the negative log of the likelihood. This is done because the product or likelihoods isn’t particularly stable on floating point machines, so we take the sum of logs of likelihoods instead. Well if the result of this logarithm can vary in the last few bits (even in the 30th decimal place!) I’ve found it can impact the sum of likelihoods by a numerically significant amount (around the 11th decimal place for the smallest datasets, worse for larger ones).
I figure the implementation is meant to be faster than some Taylor series or bilinear expansion, but I’m working with data that requires precision. I thought libm would solve this, but my tests are indicating that it is also non-deterministic (is it?). Do people have recommendations for math libraries which guarantee determinism or a solution to handle the lack thereof in the standard library?
Edit: I’m learning so much, thank you everyone!