473,555 Members | 2,322 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

rounding errors?

Hi,
I've just started using Python, and am having an extraordinary experience.
One thing worries me, however, I'm planning on doing some mathematical
research with Python, and it appears that it does funny thing with
floating point numbers.. Maybe It's superficial, but here's what I'm
getting,using the interpreter..
.31 0.31 .32 0.3200000000000 0001 .33 0.3300000000000 0002 .34 0.3400000000000 0002 .35 0.3499999999999 9998 .21 0.2099999999999 9999

Now, I realize that this is really small errors.. Does anybody have an
explanation why Python picks up or loses these?
Thanks
Todd
Jul 18 '05 #1
9 2020
todd wrote:
Now, I realize that this is really small errors.. Does anybody have an
explanation why Python picks up or loses these?


It has to do with floating point, and nothing to do with Python. Any
other language using floating point has the same rounding problems.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ There's really nothing like it.
-- Sade Adu
Jul 18 '05 #2

"todd" <to********@yah oo.ca> wrote in message
news:pa******** *************** *****@yahoo.ca. ..
Hi,
I've just started using Python, and am having an extraordinary experience. One thing worries me, however, I'm planning on doing some mathematical
research with Python, and it appears that it does funny thing with
floating point numbers..


No floating point numbers are funny ;-). There should be at least one FAQ
entry on the subject and at least 10 past threads available thru Google.
Plus some numerical analysis texts. It is possible that your work should
wait for the new decimal module in 2.4.

Terry J. Reedy

Jul 18 '05 #3
http://docs.python.org/tut/node15.html

todd wrote:
Hi,
I've just started using Python, and am having an extraordinary experience.
One thing worries me, however, I'm planning on doing some mathematical
research with Python, and it appears that it does funny thing with
floating point numbers.. Maybe It's superficial, but here's what I'm
getting,using the interpreter..
.31
0.31
.32
0.3200000000000 0001
.33
0.3300000000000 0002
.34
0.3400000000000 0002
.35
0.3499999999999 9998
.21


0.2099999999999 9999

Now, I realize that this is really small errors.. Does anybody have an
explanation why Python picks up or loses these?
Thanks
Todd

Jul 18 '05 #4
[todd]
....
.21 0.2099999999999 9999


Now, I realize that this is really small errors.. Does anybody have an
explanation why Python picks up or loses these?


Many people do, and a few of them are even correct <wink>. If you
start here, you can stop here too:

http://docs.python.org/tut/node15.html

Jul 18 '05 #5
todd wrote:
I've just started using Python, and am having an extraordinary
experience.
Python's pretty cool, eh?
One thing worries me, however, I'm planning on doing some
mathematical research with Python, and it appears that it does
funny thing with floating point numbers.
No, it isn't. The problem is that you're expecting it to do
something funny with them and it isn't.
Maybe It's superficial,
No, on the contrary, it's quite fundamental to the way floating
point works in computers.
but here's what I'm getting,using the interpreter..
>>.31

0.31

>>.32

0.3200000000000 0001
[...]

And you'd prefer that the intrepreter lied to you and printed
"0.32" when you tell it to print the number 0.3200..001 --
that's a common reaction from people who don't understand
floating point numbers. If you want, you can specify a format
when printing floating point numbers so that you only see as
many digits as you want to:
"%0.2f" % 0.32 '0.32'
Now, I realize that this is really small errors.. Does anybody
have an explanation why Python picks up or loses these?


Somebody else has already posted a pointer to the Python FAQ
about floating point, but if you're planning on doing numerical
research using floating point on computers, you really ought to
take a class on numerical methods or numerical analysis using
floating point on computers.

--
Grant Edwards grante Yow! PIZZA!!
at
visi.com
Jul 18 '05 #6
Note that Python 2.4 will include a 'decimal' module that exposes a
numerical type far better suited to this sort of calculation that
binary floating point.

Jul 18 '05 #7
> Now, I realize that this is really small errors.. Does anybody have an
explanation why Python picks up or loses these?


The Timbot explanation is standard to this.

But the solution is easier: google for decimal pyhton or look at python 2.4

Harald
Jul 18 '05 #8
Anthony Baxter <an***********@ gmail.com> wrote in message news:<ma******* *************** *************** *@python.org>.. .
Note that Python 2.4 will include a 'decimal' module that exposes a
numerical type far better suited to this sort of calculation that
binary floating point.


Only if you're calculating inherently decimal things (like money).
Jul 18 '05 #9
On 3 Jul 2004 15:17:35 -0700, Dan Bishop <da*****@yahoo. com> wrote:
Note that Python 2.4 will include a 'decimal' module that exposes a
numerical type far better suited to this sort of calculation that
binary floating point.


Only if you're calculating inherently decimal things (like money).


Or anything where you really care about having controllable
precision.

Jul 18 '05 #10

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

Similar topics

20
11682
by: Raoul Watson | last post by:
By any chance, anyone got a rounding routine that does a work around the VB "round" bug? I still find it amazing that a company as large as Microsoft would put out a math package that is so problematic.
2
1362
by: Christopher T King | last post by:
Speakign of rounding errors, why is round() defined to round away from zero? I've always been quite fond of the mathematical definition of rounding of floor(n+.5). As a contrived example of why the latter is IMHO better, I present the following: for x in : print int(round(x)) This prints -10, -9, ..., -2, -1, 1, 2, ..., 9, 10 (it skips...
6
4584
by: Jeff Boes | last post by:
(asked last week on .questions, no response) Can anyone explain why this happens? (under 7.4.1) select '2004-05-27 09:00:00.500001-04' :: timestamp(0) ; timestamp --------------------- 2004-05-27 09:00:01
9
6621
by: Joe Attardi | last post by:
Hi all, Math is not my strongest area so forgive me if I use some of the wrong terminology. It seems that scientific notation is immune to rounding errors. For example: (4.98 * 100) + 5.51 // returns 503.51000000000005, rounding error! 4.98e2 + 5.51 // returns 503.51, correct!
11
6630
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
6
2241
by: shaqattack1992-newsgroups | last post by:
Hello Everyone, I'm using the following on a form in my database: =(-Int(-Sum((IIf(=Yes,((*)+()), (((*)+)*1.06))*100))))/100 In this case, I want to calculate a total for an order. If the LineTaxExempt field is checked (meaning tax exempt), the total is
29
3152
by: Marco | last post by:
Hello, I have : float f = 36.09999999; When I do : char cf; sprintf(cf,"%0.03lf", f); I get : 36.100
3
2268
by: gabo22003 | last post by:
Hello, I've inherited a tax report where all of the financial numbers are currency.This is a detailed report that lists amount, tax and total for that day ( total is listed in the database). The report gets its information from a query. At this point all is working Then in the report footer I am trying to total the entire amount for that...
206
13137
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
30
29494
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As expected my_round(2.5) = 2 # Not 3, which is an odd num I'm interested in rounding numbers of the form "x.5" depending upon whether x is odd...
0
7621
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...
0
7824
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. ...
1
7587
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...
1
5452
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
5170
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...
0
3593
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...
0
3573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2034
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
1
1156
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.