MCPcopy Index your code
hub / github.com/python/cpython / rounding_direction

Method rounding_direction

Modules/_decimal/tests/deccheck.py:411–431  ·  view source on GitHub ↗

Determine the effective direction of the rounding when the exact result x is rounded according to mode. Return -1 for downwards, 0 for undirected, 1 for upwards, 2 for ROUND_05UP.

(self, x, mode)

Source from the content-addressed store, hash-verified

409 return _dec_from_triple(0, '1', dec._exp+len(dec._int)-prec)
410
411 def rounding_direction(self, x, mode):
412 """Determine the effective direction of the rounding when
413 the exact result x is rounded according to mode.
414 Return -1 for downwards, 0 for undirected, 1 for upwards,
415 2 for ROUND_05UP."""
416 cmp = 1 if x.compare_total(P.Decimal("+0")) >= 0 else -1
417
418 if mode in (P.ROUND_HALF_EVEN, P.ROUND_HALF_UP, P.ROUND_HALF_DOWN):
419 return 0
420 elif mode == P.ROUND_CEILING:
421 return 1
422 elif mode == P.ROUND_FLOOR:
423 return -1
424 elif mode == P.ROUND_UP:
425 return cmp
426 elif mode == P.ROUND_DOWN:
427 return -cmp
428 elif mode == P.ROUND_05UP:
429 return 2
430 else:
431 raise ValueError("Unexpected rounding mode: %s" % mode)
432
433 def check_ulpdiff(self, exact, rounded):
434 # current precision

Callers 1

check_ulpdiffMethod · 0.95

Calls 1

compare_totalMethod · 0.45

Tested by

no test coverage detected