473,545 Members | 2,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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!
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/
(read about IEEE754 here)

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
visi.com about your INTESTINES being
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
academic anthropologists really.

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

think visi.com about your INTESTINES being almost FORTY YARDS

LONG!!
May 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
5922
by: Nils Grimsmo | last post by:
Why did round() change in Python 2.4? $ python2.3 Python 2.3.5 (#2, Jun 19 2005, 13:28:00) on linux2 >>> round(0.0225, 3) 0.023 >>> "%.3f" % round(0.0225, 3) '0.023' >>>
0
334
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 393 open (+15) / 3315 closed (+17) / 3708 total (+32) Bugs : 908 open (+22) / 5975 closed (+49) / 6883 total (+71) RFE : 223 open ( -1) / 229 closed ( +2) / 452 total ( +1) New / Reopened Patches ______________________
11
6629
by: cj | last post by:
Lets assume all calculations are done with decimal data types so things are as precise as possible. When it comes to the final rounding to cut a check to pay dividends for example in VB rounding seems to be done like this 3.435 = 3.44 3.445 = 3.44 Dim decNbr1 As Decimal
0
1124
by: Kurt B. Kaiser | last post by:
New / Reopened Patches ______________________ minidom pretty xml output improvement (2007-08-19) http://python.org/sf/1777134 opened by Teajay removeTest() method patch for unittest.TestSuite (2007-08-21) http://python.org/sf/1778410 opened by Mark Edgington robotparser.py fixes (2007-08-21)
206
13127
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) It then updates the value with numberOfPrecisions after the decimal
135
4182
by: robinsiebler | last post by:
I've never had any call to use floating point numbers and now that I want to, I can't! *** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) on win32. *** 0.29999999999999999 0.29999999999999999
20
4965
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
0
7689
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7943
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7456
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7786
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6022
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5359
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.