I found that running
Math.Log10(double.Epsilon)
will return about -324 on machine A, but will return -Infinity on machine B.
They originally behaved the same way by returning -324.
Both machines started out using the same OS (WinXP SP3) and .NET version (3.5 SP1). There may have been Windows updates on machine B, but otherwise no changes are known to have happened.
What could explain the difference in behavior?
More details from discussions in comments:
- Machine A CPU is a 32-bit Intel Core Duo T2500 2 GHz
- Machine B CPU is a 32-bit Intel P4 2.4 GHz
- Results collected from code running in a large application using several 3rd party components. However, same .exe and component versions are running on both machines.
- Printing
Math.Log10(double.Epsilon)in a simple console application on machine B prints-324, NOT-Infinity - The FPU control word on both machines is always
0x9001F(read with_controlfp()).
UPDATE: The last point (FPU control word) is no longer true: Using a newer version of _controlfp() revealed different control words, which explains the inconsistent behavior. (See rsbarro’s answer below for details.)
Based on the comments by @CodeInChaos and @Alexandre C, I was able to throw together some code to reproduce the issue on my PC (Win7 x64, .NET 4.0). It appears this issue is due to the denormal control that can be set using _controlfp_s. The value of double.Epsilon is the same in both cases, but the way it is evaluated changes when the denormal control is switched from SAVE to FLUSH.
Here is the sample code:
A couple things to note. First, I had to specify
CallingConvention = CallingConvention.Cdeclon theControlFPSdeclaration to avoid getting an unbalanced stack exception while debugging. Second, I had to resort to unsafe code to retrieve the value of the control word inGetCurrentControlWord(). If anyone knows of a better way to write that method, please let me know.Here is the output:
To determine what is going on with machine A and machine B, you could take the sample app above and run it on each machine. I think you’re going to find that either:
If you get a chance to try out the sample app on each machine, please update the comments with the results. I’m interested to see what happens.