Skip to content

Commit

Permalink
Estimate digits
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdrozd committed Aug 30, 2023
1 parent 633a045 commit b754388
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
5 changes: 5 additions & 0 deletions test/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ def test_num(self):
).run().marks

print(marks)
assert not isinstance(marks, int)
print(marks.estimate())
more_marks = marks * 3 // 2
assert not isinstance(more_marks, int)
print(more_marks.estimate())

self.assertGreater(marks, 5)
self.assertGreaterEqual(marks, 5)
Expand Down
14 changes: 7 additions & 7 deletions test/test_turing.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,6 @@ def _test_prover_est(self, prog_data: ProverEst):
if not isinstance(result, int):
print(f'--> {result}')

result = int(result)

if not isinstance(macro := self.machine.program, str):
result *= macro.cells # type: ignore[attr-defined]

Expand All @@ -452,16 +450,18 @@ def _test_prover_est(self, prog_data: ProverEst):

if exp < 100_000:
self.assert_close(
result / 10 ** exp,
int(result) / 10 ** exp,
digits,
rel_tol = .54,
)
else:
assert isinstance(result, int)
magnitude = (
int(log10(result))
if isinstance(result, int) else
result.estimate()
)

self.assertEqual(
exp,
int(log10(result)))
self.assertEqual(exp, magnitude)

if result < 5:
self.assert_could_blank(prog)
Expand Down
31 changes: 31 additions & 0 deletions tm/num.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ def __int__(self) -> int:
int(self.r),
)

@abstractmethod
def estimate(self) -> int: ...

def estimate_l(self) -> float:
return (
l.estimate()
if isinstance(l := self.l, Num) else
0
if l < 1 else
log10(l)
)

def estimate_r(self) -> float:
return (
log10(r)
if isinstance(r := self.r, int) else
r.estimate()
)

@abstractmethod
def __neg__(self) -> Count: ...

Expand Down Expand Up @@ -126,6 +145,9 @@ class Add(Num):
def __init__(self, l: Count, r: Num):
super().__init__(l, r)

def estimate(self) -> int:
return round(max(self.estimate_l(), self.estimate_r()))

def __mod__(self, other: int) -> int:
return ((self.l % other) + (self.r % other)) % other

Expand Down Expand Up @@ -171,6 +193,9 @@ class Mul(Num):
def __init__(self, l: Count, r: Num):
super().__init__(l, r)

def estimate(self) -> int:
return round(self.estimate_l() + self.estimate_r())

def __neg__(self) -> Count:
return -(self.l) * self.r

Expand Down Expand Up @@ -220,6 +245,9 @@ def __mod__(self, other: int) -> int:

return ((self.l % other) * (inv % other)) % other

def estimate(self) -> int:
return round(self.estimate_l() - self.estimate_r())

def __rmul__(self, other: Count) -> Count:
if (isinstance(other, Num)
or isinstance(self.r, Num)
Expand All @@ -246,6 +274,9 @@ def __init__(self, l: Count, r: Count):

super().__init__(l, r)

def estimate(self) -> int:
return round(self.estimate_l() * 10 ** self.estimate_r())

def __neg__(self) -> Count:
return (
Exp(-(self.l), self.r)
Expand Down

0 comments on commit b754388

Please sign in to comment.