Normalizes op1, op2 to have the same exp and length of coefficient. Done during addition.
(op1, op2, prec = 0)
| 5652 | |
| 5653 | |
| 5654 | def _normalize(op1, op2, prec = 0): |
| 5655 | """Normalizes op1, op2 to have the same exp and length of coefficient. |
| 5656 | |
| 5657 | Done during addition. |
| 5658 | """ |
| 5659 | if op1.exp < op2.exp: |
| 5660 | tmp = op2 |
| 5661 | other = op1 |
| 5662 | else: |
| 5663 | tmp = op1 |
| 5664 | other = op2 |
| 5665 | |
| 5666 | # Let exp = min(tmp.exp - 1, tmp.adjusted() - precision - 1). |
| 5667 | # Then adding 10**exp to tmp has the same effect (after rounding) |
| 5668 | # as adding any positive quantity smaller than 10**exp; similarly |
| 5669 | # for subtraction. So if other is smaller than 10**exp we replace |
| 5670 | # it with 10**exp. This avoids tmp.exp - other.exp getting too large. |
| 5671 | tmp_len = len(str(tmp.int)) |
| 5672 | other_len = len(str(other.int)) |
| 5673 | exp = tmp.exp + min(-1, tmp_len - prec - 2) |
| 5674 | if other_len + other.exp - 1 < exp: |
| 5675 | other.int = 1 |
| 5676 | other.exp = exp |
| 5677 | |
| 5678 | tmp.int *= 10 ** (tmp.exp - other.exp) |
| 5679 | tmp.exp = other.exp |
| 5680 | return op1, op2 |
| 5681 | |
| 5682 | ##### Integer arithmetic functions used by ln, log10, exp and __pow__ ##### |
| 5683 |