0

Possible Duplicate:
problem in comparing double values in C#
Double variable keeping wrong value

I'm storing some values in a table in the MySQL database, the column type is double(8,2). I'm fetching that data and then I'm calling the LINQ's Sum() extension method. That operation returns e.g 384.18. Then I get the 2nd value (in the same way) and I get e.g 384.17.

When I do some math, the result is:

384.18 - 384.17 = 0.0099999999999909051

why not 0.01 ? I know I can use the Math.Round() method but I'm wondering why I get that result.

edit

Comparing double values in C#

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tony
  • 12,405
  • 36
  • 126
  • 226
  • 4
    (Duplicate of many, many other questions, of course.) – Jon Skeet May 18 '12 at 19:58
  • Just take a peak under the `Related` links to the right and you'll find a bunch of questions with the same problem ([for example](http://stackoverflow.com/a/1398776/95573)) – SwDevMan81 May 18 '12 at 19:59
  • 1
    And how about `654321.18 - 654321.17`, it would be even worse. As a rule of thumb, `double` has a precision of approximately 16 significant digits. But when you throw away 7 significant places (like in my example), there are only 9 digits' precision left. But as a standard, a `double` is shown to the user with 15 digits. So some of them have to be wrong in cases like this. That will always happen. – Jeppe Stig Nielsen May 18 '12 at 20:12
  • @JonSkeet, SwDevMan81 and others: This question seemed to be about the loss of precision that results when you subtract two numbers that are very nearly identical. What other question is about that? You did not link one. Is this supposed to be a duplicate of questions about completely different arithmetic situations where precision is an issue? Of course, if you just say "All questions about precision are really the same, namely 'why does my number format not possess infinite precision?'", I can see that it's a duplicate. – Jeppe Stig Nielsen May 18 '12 at 20:52
  • @JeppeStigNielsen: It's not a matter of infinite precision - it's about `double` not representing *decimal* values accurately. There are hundreds of questions like that - I linked to one which was somewhat related, but fundamentally the OP needs to learn about binary floating point types. – Jon Skeet May 18 '12 at 21:03
  • @JeppeStigNielsen: My answer here probably gives more useful information though: http://stackoverflow.com/questions/618535/what-is-the-difference-between-decimal-float-and-double-in-c/618596#618596 – Jon Skeet May 18 '12 at 21:05
  • @JonSkeet Yeah, that's a very nice answer you linked there. But even with `Decimal`s, where the string representation that is presented to the user, gives a good idea of the number actually stored in memory, we can have this kind of precision loss. Take this example: `decimal dm1 = 98765432109876543211m / 3m; decimal dm2 = 98765432109876543210m / 3m;` Now calculating `dm1 - dm2` gives `0.333333333`. Rhetorical: "We can all agree the value is one third. So why are there only nine digits 3? I thought a `Decimal` had 28 significant figures?" (I know the answer, of course.) – Jeppe Stig Nielsen May 18 '12 at 22:13
  • @JeppeStigNielsen: I think the OP is a *long* way from understanding that. Understanding just why his subtraction doesn't work would be a good starting point. – Jon Skeet May 18 '12 at 22:19

1 Answers1

0

Double does not have the precision you need. Use decimal in C# and a more precise numeric type in SQL - SQL-Server would be NUMERIC(x,y). Not sure of MySql equiv.

n8wrl
  • 19,439
  • 4
  • 63
  • 103
  • 1
    `Double` does have the precision he needs. He needs SQL column type `double(8, 2)`. The `System.Double` of .NET has far better precision. – Jeppe Stig Nielsen May 18 '12 at 20:03