1

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.

Community
  • 1
  • 1
Ryan Abbott
  • 5,317
  • 7
  • 31
  • 34
  • 1
    That's because of of way floating point types work. use `decimal` instead of double. – Bala R Oct 10 '11 at 23:21
  • 3
    I'm sure this has been answered, but I cannot find the dupe. This is correct behavior. I suggest you read [Goldberg's What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html). – user7116 Oct 10 '11 at 23:22
  • Too many duplicates. Long story short, not a bug. – Ignacio Vazquez-Abrams Oct 10 '11 at 23:22
  • 1
    possible duplicate of [Floating point inaccuracy examples](http://stackoverflow.com/questions/2100490/floating-point-inaccuracy-examples) or [Comparing floating point values](http://stackoverflow.com/questions/1530069/comparing-floating-point-values) – user7116 Oct 10 '11 at 23:24
  • 1
    Not to be mean, but if your coworker is that surprised, too, your company needs to invest in the basic knowledge of their employees about computers and how they perform floating point operations. – Christian Rau Oct 11 '11 at 00:22

3 Answers3

5

This has to do with the fact that .31 and .27 do not have exact binary representations. I found this article useful.

PengOne
  • 48,188
  • 17
  • 130
  • 149
1

This is a problem with floating point precision. What you could do is multiply the value by 100 (decmais accuracy of two houses) and make a cast to int or long. So the comparison run perfectly.

If you want to study in depth the subject of the search for Stallings book of computer architecture. link: http://williamstallings.com/

Tarcísio Júnior
  • 1,239
  • 7
  • 15
0

You need to define an epsilon or a greatest-acceptable-error.

double result = 0.27 + 0.31;
double expected = 0.58;
double epsilon = 0.000001;
bool areTheyEqual = Math.abs(expected - result) < epsilon
Yam Marcovic
  • 7,953
  • 1
  • 28
  • 38