question about math module notation

B

brad

How does one make the math module spit out actual values without using
engineer or scientific notation?

I get this from <code>print math.pow(2,64)</code>:
1.84467440737e+19

I want this:
18,446,744,073,709,551,616

I'm lazy... I don't want to convert it manually :)
 
S

Stargaming

How does one make the math module spit out actual values without using
engineer or scientific notation?

I get this from <code>print math.pow(2,64)</code>: 1.84467440737e+19

I want this:
18,446,744,073,709,551,616

I'm lazy... I don't want to convert it manually :)

Explicitly converting it to `int` works for me. (Without the 3-digit-
block notation, of course.)
 
G

Gary Herron

Stargaming said:
Explicitly converting it to `int` works for me. (Without the 3-digit-
block notation, of course.)
It's got nothing to do with the math module. Any floating point number,
whether produced by the math module or not, can be converted to a
printable representation using the % operator with a format string:
1.8447e+19

See http://docs.python.org/lib/typesseq-strings.html for all he details.

Gary Herron
 
P

Paul Rubin

brad said:
Ah yes, that works too... thanks. I've settled on doing it this way:
print int(math.pow(2,64))
I like the added parenthesis :)

I was surprised to find that gives an exact (integer, not
floating-point) answer. Still, I think it's better to say 2**64
which also works for (e.g.) 2**10000 where math.pow(2,10000)
raises an exception.
 
D

Dan Bishop

I was surprised to find that gives an exact (integer, not
floating-point) answer. Still, I think it's better to say 2**64
which also works for (e.g.) 2**10000 where math.pow(2,10000)
raises an exception.

It *is* binary floating point. Powers of 2 are exactly
representable. Of course, it doesn't give an exact answer in general.
99999999999999991611392L
 
P

Paul Rubin

Dan Bishop said:
It *is* binary floating point. Powers of 2 are exactly
representable. Of course, it doesn't give an exact answer in general.

99999999999999991611392L

Oh yikes, good point. math.pow(2,64) is really not appropriate for
what the OP wanted, I'd say. If you want integer exponentiation, then
write it that way. Don't do a floating point exponentiation that just
happens to not lose precision for the specific example.
717897987691852588770249L
 

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,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top