473,545 Members | 2,779 Online

# 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?

_______________ _______________ _______________ _______________ _____
http://messenger.msn.click-url.com/g...ave/direct/01/

May 7 '06 #1
14 3058
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.995423333545 555, 3)
2.9950000000000 001

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.

If you want to print it to only three digits, then use something like::
'%.3f' % 2.9954233335455 55

'2.995'

--
Erik Max Francis && ma*@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
May 7 '06 #2

Erik Max Francis wrote:
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.995423333545 555, 3)
2.9950000000000 001

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.

http://www2.hursley.ibm.com/decimal/

May 7 '06 #3
Erik Max Francis <ma*@alcyone.co m> 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.995423333545 555, 3)
2.9950000000000 001
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?

May 7 '06 #4
Gary Wessle wrote:
Erik Max Francis <ma*@alcyone.co m> 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.995423333545 555, 3)
2.9950000000 000001
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?

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

http://calcrpnpy.sourceforge.net/clnumManual.html

http://gmpy.sourceforge.net/
May 7 '06 #5
"Gary Wessle" <ph****@yahoo.c om> wrote in message
news:87******** ****@localhost. localdomain...
Erik Max Francis <ma*@alcyone.co m> 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.995423333545 555, 3)
2.9950000000000 001
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.33333333 3", chopping it off at some arbitrary
precision.

Which is exactly what you did in your post ;-)
Thomas Bartkus
May 8 '06 #6
On 2006-05-08, Thomas Bartkus <th***********@ comcast.net> wrote:
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.

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
As soon as you express it as a floating point - you are in a
bit of trouble because that's impossible.
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.]
You can not express (1/3) as a floating point in Python any
more than you can do it with pencil and paper.
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.
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.

Or you can write 0.1
3

:)

--
Grant Edwards grante Yow! Yes, Private
at DOBERMAN!!
visi.com
May 8 '06 #7
"Grant Edwards" <gr****@visi.co m> wrote in message
news:12******** *****@corp.supe rnews.com...
On 2006-05-08, Thomas Bartkus <th***********@ comcast.net> wrote:
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.

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
As soon as you express it as a floating point - you are in a
bit of trouble because that's impossible.

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.]
You can not express (1/3) as a floating point in Python any
more than you can do it with pencil and paper.

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.
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.

Or you can write 0.1
3

:)

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

May 8 '06 #8
On 2006-05-08, Thomas Bartkus <th***********@ comcast.net> wrote:
Or you can write 0.1
3

:)
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?

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.
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?
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.
Even base 60 makes more sense if you like it when a lot of
divisions come out nice and even.
Did they actually have 60 unique number symbols and use
place-weighting in a manner similar to the arabic/indian system
we use?
Do the Nenets amputate the left pinky as a rite of adulthood
;-)

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
almost FORTY YARDS LONG!!
May 8 '06 #9
"Grant Edwards" <gr****@visi.co m> wrote in message
news:12******** *****@corp.supe rnews.com...
On 2006-05-08, Thomas Bartkus <th***********@ comcast.net> wrote:
Or you can write 0.1
3

:)
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?

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.
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 ?
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.
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?

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.

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

On the other hand, the name "Nenet" is full of "N"s and so evocative of the
number nine ;-)
Even base 60 makes more sense if you like it when a lot of
divisions come out nice and even.

Did they actually have 60 unique number symbols and use
place-weighting in a manner similar to the arabic/indian system
we use?

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
Do the Nenets amputate the left pinky as a rite of adulthood
;-)

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