codon icon indicating copy to clipboard operation
codon copied to clipboard

The Python built-in pow seems to be missing

Open ypfmde opened this issue 2 years ago • 3 comments

a = pow(2, 3) throws an error: name 'pow' is not defined, even though pow is a Python built-in.

ypfmde avatar Mar 25 '23 06:03 ypfmde

Here you go:

import math

a = math.pow(2, 3)
print(a)

NickDatLe avatar Mar 25 '23 07:03 NickDatLe

OK, but what about the Python built-in pow(2, 3, 7)? (math.pow doesn't do the modulo integer exponentiation.) Aren't the Python built-ins supposed to be usable in condon?

ypfmde avatar Mar 25 '23 09:03 ypfmde

Hi @ypfmde, yes pow() should be there -- we'll add it in the next release. Here is a replacement you can use in the meantime:

def pow(base: int, exp: int, mod: int):
    if exp < 0:
        raise ValueError('negative exponent not supported in pow()')

    base %= mod
    result = 1
    while exp > 0:
        if exp & 1:
            result  = (result * base) % mod
        base = (base * base) % mod
        exp >>= 1
    return result

arshajii avatar Mar 25 '23 13:03 arshajii