In C#:
Console.WriteLine(1.2d - 1.0d); produces 0.2.
In python 3:
print(1.2 - 1.0) produces 0.19999999999999996.
My goal is to write fast code in C# that produces the same floating point results as python 3. I'm obviously interested in any and all float arithmetic, not just the example above.
What is the most practical way to achieve that? Is there a library I can use?
In addition, I would like to understand, what accounts for this difference. Both representations are 64 bit, and both seem to be based on IEEE. So what is different about these implementations that make them produce different results?
References:
0.2is printed, but it's the same underlying bit pattern, as can be seen usingd = 1.2 - 1.0; print(format(d, '0.60f'))which prints0.199999999999999955591079014993738383054733276367187500000000in either version.