MCPcopy Create free account
hub / github.com/RustPython/RustPython / _normalize

Function _normalize

Lib/_pydecimal.py:5618–5644  ·  view source on GitHub ↗

Normalizes op1, op2 to have the same exp and length of coefficient. Done during addition.

(op1, op2, prec = 0)

Source from the content-addressed store, hash-verified

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

Callers 1

__add__Method · 0.85

Calls 3

lenFunction · 0.85
strFunction · 0.85
minFunction · 0.85

Tested by

no test coverage detected