python rounding problem.

Thomas Bartkus thomasbartkus at comcast.net
Mon May 8 20:41:00 CEST 2006


"Gary Wessle" <phddas at yahoo.com> wrote in message
news:87ody9amsd.fsf at localhost.localdomain...
> Erik Max Francis <max at alcyone.com> writes:
>
> > chun ping wang wrote:
> >
> > > Hey i have a stupid question.
> > > How do i get python to print the result in only three decimal
> > > place...
> > > Example>>> round (2.995423333545555, 3)
> > > 2.9950000000000001
> > > but i want to get rid of all trailing 0's..how would i do that?
> >
> > Floating point arithmetic is inherently imprecise.  This is not a
> > Python problem.
>
> 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. 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.333333333", chopping it off at some arbitrary
precision.

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





More information about the Python-list mailing list