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

Function _exact_ratio

Lib/statistics.py:1597–1620  ·  view source on GitHub ↗

Return Real number x to exact (numerator, denominator) pair. >>> _exact_ratio(0.25) (1, 4) x is expected to be an int, Fraction, Decimal or float.

(x)

Source from the content-addressed store, hash-verified

1595
1596
1597def _exact_ratio(x):
1598 """Return Real number x to exact (numerator, denominator) pair.
1599
1600 >>> _exact_ratio(0.25)
1601 (1, 4)
1602
1603 x is expected to be an int, Fraction, Decimal or float.
1604
1605 """
1606 try:
1607 return x.as_integer_ratio()
1608 except AttributeError:
1609 pass
1610 except (OverflowError, ValueError):
1611 # float NAN or INF.
1612 assert not _isfinite(x)
1613 return (x, None)
1614
1615 try:
1616 # x may be an Integral ABC.
1617 return (x.numerator, x.denominator)
1618 except AttributeError:
1619 msg = f"can't convert type '{type(x).__name__}' to numerator/denominator"
1620 raise TypeError(msg)
1621
1622
1623def _convert(value, T):

Callers 1

test_decimalMethod · 0.85

Calls 2

_isfiniteFunction · 0.85
as_integer_ratioMethod · 0.45

Tested by 1

test_decimalMethod · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…