The following C code was compiled today on two systems with Microsoft's compiler (installed with Visual Studio 2017 Community), both of which had modern 64-bit Intel processors and were running Windows 10:
#include <math.h>
#include <stdio.h>
int main() {
int a = 64, b = 2;
double logA = log(a);
double logB = log(b);
double c = logA / logB;
printf("logA: %0.20lf\n", logA);
printf("logB: %0.20lf\n", logB);
printf("c: %0.20lf\n", c);
printf("result: %d\n", ((int)c));
return 0;
}
Let's call them "system A" and "system B" to keep things clear. Both systems printed exactly the same value for logA
and logB
:
logA: 4.15888308335967149532
logB: 0.69314718055994528623
However for c
and result
system A printed:
c: 6.00000000000000000000
result: 6
...And system B printed:
c: 5.999999999999...
result: 5
(I no longer have the exact result it produced, but sufficed to say it was ever so slightly smaller than 6.0.)
I tried compiling the same code on several other systems, including an older system running Windows 7 with VS Community 2013 as well as a mac running macOS 10.12 and Xcode 9.3.1, and their results agreed with system A, not system B.
Here's where it gets really strange:
To confound things even further, when the program was compiled two weeks ago on system B it produced same result as system A! Something in the interim changed causing the compiled program to produce a different answer.
My question is: how on earth did that happen?!
I'm aware of floating point inaccuracy and am not trying to justify or excuse the mistake of dividing the result of log(a)
and log(b)
without rounding or otherwise accounting for inaccuracy. That's clearly a bug.
But this particular bug only revealed itself in the last two weeks, and only on one system. I wasn't able to reproduce it anywhere else.
As far as I know the result of double-precision floating point math is based on the CPU, not the operating system, the compiler, the C runtime, or any other software.
Why might a system that produced 6.0000000000
two weeks ago suddenly switch to producing 5.9999999999
?
(Granted Windows 10 likes to automatically update itself silently, but the owner of system B did not manually install any updates that could easily explain this change.)
For both my own education and peace of mind I'd really love to know the answer to this.