I expect result
and result2
to be the same:
multiplying .1 x 10
should equal .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1
when running following code snippt of C#.
public void DoubleAddition()
{
Double x = .1;
Double result = 10 * x;
Double result2 = x + x + x + x + x + x + x + x + x + x;
Console.WriteLine("{0} - {1}", result, result2);
Console.WriteLine("{0:R} - {1:R}", result, result2);
}
But the output of Console.WriteLine("{0} - {1}", result, result2);
is 1 - 1
And When is used round-trip formater e.g. Console.WriteLine("{0:R} - {1:R}", result, result2);
then the result was 1 - 0.999999999999999989
Why is result2
not exactly is 1
when we display using round-trip formater?
What is the value stored in memory when we declared Double d = 0.1
?