python rounding problem.

C

chun ping wang

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?

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE!
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
 
E

Erik Max Francis

chun said:
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.

If you want to print it to only three digits, then use something like::
'2.995'
 
G

Gary Wessle

Erik Max Francis said:
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?
 
T

Thomas Bartkus

Gary Wessle said:
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
 
G

Grant Edwards

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.333333333", chopping it off at some arbitrary precision.

Or you can write 0.1
3

:)
 
T

Thomas Bartkus

Grant Edwards said:
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.333333333", 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
 
G

Grant Edwards

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,denominator 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.
 
T

Thomas Bartkus

Grant Edwards said:
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,denominator tuples as you suggest.

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.
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
academic anthropologists really.

On the other hand, the name "Nenet" is full of "N"s and so evocative of the
number nine ;-)
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
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!!
 
G

Grant Edwards

I don't know.

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.

http://it.stlawu.edu/~dmelvill/mesomath/sumerian.html
http://www.ancientscripts.com/sumerian.html
http://www-gap.dcs.st-and.ac.uk/~history/HistTopics/Babylonian_numerals.html
I do know that we have 360 degrees in a circle

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

Dan Bishop

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

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

Grant Edwards

Grant Edwards wrote:
...

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.

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

Dan Bishop

Grant said:
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.

Yes, it was. ("Our spy's message says that Cyrus the Great has '6 '
troops. Does that mean 360 or 21,600?")
 
T

Tim Roberts

chun ping wang said:
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?

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

C:\WINDOWS>python
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.
 
F

Florian Diesch

Thomas Bartkus said:
Grant Edwards said:
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.333333333", 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?

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.

Even base 60 makes more sense if you like it when a lot of divisions come
out nice and even.

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
 

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

Similar Threads

modifying iterator value. 2
Help with syntax warnings 6
gmail access 1
May i customize basic operator (such as 1==3)? 5
open4 2
WeBrick 4
Lines of Strings 4
PHP in instant rails 1

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top