Fix rounding errors in unweighted inclusive Gregory mode

This commit is contained in:
RunasSudo 2020-10-19 01:40:27 +11:00
parent 356509a4fe
commit fab58ab892
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
9 changed files with 38 additions and 2 deletions

View File

@ -416,7 +416,7 @@ class BaseUIGSTVCounter(BaseSTVCounter):
for ballot in cand_ballots:
__pragma__('opov')
new_value = (Rational(ballot.value.pp(0)) * Rational(surplus.pp(0))) / Rational(total_ballots.pp(0))
new_value = (ballot.value.to_rational() * surplus.to_rational()) / total_ballots.to_rational()
self.candidates[candidate].ballots.append((ballot, new_value))
__pragma__('noopov')
@ -428,7 +428,7 @@ class BaseUIGSTVCounter(BaseSTVCounter):
for ballot in next_exhausted:
__pragma__('opov')
new_value = (Rational(ballot.value.pp(0)) * Rational(surplus.pp(0))) / Rational(total_ballots.pp(0))
new_value = (ballot.value.to_rational() * surplus.to_rational()) / total_ballots.to_rational()
__pragma__('noopov')
self.exhausted.ballots.append((ballot, new_value))

View File

@ -31,6 +31,11 @@ class Fixed:
"""Pretty print to specified number of decimal places"""
return self.impl.toFixed(dp)
def to_rational(self):
"""Convert to an instance of Rational"""
from pyRCV2.numbers import Rational
return Rational(self.impl.toString())
def __add__(self, other):
return Fixed(self.impl.plus(other.impl))
def __sub__(self, other):

View File

@ -35,6 +35,11 @@ class Fixed:
"""Pretty print to specified number of decimal places"""
return format(self.impl, '.{}f'.format(dp))
def to_rational(self):
"""Convert to an instance of Rational"""
from pyRCV2.numbers import Rational
return Rational(self.impl)
def __add__(self, other):
return Fixed(self.impl + other.impl)
def __sub__(self, other):

View File

@ -26,6 +26,11 @@ class NativeInt:
"""Pretty print to specified number of decimal places"""
return self.impl.toFixed(0)
def to_rational(self):
"""Convert to an instance of Rational"""
from pyRCV2.numbers import Rational
return Rational(self.impl)
def __add__(self, other):
return NativeInt(self.impl + other.impl)
def __sub__(self, other):

View File

@ -28,6 +28,11 @@ class NativeInt:
"""Pretty print to specified number of decimal places"""
return str(self.impl)
def to_rational(self):
"""Convert to an instance of Rational"""
from pyRCV2.numbers import Rational
return Rational(self.impl)
def __add__(self, other):
return NativeInt(self.impl + other.impl)
def __sub__(self, other):

View File

@ -26,6 +26,11 @@ class Native:
"""Pretty print to specified number of decimal places"""
return self.impl.toFixed(dp)
def to_rational(self):
"""Convert to an instance of Rational"""
from pyRCV2.numbers import Rational
return Rational(self.impl)
def __add__(self, other):
return Native(self.impl + other.impl)
def __sub__(self, other):

View File

@ -28,6 +28,11 @@ class Native:
"""Pretty print to specified number of decimal places"""
return format(self.impl, '.{}f'.format(dp))
def to_rational(self):
"""Convert to an instance of Rational"""
from pyRCV2.numbers import Rational
return Rational(self.impl)
def __add__(self, other):
return Native(self.impl + other.impl)
def __sub__(self, other):

View File

@ -29,6 +29,9 @@ class Rational:
"""
return self.impl.valueOf().toFixed(dp)
def to_rational(self):
return self
def to_num(self):
"""
Convert to an instance of Num

View File

@ -31,6 +31,9 @@ class Rational:
"""
return format(self.impl, '.{}f'.format(dp))
def to_rational(self):
return self
def to_num(self):
"""
Convert to an instance of Num