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 amount
s 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.