Given the following test:
[Fact]
public void FactMethodName()
{
var d = 6.4133;
var actual = d.ToString("R");
Assert.Equal("6.4133", actual);
}
It is passed on x86 but not on Any CPU or x64:
Assert.Equal() Failure
Position: First difference is at position 5
Expected: 6.4133
Actual: 6.4132999999999996
The question is why that happens? Note that not all double values behave this way.
I understand about issues with floating point. No need to point me to wikipedia. No need to point out that test is incorrect — it just illustrates the problem — change it to Console.WriteLine(..); if you will.
UPDATE I removed mentions of test runners becasue those details turned out to be irrelevant.
I think the secret is in using “R” format string (see more about this)
“When a Single or Double value is formatted using this specifier, it is first tested using the general format, with 15 digits of precision for a Double and 7 digits of precision for a Single. If the value is successfully parsed back to the same numeric value, it is formatted using the general format specifier. If the value is not successfully parsed back to the same numeric value, it is formatted using 17 digits of precision for a Double and 9 digits of precision for a Single.”