From f534c983cd368a85a40a6061e74a93cfb9b4dac4 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Thu, 14 Jan 2021 22:31:24 +1100 Subject: [PATCH] Implement guarded fixed-point arithmetic --- README.md | 2 +- docs/options.md | 1 + html/index.html | 1 + html/index.js | 2 +- html/worker.js | 4 ++ pyRCV2/cli/stv.py | 5 +- pyRCV2/method/meek.py | 9 +++- pyRCV2/numbers/__init__.py | 24 +++++++++- pyRCV2/numbers/base.py | 4 ++ pyRCV2/numbers/fixed_js.py | 5 +- pyRCV2/numbers/fixed_py.py | 11 ++--- pyRCV2/numbers/gfixed_js.py | 93 +++++++++++++++++++++++++++++++++++++ pyRCV2/numbers/gfixed_py.py | 92 ++++++++++++++++++++++++++++++++++++ tests/test_meekm.py | 4 +- tests/test_numbers.py | 6 +++ 15 files changed, 243 insertions(+), 20 deletions(-) create mode 100644 pyRCV2/numbers/gfixed_js.py create mode 100644 pyRCV2/numbers/gfixed_py.py diff --git a/README.md b/README.md index 47d5d26..b06ccb5 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ pyRCV2 accepts data in the [BLT file format](http://www.dia.govt.nz/diawebsite.N pyRCV2 is highly customisable, including options for: * different quotas and quota rules (e.g. exact Droop, Hare) or progressively reducing quota -* calculations using fixed-point arithmetic or exact rational numbers +* calculations using fixed-point arithmetic, guarded fixed-point ([quasi-exact](http://www.votingmatters.org.uk/ISSUE24/I24P2.pdf)) or exact rational numbers * different tie breaking rules (backwards, random, manual) with auditable deterministic random number generation * extensible API for other counting methods diff --git a/docs/options.md b/docs/options.md index 0bb394e..06505d8 100644 --- a/docs/options.md +++ b/docs/options.md @@ -18,6 +18,7 @@ This functionality is not available on the Python command line. This dropdown allows you to select how numbers (vote totals, etc.) are represented internally in memory. The options are: * *Fixed*: Numbers are represented as fixed-precision decimals, up to a certain number of decimal places (default: 5). +* *Fixed (guarded)*: Numbers are represented as fixed-precision decimals with ‘guard digits’ – also known as [‘quasi-exact’ arithmetic](http://www.votingmatters.org.uk/ISSUE24/I24P2.pdf). If *n* decimal places are requested, numbers are represented up to 2*n* decimal places, and two values are considered equal if the absolute difference is less than (10^-*n*)/2. * *Rational*: Numbers are represented exactly as fractions, resulting in the elimination of rounding error, but increasing computational complexity when the number of surplus transfers is very large. * *Native*: Numbers are represented as native integers or floating-point numbers. This is fast, but not recommended as unexpectedly large rounding errors may be introduced in some circumstances. diff --git a/html/index.html b/html/index.html index f3aa047..80ea529 100644 --- a/html/index.html +++ b/html/index.html @@ -139,6 +139,7 @@ +