Avoid excessive rounding for unweighted inclusive Gregory in non-integer modes

This commit is contained in:
RunasSudo 2020-10-19 00:58:06 +11:00
parent f8ee464421
commit 77d773605a
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
4 changed files with 26 additions and 5 deletions

View File

@ -39,7 +39,7 @@
<select id="selPreset" onchange="changePreset()">
<option value="scottish" selected>Scottish STV</option>
<option value="stvc">pyRCV STV-C</option>
<option value="senate">Australian Senate STV</option>
<!--<option value="senate">Australian Senate STV</option>-->
<option value="wright">Wright STV</option>
</select>
</label>

View File

@ -458,9 +458,9 @@ class BaseUIGSTVCounter(BaseSTVCounter):
# Credit votes
for candidate, cand_votes in next_preferences.items():
__pragma__('opov')
self.candidates[candidate].transfers += Num(cand_votes.__floor__().pp(0))
self.candidates[candidate].transfers += cand_votes.to_num()
__pragma__('noopov')
__pragma__('opov')
self.exhausted.transfers += Num(next_exhausted.__floor__().pp(0))
self.exhausted.transfers += next_exhausted.to_num()
__pragma__('noopov')

View File

@ -23,9 +23,21 @@ class Rational:
self.impl = bigRat(val)
def pp(self, dp):
"""Pretty print to specified number of decimal places"""
"""
Pretty print to specified number of decimal places
This will fail for numbers which cannot be represented as a JavaScript number
"""
return self.impl.valueOf().toFixed(dp)
def to_num(self):
"""
Convert to an instance of Num
"""
from pyRCV2.numbers import Num
__pragma__('opov')
return Num(self.impl.numerator.toString()) / Num(self.impl.denominator.toString())
__pragma__('noopov')
def __add__(self, other):
return Rational(self.impl.add(other.impl))
def __sub__(self, other):

View File

@ -26,9 +26,18 @@ class Rational:
self.impl = Fraction(val)
def pp(self, dp):
"""Pretty print to specified number of decimal places"""
"""
Pretty print to specified number of decimal places
"""
return format(self.impl, '.{}f'.format(dp))
def to_num(self):
"""
Convert to an instance of Num
"""
from pyRCV2.numbers import Num
return Num(self.impl.numerator) / Num(self.impl.denominator)
def __add__(self, other):
return Rational(self.impl + other.impl)
def __sub__(self, other):