It is well-known that integer division can result in errors, no matter which rounding method is used (we use Floor rounding below as example).
Moreover, if division is calculated multiple times (with the same divisor), it may lead to even greater errors. For example, 6/2=3. However, if 6 is split into 1+5, it becomes two divisions: 1/2=0 and 5/2=2. Adding the two results 0+2 gives 2, which is not equal to 3.
I was thinking that the error from integer division rounding could be accumulated and applied to the next division (with the same divisor), which might reduce the error.
The function prototype might look like this:
int divide(int a, int b, int *cum_error);
Using the example above:
int cum_error = 0;
int r1 = divide(1, 2, &cum_error); // After the function call, cum_error = 1
int r2 = divide(5, 2, &cum_error); // r2 = (5+1)/2 = 3
I would like to know if there are any existing algorithms or articles that discuss this method. I had planned to implement it myself, but found that dealing with negative numbers and overflow makes it quite complex.
=== EDIT ===
Supplement: Application scenario.
First, there is the division. In a trading system that charges a fee = quantity * fee_rate. All values in this system are represented by integers, so the fee rate is also expressed as two integers. For example, 0.03 is represented as (3, 100). Therefore, the calculation becomes:
fee = quantity * 3 / 100
Here, integer division comes into play.
Then there is the error. For example, if a user trades a quantity of 10,000, multiplying it by the fee rate of 0.03 yields 300. However, if the user breaks down the quantity of 10,000 into 1,000 transactions of 10 each, then each transaction multiplied by the fee rate of 0.03 results in 0.3. After applying the floor rounding, it becomes 0, and for 1,000 transactions, it is 1000*0=0. If ceiling rounding is applied instead, it becomes 1, and 1000*1=1000. Both results are significantly different from the original 300.
If the approach I described above is used, where the error is accumulated, this problem can be resolved.