2020-10-18 03:25:41 +11:00
|
|
|
# pyRCV2: Preferential vote counting
|
2021-01-03 18:39:32 +11:00
|
|
|
# Copyright © 2020–2021 Lee Yingtong Li (RunasSudo)
|
2020-10-18 03:25:41 +11:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2021-01-03 18:39:32 +11:00
|
|
|
from pyRCV2.numbers.base import BaseNum, compatible_types
|
|
|
|
|
2020-10-18 03:25:41 +11:00
|
|
|
Big.DP = 6
|
|
|
|
|
2021-01-14 22:31:24 +11:00
|
|
|
def _fixed_set_dps(dps):
|
2020-10-18 03:25:41 +11:00
|
|
|
Big.DP = dps
|
|
|
|
|
2021-01-03 18:39:32 +11:00
|
|
|
class Fixed(BaseNum):
|
2020-10-18 03:25:41 +11:00
|
|
|
"""
|
|
|
|
Wrapper for big.js (fixed-point arithmetic)
|
|
|
|
"""
|
|
|
|
|
2021-01-03 18:39:32 +11:00
|
|
|
@classmethod
|
|
|
|
def _to_impl(cls, value):
|
|
|
|
"""Implements BaseNum._to_impl"""
|
|
|
|
return Big(value).round(Big.DP)
|
2020-10-18 03:25:41 +11:00
|
|
|
|
|
|
|
def pp(self, dp):
|
2021-01-03 18:39:32 +11:00
|
|
|
"""Implements BaseNum.pp"""
|
2020-10-18 03:25:41 +11:00
|
|
|
return self.impl.toFixed(dp)
|
|
|
|
|
2021-01-03 18:39:32 +11:00
|
|
|
@classmethod
|
|
|
|
def _add_impl(cls, i1, i2):
|
|
|
|
"""Implements BaseNum._add_impl"""
|
|
|
|
return i1.plus(i2)
|
|
|
|
@classmethod
|
|
|
|
def _sub_impl(cls, i1, i2):
|
|
|
|
"""Implements BaseNum._sub_impl"""
|
|
|
|
return i1.minus(i2)
|
|
|
|
@classmethod
|
|
|
|
def _mul_impl(cls, i1, i2):
|
|
|
|
"""Implements BaseNum._mul_impl"""
|
|
|
|
return i1.times(i2)
|
|
|
|
@classmethod
|
|
|
|
def _truediv_impl(cls, i1, i2):
|
|
|
|
"""Implements BaseNum._truediv_impl"""
|
|
|
|
return i1.div(i2)
|
2020-10-18 03:25:41 +11:00
|
|
|
|
2021-01-03 18:39:32 +11:00
|
|
|
@compatible_types
|
2020-12-23 22:36:49 +11:00
|
|
|
def __eq__(self, other):
|
2021-01-03 18:39:32 +11:00
|
|
|
"""Implements BaseNum.__eq__"""
|
2020-12-23 22:36:49 +11:00
|
|
|
return self.impl.eq(other.impl)
|
2021-01-03 18:39:32 +11:00
|
|
|
@compatible_types
|
2020-10-18 03:25:41 +11:00
|
|
|
def __gt__(self, other):
|
2021-01-03 18:39:32 +11:00
|
|
|
"""Implements BaseNum.__gt__"""
|
2020-10-18 03:25:41 +11:00
|
|
|
return self.impl.gt(other.impl)
|
2021-01-03 18:39:32 +11:00
|
|
|
@compatible_types
|
2020-10-18 03:25:41 +11:00
|
|
|
def __ge__(self, other):
|
2021-01-03 18:39:32 +11:00
|
|
|
"""Implements BaseNum.__ge__"""
|
2020-10-18 03:25:41 +11:00
|
|
|
return self.impl.gte(other.impl)
|
2021-01-03 18:39:32 +11:00
|
|
|
@compatible_types
|
2020-10-18 03:25:41 +11:00
|
|
|
def __lt__(self, other):
|
2021-01-03 18:39:32 +11:00
|
|
|
"""Implements BaseNum.__lt__"""
|
2020-10-18 03:25:41 +11:00
|
|
|
return self.impl.lt(other.impl)
|
2021-01-03 18:39:32 +11:00
|
|
|
@compatible_types
|
2020-10-18 03:25:41 +11:00
|
|
|
def __le__(self, other):
|
2021-01-03 18:39:32 +11:00
|
|
|
"""Implements BaseNum.__le__"""
|
2020-10-18 03:25:41 +11:00
|
|
|
return self.impl.lte(other.impl)
|
2020-10-18 17:54:50 +11:00
|
|
|
|
2021-01-01 22:26:57 +11:00
|
|
|
def __pow__(self, power):
|
|
|
|
"""Implements BaseNum.__pow__"""
|
|
|
|
return Fixed._from_impl(self.impl.pow(power))
|
|
|
|
|
2021-01-03 00:29:41 +11:00
|
|
|
def round(self, dps, mode):
|
2021-01-03 18:39:32 +11:00
|
|
|
"""Implements BaseNum.round"""
|
2021-01-01 22:26:57 +11:00
|
|
|
return Fixed._from_impl(self.impl.round(dps, mode))
|