question about math module notation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • brad

    #1

    question about math module notation

    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 :)
  • Stargaming

    #2
    Re: question about math module notation

    On Thu, 26 Jul 2007 15:54:11 -0400, brad wrote:
    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.)

    Comment

    • brad

      #3
      Re: question about math module notation

      Stargaming wrote:
      Explicitly converting it to `int` works for me. (Without the 3-digit-
      block notation, of course.)
      Thank you!

      Comment

      • Paul Rubin

        #4
        Re: question about math module notation

        brad <byte8bits@gmai l.comwrites:
        18,446,744,073, 709,551,616
        >
        I'm lazy... I don't want to convert it manually :)
        print 2**64

        Comment

        • brad

          #5
          Re: question about math module notation

          Paul Rubin wrote:
          print 2**64
          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 :)

          Comment

          • Paul Rubin

            #6
            Re: question about math module notation

            brad <byte8bits@gmai l.comwrites:
            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,1000 0)
            raises an exception.

            Comment

            • Gary Herron

              #7
              Re: question about math module notation

              Stargaming wrote:
              On Thu, 26 Jul 2007 15:54:11 -0400, brad wrote:
              >
              >
              >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.)
              >
              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:
              >>x = 1.84467440737e+ 19
              >>x
              1.84467440737e+ 19
              >>print '%25.3f' % x
              184467440736999 99744.000
              >>print '%12.5e' % x
              1.84467e+19
              >>print '%12.5g' % x
              1.8447e+19

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

              Gary Herron




              Comment

              • Dan Bishop

                #8
                Re: question about math module notation

                On Jul 26, 3:59 pm, Paul Rubin <http://phr...@NOSPAM.i nvalidwrote:
                brad <byte8b...@gmai l.comwrites:
                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,1000 0)
                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.
                >>int(math.pow( 10, 23))
                999999999999999 91611392L

                Comment

                • Paul Rubin

                  #9
                  Re: question about math module notation

                  Dan Bishop <danb_83@yahoo. comwrites:
                  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,1000 0)
                  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.
                  >
                  >int(math.pow(1 0, 23))
                  999999999999999 91611392L
                  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.
                  >>int(math.pow( 3,50)) # wrong
                  717897987691852 578422784L
                  >>3**50 # right
                  717897987691852 588770249L

                  Comment

                  Working...