Adding floats does not yield a correct result

谁说胖子不能爱 提交于 2019-12-12 04:26:00

问题


Imagine the following situation. Amounts are added to a total in a loop:

float total = 0;
float[] amounts = new float[]{...};
foreach(float amount in amounts)
{
  total += amount;
}

total, as wel as all the amounts are written to a database. When I calculate SUM(amount) in SQL, it results in a value that differs from total. Moreover, when I do the same calculation in C#, but this time adding the amounts to a value of type double,

double total = 0;
//the rest of the code is the same as above

then total represents the correct value.

Could this be caused by the difference in precision between a float and a double?

Note that it depends on the values. Most of the results of this calculation are correct.


回答1:


Yes. SQL Server's float size is variant but you likely have 8 byte floats in the database. When you use doubles ( 8 bytes ) in the C# code the results are the same, when you use floats ( 4 bytes ) they're different. double values within the range of a float will not be the same as the float values.




回答2:


Could this be caused by the difference in precision between a float and a double?

Yes. It can also sometimes simply be because not all values can be represented in an IEEE floating point; in many scenarios where you expect "exact" sums (money, etc), it is usually better to use decimal. However, that shouldn't to be interpreted to mean that decimal is "exact" - simply that the way it rounds is more conducive to how we humans think of rounding.



来源:https://stackoverflow.com/questions/17373909/adding-floats-does-not-yield-a-correct-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!