Skip to content

Rounding algorithm. #214

Open
Open

Description

@PiotrCzapla

I've tried to represent Polish income taxes and social security with Numscript. They are complex, and a consistent Numscript representation should help a lot compared to a textual description.

When I was working with an LLM, it noticed that the rounding algorithm (the loop distributing cents) does not match what these tax calculations require round to the nearest grosz (round_half_up).

The LLM tried all sorts of counterproductive ideas, like using scaling to go from PLN/2 to PLN/12. This made the numscripts hard to follow and verbose, defeating the purpose of the DSL.

We haven't found a satisfactory solution, so for the time being I've let the calculations be inaccurate.

Would you entertain a solution where a different rounding algorithm can be declared through a directive or interpreter configuration?

I understand why independent rounding won't work in every case, but I don't think Poland is the only tax system that requires this kind of rounding. Having something in place would be lovely.

Here is an example where it matters:

send [PLN/2 345160] (
  source = @user
  destination = {
    9% to @health
    remaining to @user
  }
)

The exact health contribution is PLN 310.644. With round_half_up, it should be PLN 310.64, but the current algorithm produces PLN 310.65. Reversing the order of accounts fixes this.

    91% to @user
    remaining to @health

But then increasing the amount by two grosze makes the reversed version wrong.
PLN 3,451.62 × 9% = PLN 310.6458, which rounds half up to PLN 310.65. So changing the order won't solve the issue.

Proposed extension

Here is an example of how this feature could look. We could support different rounding methods with syntax similar to the current exchange syntax: <type of rounding> rounding through <account>.

send [PLN/2 345160] (
  source = @user
  half_up rounding through @user
  destination = {
    9% to @health
    remaining to @user
  }
)
// Proposed result: @health receives 31064 grosze.

Each percentage allocation would round independently. The account named in rounding through would receive any leftover amount or fund any excess. A remaining allocation would use what is left after rounding.

Here is a corner case. For PLN 10, the three rounded shares would leave one grosz:

send [PLN/2 1000] (
  source = @user
  half_up rounding through @user
  destination = {
    33.33% to @a     // 333 grosze
    33.33% to @b     // 333 grosze
    33.33% to @c     // 333 grosze
  }
)
// The rounding account receives the remaining 1 grosz.
// @user's net debit is 999 grosze.

For PLN 11, the rounded shares would exceed the amount by one grosz. The same rounding account would fund that additional grosz:

send [PLN/2 1100] (
  source = @user
  half_up rounding through @user
  destination = {
    33.33% to @a     // 367 grosze
    33.33% to @b     // 367 grosze
    33.33% to @c     // 367 grosze
  }
)
// The rounding account funds the additional 1 grosz.
// @user's net debit is 1101 grosze.

This makes equal splits fairer: each recipient gets the same rounded amount, regardless of their position in the script. The named account receives any surplus and covers any shortfall.

Let me know what you think.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions