Skip to content

Commit

Permalink
Fix sign_type
Browse files Browse the repository at this point in the history
  • Loading branch information
kg583 committed Nov 22, 2024
1 parent c2ffa09 commit 170e063
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
8 changes: 6 additions & 2 deletions tests/tivars.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,18 @@ def test_real_pi_frac(self):
def test_real_radical(self):
test_radical = TIRealRadical.open("tests/data/var/Exact_RealRadical.8xn")

self.assertEqual(test_radical.sign_type, 2)
self.assertEqual(test_radical.sign_type, 0)
self.assertEqual(test_radical.left_scalar, 41)
self.assertEqual(test_radical.left_radicand, 789)
self.assertEqual(test_radical.right_scalar, 14)
self.assertEqual(test_radical.right_radicand, 654)
self.assertEqual(test_radical.denominator, 259)

self.assertEqual(str(test_radical), "(41√789-14√654)/259")
self.assertEqual(str(test_radical), "(41√789+14√654)/259")

test_alternate = TIRealRadical("(4√3-2√1)/2")
self.assertEqual(f"{test_alternate}", "(4√3-2)/2")
self.assertEqual(f"{test_alternate:#}", "(4√3-2√1)/2")

def complex_float_test(self, comp_type, filename, name, real_sign, real_exponent, real_mantissa,
imag_sign, imag_exponent, imag_mantissa, string, comp):
Expand Down
30 changes: 23 additions & 7 deletions tivars/types/real.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ def reduce(part):

match format_spec:
case "":
left = reduce(f"{self.left_scalar * (-1 if self.sign_type % 2 else 1)}{self.left_radicand}")
right = reduce(f"{self.right_scalar * (-1 if self.sign_type > 1 else 1)}{self.right_radicand}")
left = reduce(f"{self.signed_left_scalar}{self.left_radicand}")
right = reduce(f"{self.signed_right_scalar}{self.right_radicand}")

match left, right, self.denominator:
case "", "", _:
Expand All @@ -440,8 +440,8 @@ def reduce(part):
return string.replace("+-", "-")

case "#":
left = f"{self.left_scalar * (-1 if self.sign_type % 2 else 1)}{self.left_radicand}"
right = f"{self.right_scalar * (-1 if self.sign_type > 1 else 1)}{self.right_radicand}"
left = f"{self.signed_left_scalar}{self.left_radicand}"
right = f"{self.signed_right_scalar}{self.right_radicand}"

return f"({left}+{right})/{self.denominator}".replace("+-", "-")

Expand All @@ -452,7 +452,7 @@ def reduce(part):
def calc_data(self) -> bytes:
pass

@View(calc_data, Bits[0:4])[1:2]
@View(calc_data, Bits[4:8])[1:2]
def sign_type(self) -> int:
"""
The sign type of the real radical
Expand Down Expand Up @@ -495,13 +495,29 @@ def left_radicand(self) -> int:
def sign(self) -> int:
return -1 if self.decimal() < 0 else 1

@property
def signed_left_scalar(self) -> int:
"""
The left scalar of the real radical, with sign attached
"""

return self.left_scalar * (-1 if self.sign_type & 1 else 1)

@property
def signed_right_scalar(self) -> int:
"""
The right scalar of the real radical, with sign attached
"""

return self.right_scalar * (-1 if self.sign_type & 2 else 1)

@Loader[Decimal]
def load_decimal(self, decimal: Decimal):
raise NotImplementedError("cannot determine exact representation from decimal approximation")

def decimal(self) -> Decimal:
return (self.left_scalar * (-1 if self.sign_type % 2 else 1) * Decimal(self.left_radicand).sqrt() +
self.right_scalar * (-1 if self.sign_type > 1 else 1) * Decimal(self.right_radicand).sqrt()) \
return (self.signed_left_scalar * Decimal(self.left_radicand).sqrt() +
self.signed_right_scalar * Decimal(self.right_radicand).sqrt()) \
/ self.denominator

@Loader[str]
Expand Down

0 comments on commit 170e063

Please sign in to comment.