Normalizes op1, op2 to have the same exp and length of coefficient. Done during addition.
(op1, op2, prec = 0)
| 5619 | |
| 5620 | |
| 5621 | def _normalize(op1, op2, prec = 0): |
| 5622 | """Normalizes op1, op2 to have the same exp and length of coefficient. |
| 5623 | |
| 5624 | Done during addition. |
| 5625 | """ |
| 5626 | if op1.exp < op2.exp: |
| 5627 | tmp = op2 |
| 5628 | other = op1 |
| 5629 | else: |
| 5630 | tmp = op1 |
| 5631 | other = op2 |
| 5632 | |
| 5633 | # Let exp = min(tmp.exp - 1, tmp.adjusted() - precision - 1). |
| 5634 | # Then adding 10**exp to tmp has the same effect (after rounding) |
| 5635 | # as adding any positive quantity smaller than 10**exp; similarly |
| 5636 | # for subtraction. So if other is smaller than 10**exp we replace |
| 5637 | # it with 10**exp. This avoids tmp.exp - other.exp getting too large. |
| 5638 | tmp_len = len(str(tmp.int)) |
| 5639 | other_len = len(str(other.int)) |
| 5640 | exp = tmp.exp + min(-1, tmp_len - prec - 2) |
| 5641 | if other_len + other.exp - 1 < exp: |
| 5642 | other.int = 1 |
| 5643 | other.exp = exp |
| 5644 | |
| 5645 | tmp.int *= 10 ** (tmp.exp - other.exp) |
| 5646 | tmp.exp = other.exp |
| 5647 | return op1, op2 |
| 5648 | |
| 5649 | ##### Integer arithmetic functions used by ln, log10, exp and __pow__ ##### |
| 5650 |