Fix rounding errors in unweighted inclusive Gregory mode
This commit is contained in:
parent
356509a4fe
commit
fab58ab892
@ -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))
|
||||
|
||||
|
@ -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):
|
||||
|
@ -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):
|
||||
|
@ -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):
|
||||
|
@ -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):
|
||||
|
@ -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):
|
||||
|
@ -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):
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user