why does math.pow yields OverflowError (while python itself cancalculate that large number)

T

Tzury Bar Yochay

What is the reason math.pow yields OverflowError while python itself
can
calculate these large numbers. e.g:
Traceback (most recent call last):
File said:
eval(('100*'* 155)[:-1])
10000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000
0000000000000000L
 
J

John Machin

What is the reason math.pow yields OverflowError while python itself
can
calculate these large numbers. e.g:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: math range error>>> eval(('100*'* 155)[:-1])

10000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000
0000000000000000L

Because math.pow returns a float; 100 ** 155 won't fit in a float.
 
T

Tzury Bar Yochay

Because math.pow returns a float; 100 ** 155 won't fit in a float.

Sure that is the reason.
May I rephrase, my question:
Why not returning another type as long as we can calculate it?
After all, math module is likely to be used on large numbers as well.
 
J

John Machin

Sure that is the reason.
May I rephrase, my question:
Why not returning another type as long as we can calculate it?
After all, math module is likely to be used on large numbers as well.

The math module is intended to replicate the functionality found in
math.h in the C Standard Library; that's it, no more, no less. There
are other libraries if you want more-than-float precision.
 
M

Marc 'BlackJack' Rintsch

eval(('100*'* 155)[:-1])
10000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000
0000000000000000L

This can be written more straigth forward as ``100**155`` or
``pow(100, 155)``. No need for `eval()`\ing a string.

Ciao,
Marc 'BlackJack' Rintsch
 
S

Steven D'Aprano

This can be written more straigth forward as ``100**155`` or ``pow(100,
155)``. No need for `eval()`\ing a string.

But how else can the OP get an order of magnitude slow-down on an
operation that is slow in the first place?

*wink*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top