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

Method __pow__

Lib/fractions.py:885–917  ·  view source on GitHub ↗

a ** b If b is not an integer, the result will be a float or complex since roots are generally irrational. If b is an integer, the result will be rational.

(a, b, modulo=None)

Source from the content-addressed store, hash-verified

883 __mod__, __rmod__ = _operator_fallbacks(_mod, operator.mod, False)
884
885 def __pow__(a, b, modulo=None):
886 """a ** b
887
888 If b is not an integer, the result will be a float or complex
889 since roots are generally irrational. If b is an integer, the
890 result will be rational.
891
892 """
893 if modulo is not None:
894 return NotImplemented
895 if isinstance(b, numbers.Rational):
896 if b.denominator == 1:
897 power = b.numerator
898 if power >= 0:
899 return Fraction._from_coprime_ints(a._numerator ** power,
900 a._denominator ** power)
901 elif a._numerator > 0:
902 return Fraction._from_coprime_ints(a._denominator ** -power,
903 a._numerator ** -power)
904 elif a._numerator == 0:
905 raise ZeroDivisionError('Fraction(%s, 0)' %
906 a._denominator ** -power)
907 else:
908 return Fraction._from_coprime_ints((-a._denominator) ** -power,
909 (-a._numerator) ** -power)
910 else:
911 # A fractional power will generally produce an
912 # irrational number.
913 return float(a) ** float(b)
914 elif isinstance(b, (float, complex)):
915 return float(a) ** b
916 else:
917 return NotImplemented
918
919 def __rpow__(b, a, modulo=None):
920 """a ** b"""

Callers

nothing calls this directly

Calls 1

_from_coprime_intsMethod · 0.80

Tested by

no test coverage detected