Skip to content

Commit

Permalink
Fix BasedReal resize mixed radix (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
legau authored Jun 30, 2022
1 parent 72debe0 commit 0c86467
Show file tree
Hide file tree
Showing 7 changed files with 326 additions and 285 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.2
rev: 4.0.1
hooks:
- id: flake8
exclude: docs
Expand All @@ -16,12 +16,12 @@ repos:
args: ["--profile", "black", "--filter-files"]

- repo: https://github.com/psf/black
rev: 22.3.0
rev: 22.6.0
hooks:
- id: black

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.950
rev: v0.960
hooks:
- id: mypy
files: kanon
Expand Down
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
0.6.5
_____

*Bug Fixes*

- radices : Fix `BasedReal.resize` sometimes not working on mixed radix `BasedReal`

0.6.4
_____

Expand Down
13 changes: 9 additions & 4 deletions kanon/units/precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ def _get_significant(self: TPreciseNumber, other: "PreciseNumber") -> int:
@_with_context_precision(symbol="+")
def __add__(self: TPreciseNumber, other):
if f := get_context().add:
return f(self, other)
with set_precision(**asdict(PrecisionContext())):
return f(self, other)
return self._add(other)

@abc.abstractmethod
Expand All @@ -189,7 +190,8 @@ def _add(self: TPreciseNumber, other: "PreciseNumber") -> TPreciseNumber:
@_with_context_precision(symbol="-")
def __sub__(self: TPreciseNumber, other):
if f := get_context().sub:
return f(self, other)
with set_precision(**asdict(PrecisionContext())):
return f(self, other)
return self._sub(other)

@abc.abstractmethod
Expand All @@ -199,7 +201,8 @@ def _sub(self: TPreciseNumber, other: "PreciseNumber") -> TPreciseNumber:
@_with_context_precision(symbol="*")
def __mul__(self: TPreciseNumber, other):
if f := get_context().mul:
return f(self, other)
with set_precision(**asdict(PrecisionContext())):
return f(self, other)
return self._mul(other)

@abc.abstractmethod
Expand All @@ -209,7 +212,8 @@ def _mul(self: TPreciseNumber, other: "PreciseNumber") -> TPreciseNumber:
@_with_context_precision(symbol="/")
def __truediv__(self: TPreciseNumber, other):
if f := get_context().div:
return f(self, other)
with set_precision(**asdict(PrecisionContext())):
return f(self, other)
return self._truediv(other)

@abc.abstractmethod
Expand Down Expand Up @@ -425,6 +429,7 @@ def set_precision(
sub: Union[CustomArithmeticAlgorithm, Literal[False]] = False,
mul: Union[CustomArithmeticAlgorithm, Literal[False]] = False,
div: Union[CustomArithmeticAlgorithm, Literal[False]] = False,
**kwargs,
):
"""Mutates the current `PrecisionContext` with the specified rules."""
ctx = get_context()
Expand Down
11 changes: 7 additions & 4 deletions kanon/units/radices.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,14 +582,17 @@ def resize(self: TBasedReal, significant: int) -> TBasedReal:
sign=self.sign,
)
if significant >= 0:
remainder = type(self)(
(), self.right[significant:], remainder=self.remainder
)
remainder = Decimal(0)
factor = Decimal(1)
for idx, number in enumerate(self.right[significant:]):
factor *= Decimal(self.base[1][significant + idx])
remainder += Decimal(number) / factor
remainder += self.remainder / factor

return type(self)(
self.left,
self.right[:significant],
remainder=remainder.decimal,
remainder=remainder,
sign=self.sign,
)

Expand Down
4 changes: 4 additions & 0 deletions kanon/units/tests/test_basedreal.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from hypothesis.core import given

from kanon.units import BasedReal, Historical, Sexagesimal
from kanon.units.definitions import Temporal
from kanon.units.radices import (
EmptyStringException,
IllegalBaseValueError,
Expand Down Expand Up @@ -184,6 +185,9 @@ def test_misc():

assert divmod(Sexagesimal(5), 3) == divmod(5, 3)

assert Temporal("1;2,44,44,44,44").resize(2).truncate() == Temporal("1;2,44")
assert Temporal("1;2,44,44,44,44").resize(0).truncate() == 1


def test_shift():
s = Sexagesimal("20, 1, 2, 30; 0")
Expand Down
6 changes: 5 additions & 1 deletion kanon/units/tests/test_precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def div(a: PreciseNumber, b: PreciseNumber):

with set_precision(add=add, sub=sub, div=div, mul=mul):
assert Sexagesimal(1) + Sexagesimal(1) == 3
assert Sexagesimal(1) - Sexagesimal(1) == 0
assert Sexagesimal(1) - Sexagesimal(1) == -1
assert Sexagesimal(1) * Sexagesimal(1) == 2
assert Sexagesimal(1) / Sexagesimal(1) == 5

Expand Down Expand Up @@ -215,6 +215,10 @@ def func(a, b):
func(Sexagesimal(1), Sexagesimal(2))
assert func(1, 2) == 1

def test_test(self):
with set_precision(tmode=TruncatureMode.ROUND, pmode=1):
assert Sexagesimal("1;50") + Sexagesimal("2;5,30") == Sexagesimal("3;56")

@classmethod
def teardown_class(cls):
get_context().mutate(**cls.context)
Loading

0 comments on commit 0c86467

Please sign in to comment.