Possible Duplicate:
Floating point inaccuracy examples
double arithmetic and equality in Java
I caught this issue while trying to debug a sorting routine that checked if two values were equal. Getting the values was simply doing some addition on two double variables: 0.31 + 0.27.
When the sort compared the sum of those two against the some of another objects, whose sum also equaled 0.58, it told me the comparison was not equal. Looking at the first object's sum, I saw it was listing it as 0.58000000000000007. Wondering if it was something with my code, I created a simple console app to test it out:
static void Main(string[] args)
{
double val1 = .31;
double val2 = .27;
Console.WriteLine("Value 1: " + val1);
Console.WriteLine("Value 2: " + val2);
double added = val1 + val2;
if (!added.Equals(.58))
Console.WriteLine("Added value is not .58!");
else
Console.WriteLine("Added value is .58");
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
Ran it on my machine, and it was 0.58000000000000007 again. I had a co-worker do the same and came up with the same output.
Has anyone come across this before? We are both running 64-bit Windows 7, and this was done in C# - I haven't tested it out in other scenarios.