473,915 Members | 3,104 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 2033
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
11747
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
1380
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 zero), probably not what you'd expect. I'm not sure how often a case like this
6
4618
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
6683
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
6674
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
2257
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
3206
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
2287
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 particular day. The basic formula in use is: =Sum()
206
13460
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
29783
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 or even. Any idea about how to implement it ?
0
10039
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
11354
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...
1
11066
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9732
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...
1
8100
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7256
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
5943
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...
0
6148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3368
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.