This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: pow with three int arguments works like it had two arguments
Type: behavior Stage:
Components: Distutils Versions: Python 2.7
process
Status: closed Resolution: third party
Dependencies: Superseder:
Assigned To: Nosy List: Benjamin Pollak, casevh, dstufft, eric.araujo, mark.dickinson, pch
Priority: normal Keywords:

Created on 2017-01-04 22:34 by pch, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
pow-bug_report.py pch, 2017-01-04 22:34 simple test reviling a bug
pow-bug_repor-upd.py pch, 2017-01-05 03:00
Messages (5)
msg284675 - (view) Author: Pavel Chuvakhov (pch) Date: 2017-01-04 22:34
Standard `pow` function of three integer arguments should result in a reminder `(x**y) % z`. It seems that `pow(x,y,z)` ignores `%z` operation if type(z) is not `int`. This happens also in the cases when `z` has type numpy.int32, numpy.int64, etc. I consider such a behavior of `pow` as a bug to be fixed.

Thank you for your attantion, guys!
Best wishes, Pavel.
msg284676 - (view) Author: Benjamin Pollak (Benjamin Pollak) Date: 2017-01-04 23:16
I tried running your script, but it crashes on the last two lines (variables n2 and n1 appear to be undefined). Could you please update that script so we can have a better idea of the behavior of your bug?

Thanks,
BP
msg284679 - (view) Author: Case Van Horsen (casevh) Date: 2017-01-05 00:04
This is a bug/limitation in numpy. If x and y are Python integers, and type(z) is of another numeric type, pow calls the nb_power slot of the underlying type. Here a quick example using numpy.int32 and gmpy2.mpz:

>>> import numpy, gmpy2
>>> pow(11,13,7)
4
>>> pow(11,13,numpy.int32(7))
34522712143931
>>> pow(11,13,gmpy2.mpz(7))
mpz(4)


>>> (11).__pow__(13,7)
4
>>> numpy.int32(11).__pow__(13,7)
34522712143931
>>> gmpy2.mpz(11).__pow__(13,7)
mpz(4)


casevh
msg284694 - (view) Author: Pavel Chuvakhov (pch) Date: 2017-01-05 03:00
Sorry about script, n1 should be n, and n2 should be m. Updated script is attached.

Ofc mpz is a way out. One also could cast int( np.int32 ) explicitly. I just wanted to underline that the best way is to hide all this stuff from a user and not make the user think what kind of int subtypes he should use. Python is a pretty language, isn't it? =)

Ones this is bug/limitation of numpy, shall we make a report for 'numpy guys' directly?
msg284737 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2017-01-05 11:50
> shall we make a report for 'numpy guys' directly?

Yes, please! I'll close here, since this isn't a core Python issue: what 3rd party libraries choose to do in their `__pow__` methods is out of the control of the core language.
History
Date User Action Args
2022-04-11 14:58:41adminsetgithub: 73346
2017-01-20 09:22:36mark.dickinsonsetresolution: not a bug -> third party
2017-01-05 11:50:51mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg284737

resolution: not a bug
2017-01-05 03:00:30pchsetfiles: + pow-bug_repor-upd.py

messages: + msg284694
components: + Distutils, - Interpreter Core
2017-01-05 00:04:38casevhsetnosy: + casevh
messages: + msg284679
2017-01-04 23:28:50Jim Fasarakis-Hilliardsetcomponents: + Interpreter Core, - Distutils
title: `pow` with three int arguments works like it had two arguments -> pow with three int arguments works like it had two arguments
2017-01-04 23:16:34Benjamin Pollaksetnosy: + Benjamin Pollak
messages: + msg284676
2017-01-04 22:34:57pchcreate