python rounding problem.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chun ping wang

    #1

    python rounding problem.

    Hey i have a stupid question.

    How do i get python to print the result in only three decimal place...

    Example>>> round (2.995423333545 555, 3)
    2.9950000000000 001

    but i want to get rid of all trailing 0's..how would i do that?

    _______________ _______________ _______________ _______________ _____
    Express yourself instantly with MSN Messenger! Download today - it's FREE!


  • Erik Max Francis

    #2
    Re: python rounding problem.

    chun ping wang wrote:
    [color=blue]
    > Hey i have a stupid question.
    >
    > How do i get python to print the result in only three decimal place...
    >
    > Example>>> round (2.995423333545 555, 3)
    > 2.9950000000000 001
    >
    > but i want to get rid of all trailing 0's..how would i do that?[/color]

    Floating point arithmetic is inherently imprecise. This is not a Python
    problem.

    If you want to print it to only three digits, then use something like::
    [color=blue][color=green][color=darkred]
    >>> '%.3f' % 2.9954233335455 55[/color][/color][/color]
    '2.995'

    --
    Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    Whoever contends with the great sheds his own blood.
    -- Sa'di

    Comment

    • gene tani

      #3
      Re: python rounding problem.


      Erik Max Francis wrote:[color=blue]
      > chun ping wang wrote:
      >[color=green]
      > > Hey i have a stupid question.
      > >
      > > How do i get python to print the result in only three decimal place...
      > >
      > > Example>>> round (2.995423333545 555, 3)
      > > 2.9950000000000 001
      > >
      > > but i want to get rid of all trailing 0's..how would i do that?[/color]
      >
      > Floating point arithmetic is inherently imprecise. This is not a Python
      > problem.
      >[/color]


      (read about IEEE754 here)

      Comment

      • Gary Wessle

        #4
        Re: python rounding problem.

        Erik Max Francis <max@alcyone.co m> writes:
        [color=blue]
        > chun ping wang wrote:
        >[color=green]
        > > Hey i have a stupid question.
        > > How do i get python to print the result in only three decimal
        > > place...
        > > Example>>> round (2.995423333545 555, 3)
        > > 2.9950000000000 001
        > > but i want to get rid of all trailing 0's..how would i do that?[/color]
        >
        > Floating point arithmetic is inherently imprecise. This is not a
        > Python problem.[/color]

        does python support true rations, which means that 1/3 is a true
        one-third and not 0.333333333 rounded off at some arbitrary precision?

        Comment

        • Raymond L. Buvel

          #5
          Re: python rounding problem.

          Gary Wessle wrote:[color=blue]
          > Erik Max Francis <max@alcyone.co m> writes:
          >
          >[color=green]
          >>chun ping wang wrote:
          >>
          >>[color=darkred]
          >>>Hey i have a stupid question.
          >>>How do i get python to print the result in only three decimal
          >>>place...
          >>>Example>>> round (2.995423333545 555, 3)
          >>>2.9950000000 000001
          >>>but i want to get rid of all trailing 0's..how would i do that?[/color]
          >>
          >>Floating point arithmetic is inherently imprecise. This is not a
          >>Python problem.[/color]
          >
          >
          > does python support true rations, which means that 1/3 is a true
          > one-third and not 0.333333333 rounded off at some arbitrary precision?
          >[/color]

          Python doesn't directly support rationals but there are at least two
          third party modules that add this capability to Python.




          Comment

          • Thomas Bartkus

            #6
            Re: python rounding problem.

            "Gary Wessle" <phddas@yahoo.c om> wrote in message
            news:87ody9amsd .fsf@localhost. localdomain...[color=blue]
            > Erik Max Francis <max@alcyone.co m> writes:
            >[color=green]
            > > chun ping wang wrote:
            > >[color=darkred]
            > > > Hey i have a stupid question.
            > > > How do i get python to print the result in only three decimal
            > > > place...
            > > > Example>>> round (2.995423333545 555, 3)
            > > > 2.9950000000000 001
            > > > but i want to get rid of all trailing 0's..how would i do that?[/color]
            > >
            > > Floating point arithmetic is inherently imprecise. This is not a
            > > Python problem.[/color]
            >
            > does python support true rations, which means that 1/3 is a true
            > one-third and not 0.333333333 rounded off at some arbitrary precision?[/color]

            At risk of being boring ;-)

            - Python supports both rational and irrational numbers as floating point
            numbers the way any language on any digital computer does - imprecisely.

            A "true" (1/3) can only be expressed as a fraction. As soon as you express
            it as a floating point - you are in a bit of trouble because that's
            impossible. You can not express (1/3) as a floating point in Python any
            more than you can do it with pencil and paper. You can be precise and write
            "1/3" or you can surrender to arithmetic convenience and settle for the
            imprecise by writing "0.33333333 3", chopping it off at some arbitrary
            precision.

            Which is exactly what you did in your post ;-)
            Thomas Bartkus


            Comment

            • Grant Edwards

              #7
              Re: python rounding problem.

              On 2006-05-08, Thomas Bartkus <thomasbartkus@ comcast.net> wrote:
              [color=blue][color=green]
              >> does python support true rations, which means that 1/3 is a
              >> true one-third and not 0.333333333 rounded off at some
              >> arbitrary precision?[/color]
              >
              > At risk of being boring ;-)
              >
              > - Python supports both rational and irrational numbers as
              > floating point numbers the way any language on any digital
              > computer does - imprecisely.
              >
              > A "true" (1/3) can only be expressed as a fraction.[/color]

              At the risk of being both boring and overly pedantic, that's
              not true. In base 3, the value in question is precisely
              representable in floating point: 0.1
              [color=blue]
              > As soon as you express it as a floating point - you are in a
              > bit of trouble because that's impossible.[/color]

              It's not possible in base 2 or base 10. It's perfectly
              possible in base 9 (used by the Nenets of Northern Russia) base
              12 (popular on planets where everybody has twelve toes) or base
              60 (used by th Sumerians). [I don't know if any of those
              peoples used floating point in those bases -- I'm just pointing
              out that your prejudice towards base 10 notation is showing.]
              [color=blue]
              > You can not express (1/3) as a floating point in Python any
              > more than you can do it with pencil and paper.[/color]

              That's true assuming base 2 in Python and base 10 on paper. The
              base used by Python is pretty much etched in stone (silicon, to
              be precise). There used to be articles about people working on
              base-3 logic gates, but base-3 logic never made it out of the
              lab. However, you can pick any base you want when using paper
              and pencil.
              [color=blue]
              > You can be precise and write "1/3" or you can surrender to
              > arithmetic convenience and settle for the imprecise by writing
              > "0.33333333 3", chopping it off at some arbitrary precision.[/color]

              Or you can write 0.1
              3

              :)

              --
              Grant Edwards grante Yow! Yes, Private
              at DOBERMAN!!
              visi.com

              Comment

              • Thomas Bartkus

                #8
                Re: python rounding problem.

                "Grant Edwards" <grante@visi.co m> wrote in message
                news:125v9g8q00 o1d52@corp.supe rnews.com...[color=blue]
                > On 2006-05-08, Thomas Bartkus <thomasbartkus@ comcast.net> wrote:
                >[color=green][color=darkred]
                > >> does python support true rations, which means that 1/3 is a
                > >> true one-third and not 0.333333333 rounded off at some
                > >> arbitrary precision?[/color]
                > >
                > > At risk of being boring ;-)
                > >
                > > - Python supports both rational and irrational numbers as
                > > floating point numbers the way any language on any digital
                > > computer does - imprecisely.
                > >
                > > A "true" (1/3) can only be expressed as a fraction.[/color]
                >
                > At the risk of being both boring and overly pedantic, that's
                > not true. In base 3, the value in question is precisely
                > representable in floating point: 0.1
                >[color=green]
                > > As soon as you express it as a floating point - you are in a
                > > bit of trouble because that's impossible.[/color]
                >
                > It's not possible in base 2 or base 10. It's perfectly
                > possible in base 9 (used by the Nenets of Northern Russia) base
                > 12 (popular on planets where everybody has twelve toes) or base
                > 60 (used by th Sumerians). [I don't know if any of those
                > peoples used floating point in those bases -- I'm just pointing
                > out that your prejudice towards base 10 notation is showing.]
                >[color=green]
                > > You can not express (1/3) as a floating point in Python any
                > > more than you can do it with pencil and paper.[/color]
                >
                > That's true assuming base 2 in Python and base 10 on paper. The
                > base used by Python is pretty much etched in stone (silicon, to
                > be precise). There used to be articles about people working on
                > base-3 logic gates, but base-3 logic never made it out of the
                > lab. However, you can pick any base you want when using paper
                > and pencil.
                >[color=green]
                > > You can be precise and write "1/3" or you can surrender to
                > > arithmetic convenience and settle for the imprecise by writing
                > > "0.33333333 3", chopping it off at some arbitrary precision.[/color]
                >
                > Or you can write 0.1
                > 3
                >
                > :)[/color]

                Ahhh!
                But if I need to store the value 1/10 (decimal!), what kind of a precision
                pickle will I then find myself while working in base 3 ? How much better
                for precision if we just learn our fractions and stick to storing integer
                numerators alongside integer denominators in big 128 bit double registers ?

                Even the Nenets might become more computationally precise by such means ;-)
                And how does a human culture come to decide on base 9 arithmetic anyway?
                Even base 60 makes more sense if you like it when a lot of divisions come
                out nice and even.

                Do the Nenets amputate the left pinky as a rite of adulthood ;-)
                Thomas Bartkus




                Comment

                • Grant Edwards

                  #9
                  Re: python rounding problem.

                  On 2006-05-08, Thomas Bartkus <thomasbartkus@ comcast.net> wrote:
                  [color=blue][color=green]
                  >> Or you can write 0.1
                  >> 3
                  >>
                  >> :)[/color]
                  >
                  > Ahhh!
                  >
                  > But if I need to store the value 1/10 (decimal!), what kind of
                  > a precision pickle will I then find myself while working in
                  > base 3?[/color]

                  Then we're right back where we started. No matter what base
                  you choose, any fixed length floating-point representation can
                  only represent 0% of all rational numbers.

                  So, clearly what we need are floating point objects with
                  configurable bases -- bases that automatically adjust to
                  maintain exact representation of calculation results. Which
                  probably exactly the same as just storing rational numbers as
                  numerator,denom inator tuples as you suggest.
                  [color=blue]
                  > How much better for precision if we just learn our fractions
                  > and stick to storing integer numerators alongside integer
                  > denominators in big 128 bit double registers ?
                  >
                  > Even the Nenets might become more computationally precise by
                  > such means ;-) And how does a human culture come to decide on
                  > base 9 arithmetic anyway?[/color]

                  I've no clue, whatsoever. I just stumbled across that factoid
                  when I used Wikipedia to look up which civilizations used
                  base-60. For some reason I can never remember whether it was
                  one of the mesoamerican ones or one of the mesopotamian ones.
                  [color=blue]
                  > Even base 60 makes more sense if you like it when a lot of
                  > divisions come out nice and even.[/color]

                  Did they actually have 60 unique number symbols and use
                  place-weighting in a manner similar to the arabic/indian system
                  we use?
                  [color=blue]
                  > Do the Nenets amputate the left pinky as a rite of adulthood
                  > ;-)[/color]

                  Nah, winters up there are so friggin' cold that nobody ever has
                  more than nine digits by the time they reach adulthood.

                  --
                  Grant Edwards grante Yow! Hello. Just walk
                  at along and try NOT to think
                  visi.com about your INTESTINES being
                  almost FORTY YARDS LONG!!

                  Comment

                  • Thomas Bartkus

                    #10
                    Re: python rounding problem.

                    "Grant Edwards" <grante@visi.co m> wrote in message
                    news:125vedc9n2 q3m55@corp.supe rnews.com...[color=blue]
                    > On 2006-05-08, Thomas Bartkus <thomasbartkus@ comcast.net> wrote:
                    >[color=green][color=darkred]
                    > >> Or you can write 0.1
                    > >> 3
                    > >>
                    > >> :)[/color]
                    > >
                    > > Ahhh!
                    > >
                    > > But if I need to store the value 1/10 (decimal!), what kind of
                    > > a precision pickle will I then find myself while working in
                    > > base 3?[/color]
                    >
                    > Then we're right back where we started. No matter what base
                    > you choose, any fixed length floating-point representation can
                    > only represent 0% of all rational numbers.
                    >
                    > So, clearly what we need are floating point objects with
                    > configurable bases -- bases that automatically adjust to
                    > maintain exact representation of calculation results. Which
                    > probably exactly the same as just storing rational numbers as
                    > numerator,denom inator tuples as you suggest.
                    >[color=green]
                    > > How much better for precision if we just learn our fractions
                    > > and stick to storing integer numerators alongside integer
                    > > denominators in big 128 bit double registers ?[/color][/color]

                    I completely overlooked the infinite (presumably!) length integer handling
                    in Python. You can do integer arithmetic on integers of large and arbitrary
                    lengths and if ultimate precision were indeed so important (and I can't
                    imagine why!) then working with numerators and denominators stored as tuples
                    is quite practical.

                    Anyone old enough to remember Forth might remember the arguments about how
                    unnecessary floating point is. True enough! Floating point is merely a
                    convenience for which we sacrifice some (insignificant! ) arithmetic
                    precision to enjoy.
                    [color=blue][color=green]
                    > > Even the Nenets might become more computationally precise by
                    > > such means ;-) And how does a human culture come to decide on
                    > > base 9 arithmetic anyway?[/color]
                    >
                    > I've no clue, whatsoever. I just stumbled across that factoid
                    > when I used Wikipedia to look up which civilizations used
                    > base-60. For some reason I can never remember whether it was
                    > one of the mesoamerican ones or one of the mesopotamian ones.[/color]

                    I suspect a hoax or an urban legend here. A brief and casual googling
                    brings up the Nenets but no mention of base 9 arithmetic which I would find
                    rather astonishing.
                    Look up the Tasaday tribe together with the word "hoax". A great joke on
                    academic anthropologists really.

                    On the other hand, the name "Nenet" is full of "N"s and so evocative of the
                    number nine ;-)
                    [color=blue][color=green]
                    > > Even base 60 makes more sense if you like it when a lot of
                    > > divisions come out nice and even.[/color]
                    >
                    > Did they actually have 60 unique number symbols and use
                    > place-weighting in a manner similar to the arabic/indian system
                    > we use?[/color]

                    I don't know.
                    I do know that we have 360 degrees in a circle for the simple reason that
                    this is evenly divisible by so damned many integers. A significant and
                    logical convenience if you have to do all your calculations on a wooden
                    board using the chunk of charcoal you hold in your fist.

                    Thomas Bartkus
                    [color=blue][color=green]
                    > > Do the Nenets amputate the left pinky as a rite of adulthood
                    > > ;-)[/color]
                    >
                    > Nah, winters up there are so friggin' cold that nobody ever has
                    > more than nine digits by the time they reach adulthood.
                    >
                    > --
                    > Grant Edwards grante Yow! Hello. Just walk
                    > at along and try NOT to[/color]
                    think[color=blue]
                    > visi.com about your INTESTINES[/color]
                    being[color=blue]
                    > almost FORTY YARDS[/color]
                    LONG!!


                    Comment

                    • Grant Edwards

                      #11
                      Re: python rounding problem.

                      On 2006-05-09, Thomas Bartkus <thomasbartkus@ comcast.net> wrote:
                      [color=blue][color=green][color=darkred]
                      >>> Even base 60 makes more sense if you like it when a lot of
                      >>> divisions come out nice and even.[/color]
                      >>
                      >> Did they actually have 60 unique number symbols and use
                      >> place-weighting in a manner similar to the arabic/indian
                      >> system we use?[/color]
                      >
                      > I don't know.[/color]

                      I googled around a while last night, and they ahd sort of a
                      hybrid notation. The Sumerians started withindividual "tic
                      marks" up to 9, and symbols for 10, 60, 600, 3600 and so on.
                      That evolved into the Babylonian base-60 position-weighted
                      system (without a zero symbol) that used only the 1 symbol and
                      the 10 symbol.




                      [color=blue]
                      > I do know that we have 360 degrees in a circle[/color]

                      And 60 seconds in a minute, 60 minutes in a hour (for both time
                      and angles), and 60 minutes in a degree.

                      --
                      Grant Edwards grante Yow! PARDON me, am I
                      at speaking ENGLISH?
                      visi.com

                      Comment

                      • Dan Bishop

                        #12
                        Re: python rounding problem.

                        Grant Edwards wrote:
                        ....[color=blue]
                        > Did they actually have 60 unique number symbols and use
                        > place-weighting in a manner similar to the arabic/indian system
                        > we use?[/color]

                        The Bablyonians did use a place-value system, but they only had two
                        basic numerals: a Y-like symbol for 1 and a <-like symbol for ten.
                        These were combined to make base-60 digits. For example, 59 was
                        represented by

                        < YYY
                        < < YYY
                        < < YYY

                        Zero (used as a placeholder, but not as a number in itself) was
                        represented by a space.

                        Comment

                        • Grant Edwards

                          #13
                          Re: python rounding problem.

                          On 2006-05-09, Dan Bishop <danb_83@yahoo. com> wrote:[color=blue]
                          > Grant Edwards wrote:
                          > ...[color=green]
                          >> Did they actually have 60 unique number symbols and use
                          >> place-weighting in a manner similar to the arabic/indian system
                          >> we use?[/color]
                          >
                          > The Bablyonians did use a place-value system, but they only had two
                          > basic numerals: a Y-like symbol for 1 and a <-like symbol for ten.
                          > These were combined to make base-60 digits. For example, 59 was
                          > represented by
                          >
                          > < YYY
                          > < < YYY
                          >< < YYY
                          >
                          > Zero (used as a placeholder, but not as a number in itself) was
                          > represented by a space.[/color]

                          And they also (acording to the web pages I found) used base-60
                          floating point notation, but without an actual symbol to
                          represent the sexagesimal point. Which seems really ambiguous --
                          even to somebody who does know how to use a slide rule.

                          --
                          Grant Edwards grante Yow! I'm totally
                          at DESPONDENT over the LIBYAN
                          visi.com situation and the price
                          of CHICKEN...

                          Comment

                          • Tim Roberts

                            #14
                            Re: python rounding problem.

                            "chun ping wang" <cablepuff@hotm ail.com> wrote:[color=blue]
                            >
                            >Hey i have a stupid question.
                            >
                            >How do i get python to print the result in only three decimal place...
                            >
                            >Example>>> round (2.995423333545 555, 3)
                            >2.995000000000 0001
                            >
                            >but i want to get rid of all trailing 0's..how would i do that?[/color]

                            Your "problem" is not a problem in real programs -- it's only a problem
                            because of the way the interactive interpreter works:

                            C:\WINDOWS>pyth on
                            Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on
                            win32
                            Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
                            >>> round(2.9954332 2,3)[/color][/color][/color]
                            2.9950000000000 001[color=blue][color=green][color=darkred]
                            >>> print round(2.9954332 2,3)[/color][/color][/color]
                            2.995[color=blue][color=green][color=darkred]
                            >>>[/color][/color][/color]

                            --
                            - Tim Roberts, timr@probo.com
                            Providenza & Boekelheide, Inc.

                            Comment

                            • Florian Diesch

                              #15
                              Re: python rounding problem.

                              "Thomas Bartkus" <thomasbartkus@ comcast.net> wrote:
                              [color=blue]
                              > "Grant Edwards" <grante@visi.co m> wrote in message
                              > news:125v9g8q00 o1d52@corp.supe rnews.com...[color=green]
                              >> On 2006-05-08, Thomas Bartkus <thomasbartkus@ comcast.net> wrote:
                              >>[color=darkred]
                              >> >> does python support true rations, which means that 1/3 is a
                              >> >> true one-third and not 0.333333333 rounded off at some
                              >> >> arbitrary precision?
                              >> >
                              >> > At risk of being boring ;-)
                              >> >
                              >> > - Python supports both rational and irrational numbers as
                              >> > floating point numbers the way any language on any digital
                              >> > computer does - imprecisely.
                              >> >
                              >> > A "true" (1/3) can only be expressed as a fraction.[/color]
                              >>
                              >> At the risk of being both boring and overly pedantic, that's
                              >> not true. In base 3, the value in question is precisely
                              >> representable in floating point: 0.1
                              >>[color=darkred]
                              >> > As soon as you express it as a floating point - you are in a
                              >> > bit of trouble because that's impossible.[/color]
                              >>
                              >> It's not possible in base 2 or base 10. It's perfectly
                              >> possible in base 9 (used by the Nenets of Northern Russia) base
                              >> 12 (popular on planets where everybody has twelve toes) or base
                              >> 60 (used by th Sumerians). [I don't know if any of those
                              >> peoples used floating point in those bases -- I'm just pointing
                              >> out that your prejudice towards base 10 notation is showing.]
                              >>[color=darkred]
                              >> > You can not express (1/3) as a floating point in Python any
                              >> > more than you can do it with pencil and paper.[/color]
                              >>
                              >> That's true assuming base 2 in Python and base 10 on paper. The
                              >> base used by Python is pretty much etched in stone (silicon, to
                              >> be precise). There used to be articles about people working on
                              >> base-3 logic gates, but base-3 logic never made it out of the
                              >> lab. However, you can pick any base you want when using paper
                              >> and pencil.
                              >>[color=darkred]
                              >> > You can be precise and write "1/3" or you can surrender to
                              >> > arithmetic convenience and settle for the imprecise by writing
                              >> > "0.33333333 3", chopping it off at some arbitrary precision.[/color]
                              >>
                              >> Or you can write 0.1
                              >> 3
                              >>
                              >> :)[/color]
                              >
                              > Ahhh!
                              > But if I need to store the value 1/10 (decimal!), what kind of a precision
                              > pickle will I then find myself while working in base 3 ? How much better
                              > for precision if we just learn our fractions and stick to storing integer
                              > numerators alongside integer denominators in big 128 bit double registers ?
                              >
                              > Even the Nenets might become more computationally precise by such means ;-)
                              > And how does a human culture come to decide on base 9 arithmetic anyway?[/color]

                              Just guessing:
                              * Use one thumb to point at one of the other 9 fingers
                              * Every finger (except for the thumb) has 3 segments (and links), each
                              of which can easily divided in three part (upper, middle, lower or left
                              middle, right for the links) making 9 points for each finger.

                              [color=blue]
                              > Even base 60 makes more sense if you like it when a lot of divisions come
                              > out nice and even.[/color]

                              You can count to 60 using two hands: Use the right thumb to point on
                              one of the 12 segments of the remaining 4 fingers and on the left hand
                              one finger for each dozen.


                              Of course this is wasting resources as you can count to 1023 with your
                              fingers. I never heard of a culture doing so, though.



                              Florian
                              --
                              <http://www.florian-diesch.de/>

                              Comment

                              Working...