2

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.

5
  • Providing more contextual details about the problem you try to solve would help. Commented Apr 9 at 15:04
  • This sounds like it would be a half-way compromise to a system of rational numbers. (Some sort of language-dependent structure/class/object with 2 integers for a numerator and denominator; and a bunch of arithmetic logic involving lowest common denominators for +,-,*.....). Commented Apr 9 at 21:47
  • 1
    This sounds very similar to what Bresenham's algorithm is doing, advancing on integer points horizontally and vertically, keeping the accumulated error to know whether it needs to step up or not Commented Apr 10 at 7:39
  • 1
    You could also take a probabilistic approach and say that (x+0.3) has 2/3 chance of being rounded down to x and 1/3 chance of being rounded up to (x+1). But if you're doing that in a bank it's going to bring up all kinds of legal trouble such as justifying that your random algorithm is truly random and not biased in the bank's favour. Commented Apr 10 at 7:51
  • 1
    Why would you want to round between each transaction? Can't you just not round intermediate steps? Also this rounding error might be intended, e.g. supermarkets that have prices in cents but the final price is rounded up to a multiple of 5 cents, obviously buying 10 times a 3 cent product is then more expensive than buying 10 of it at once. Commented Apr 10 at 19:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.