Steven D'Aprano <steve+pyt...@pearwood.info> added the comment:

Hi,

The behaviour you are calling "gibbrish" is correct, as the expression 
`(p-1)/2` calculates a 64-bit floating point number, which may lose 
precision even for small values of p, but will definitely lose precision 
for large p.

Calling `int()` on that float will not recover the lost precision.

So there is no bug here, this is expected behaviour with floats. Please 
remember that floats are not mathematically exact Real numbers like we 
learn about in school, they have limited precision.

To avoid the float conversion, use the floor-division operator 
`(p-1)//2` as you mention. If `p` is an int, the result will be exact.

----------
nosy: +steven.daprano
title: pow(a,b,p) where b=int((p-1)/2) spits out gibbrish for big p -> pow(a, 
b, p) where b=int((p-1)/2) spits out gibbrish for big p

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue40446>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to