Rational numeral — concrete rational value with extraction methods.
| 569 | |
| 570 | |
| 571 | class RatNumRef(ArithRef): |
| 572 | """Rational numeral — concrete rational value with extraction methods.""" |
| 573 | |
| 574 | __slots__ = ("_num", "_den") |
| 575 | |
| 576 | def __init__(self, num: int, den: int) -> None: |
| 577 | ast = BinOpNode(BinOp.DIV, ToRealNode(IntLit(num)), ToRealNode(IntLit(den))) |
| 578 | super().__init__(ast, RealSort()) |
| 579 | self._num = num |
| 580 | self._den = den |
| 581 | |
| 582 | def numerator(self) -> IntNumRef: |
| 583 | return IntNumRef(IntLit(self._num), IntSort()) |
| 584 | |
| 585 | def denominator(self) -> IntNumRef: |
| 586 | return IntNumRef(IntLit(self._den), IntSort()) |
| 587 | |
| 588 | def numerator_as_long(self) -> int: |
| 589 | return self._num |
| 590 | |
| 591 | def denominator_as_long(self) -> int: |
| 592 | return self._den |
| 593 | |
| 594 | def as_fraction(self): |
| 595 | return Fraction(self._num, self._den) |
| 596 | |
| 597 | def as_decimal(self, prec: int = 10) -> str: |
| 598 | ctx = getcontext() |
| 599 | ctx.prec = prec |
| 600 | return str(Decimal(self._num) / Decimal(self._den)) |
| 601 | |
| 602 | def as_string(self) -> str: |
| 603 | return f"{self._num}/{self._den}" |
| 604 | |
| 605 | def as_long(self) -> int: |
| 606 | return self._num // self._den |
| 607 | |
| 608 | |
| 609 | class AlgebraicNumRef(ArithRef): |
no outgoing calls
no test coverage detected