3-arg float pow()

Tim Peters tim.one at home.com
Mon Sep 3 17:06:09 EDT 2001


[David C. Ullrich]
> Seems like pow(3., 500., 7.) can't possibly do anything except just
> 3.**500. % 7(???), hence the random nature of the result and the utter
> uselessness.

[Markus Schaber]
> The trick is that it is a exponential function with large result modulo
> a small number. (such operations are e. G. used in the RSA crypto
> algorithm)
> ... [and an explanation of the modular square-and-multiply method] ...

I'm sure David knows all that.  He's responding specifically to that these
are not integers, but floating-point numbers.

> ...
> But I don't know whether this trick is usable in floating point, as
> most floating point is done in hardware nowadays.

Bingo, although it has less to do with hardware than with that multiplying
two floats loses half the result bits each time you do it.  The result is
thus gibberish in the absence of heroic efforts to simulate
arbitrary-precision floats.  But anyone working in crypto etc sticks to
integers in these contexts, and indeed that's why 3-argument pow() was
introduced.  The generalization to allow float arguments too was a Bad Idea,
and since we're never going to "fix it" and there are no known uses of it, I
want to get rid of it instead.  x**y % z will still "work" with float
arguments, if you couldn't care less what result you get <0.6 wink>.

BTW, note that in 2.2, overflowing int operations automatically produce long
results:

C:\Code\python\PCbuild>python
Python 2.2a2+ (#22, Sep  3 2001, 14:31:58) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 3 ** 40
12157665459056928801L
>>> 9238792387 * 928379283
8577103452028918521L
>>> 823749238749238749238749238472398
823749238749238749238749238472398L
>>>

If you do work with long-int algorithms, I think you'll be delighted with
how this simplifies your life.






More information about the Python-list mailing list