I am working with a lot of mathematical operations, mostly with divisions, and its really important that after all calculations has been done, all the numbers match with the initial state. For example I am testing the following code for doubles in C#:
double total = 1000;
double numberOfParts = 6;
var sumOfResults = 0.0;
for (var i = 0; i < numberOfParts; i++)
{
var result = total / numberOfParts;
sumOfResults += result;
}
This code gives a result of sumOfResults: 999.9999999999999, and i expect 1000. The similar problem happens when using decimals, just it's more precise:
decimal total = 1000m;
decimal numberOfParts = 6m;
var sumOfResults = decimal.Zero;
for (var i = 0; i < numberOfParts; i++)
{
var result = total / numberOfParts;
sumOfResults += result;
}
Expected sumOfResults to be 1000M, but found 1000.0000000000000000000000001M. So even here when i need to compare with the initial state of 1000, I will never have the same state as before the division of numbers.
I am aware of the Numerical Analysis field and everything, but is there some library, that will help me to get the exact number of 1000 after sum of all the division results?