473,804 Members | 3,469 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 3076
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
5953
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
6663
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
1145
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
13348
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
4339
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
5023
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
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10589
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10085
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2999
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.