问题
I have a system that is performing lots of calculations using decimals, occasionally it will add up the same numbers, but return different results, +/- 0.000000000000000000000000001
Here is a short example:
decimal a = 2.016879990455473621256359079m;
decimal b = 0.8401819425625631128956517177m;
decimal c = 0.4507062854741283043456903406m;
decimal d = 6.7922317815078349615022988627m;
decimal result1 = a + b + c + d;
decimal result2 = a + d + c + b;
Console.WriteLine((result1 == result2) ? "Same" : "DIFFERENT");
Console.WriteLine(result1);
Console.WriteLine(result2);
That outputs:
DIFFERENT
10.100000000000000000000000000
10.100000000000000000000000001
The differences are so small that there is no practical effect, but has anyone seen something like this before? I expected that when adding up the same numbers you would always get the same results.
回答1:
The entire field of Numerical analysis is devoted to studying these kind of effects and how to avoid them.
To produce the best result when summing a list of floating point numbers, first sort the list from smallest to largest, and add them up in that order.
回答2:
You might suspect a decimal type to be immune to the bane of double-users everywhere.
But because decimal has 28-29 digits of precision and your input is asking for the sum of 29 digits of precision of data, you're right at the very edge of what your data type can accurately represent.
回答3:
Read The Floating-Point Guide, Floating Point Arithmetic: Issues and Limitations, and of course What Every Computer Scientist Should Know About Floating-Point Arithmetic. Ah, also How to mess with people who’ve learned to expect rounding errors in floating-point math..
回答4:
According to MSDN, the precision of a decimal is 28-29 digits. At least one of your numbers is 29 digits, so you are likely exceeding the limit.
回答5:
Because of rounding the result of sum for multiple numbers may vary depending upon the order in which they are summed. This won't happen in mathematics but the way sum is computed in your example the order matters. result += number; sums and stores the result in result variable. At that time some of the precision is lost. However, if we do it in the same order, it would always result in same result.
Console.WriteLine(numbers.Sum()); // Always returns 9.214085249270111332166335344
Because of this many programs use Bankers rounding which produces closer results. Please know the precision is always lost. There is no way to store "accurate" floating point number in compuiter memor
Rounding
Bankers Rounding
来源:https://stackoverflow.com/questions/6221392/decimal-order-of-addition-affects-results