473,785 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

round() wrong in Python 2.4?

Why did round() change in Python 2.4?

$ python2.3
Python 2.3.5 (#2, Jun 19 2005, 13:28:00)
[GCC 3.3.6 (Debian 1:3.3.6-6)] on linux2
round(0.0225, 3) 0.023 "%.3f" % round(0.0225, 3) '0.023' $ python2.4
Python 2.4.1 (#2, Jul 12 2005, 09:22:25)
[GCC 4.0.1 (Debian 4.0.1-1)] on linux2 round(0.0225, 3) 0.0219999999999 99999 "%.3f" % round(0.0225, 3) '0.022'


(Is this due to the different GCC used?)

How do you correctly output floating-point numbers in 2.4?

I do not like the "print number + EPS" solution, as you would need
different EPS for different exponent sizes. In C you could get it by
taking integer 1, and &-ing in the right exponent, and then casting to
double via void*. This would not be very portable, though.
Klem fra Nils

Sep 13 '05
14 5949
Grant Edwards wrote:
On 2005-09-14, Robert Kern <rk***@ucsd.edu > wrote:
Antoon Pardon wrote:

0.0225 isn't representable and it happens that the actual number
you get differ. Now which number python should choose when it is
fed 0.0225, I don't know. But expressing the different behaviour
as a change in round, suggest that the O.P. would be wise to
learn about floating point problems


Uhh, Python didn't change anything between 2.3 and 2.4 wrt round().


That's what Antoon Pardon just said. The above paragraph says
that round() didn't change, and the fact that the OP thinks it
did indicates that the OP needs to learn more about FP.


Antoon:
"Python 2.3 isn't rounding 0.0225 up while pyton 2.4 rounds it down."

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Sep 14 '05 #11
Robert Kern wrote:
Grant Edwards wrote:
On 2005-09-14, Robert Kern <rk***@ucsd.edu > wrote:
Antoon Pardon wrote:

0.0225 isn't representable and it happens that the actual number
you get differ. Now which number python should choose when it is
fed 0.0225, I don't know. But expressing the different behaviour
as a change in round, suggest that the O.P. would be wise to
learn about floating point problems

Uhh, Python didn't change anything between 2.3 and 2.4 wrt round().


That's what Antoon Pardon just said. The above paragraph says
that round() didn't change, and the fact that the OP thinks it
did indicates that the OP needs to learn more about FP.


Antoon:
"Python 2.3 isn't rounding 0.0225 up while pyton 2.4 rounds it down."


Written in Pseudocode:

not (Py2.3 rounding up and Py2.4 rounding down)

Reinhold
Sep 15 '05 #12
Reinhold Birkenfeld wrote:
Robert Kern wrote:

Antoon:
"Python 2.3 isn't rounding 0.0225 up while pyton 2.4 rounds it down."


Written in Pseudocode:

not (Py2.3 rounding up and Py2.4 rounding down)


I presumed the "isn't" was a typo given the "while."

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Sep 15 '05 #13
Robert Kern wrote:
Reinhold Birkenfeld wrote:
Robert Kern wrote:

Antoon:
"Python 2.3 isn't rounding 0.0225 up while pyton 2.4 rounds it down."


Written in Pseudocode:

not (Py2.3 rounding up and Py2.4 rounding down)


I presumed the "isn't" was a typo given the "while."


Oh never mind. I'm sorry I started this line of conversation.

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Sep 15 '05 #14
Op 2005-09-14, Robert Kern schreef <rk***@ucsd.edu >:
Antoon Pardon wrote:
Op 2005-09-13, Robert Kern schreef <rk***@ucsd.edu >:
Jeremy Sanders wrote:

Nils Grimsmo wrote:

>Why did round() change in Python 2.4?

It the usual floating point representation problem. 0.0225 cannot be
represent ed exactly:

That's not what he's asking about. He's asking why his Python 2.3 rounds
0.0225 *up* to 0.023 while his Python 2.4 rounds *down* to 0.022. It's
the change in behavior that he's concerned with and isn't just the usual
floating point problem.
I would say the usual floating point problem is involved.

Python 2.3 isn't rounding 0.0225 up while pyton 2.4 rounds it down.

0.0225 isn't representable and it happens that the actual number
you get differ. Now which number python should choose when it is
fed 0.0225, I don't know. But expressing the different behaviour
as a change in round, suggest that the O.P. would be wise to
learn about floating point problems


Uhh, Python didn't change anything between 2.3 and 2.4 wrt round().


That is what I said, or at least meant to say.
The
reason he is seeing a difference is because the two executables were
built with different compilers. The fact that the version of Python was
different in the two cases obscures the real cause.
IMO the real cause is unimportant. The real cause can be a different
CPU or a different compilor or a different library. What it boils
down to is that you can't expect 0,0225 to be represented in a
value that will be rounded up.
Saying that 0.0225 can't be represented exactly as a binary floating
point number is entirely true but is an incomplete answer. Yes,
obviously binary floating point representations are involved. But one
could always define a standard representation scheme that always gives
the same answer for the same input.
Can we? I don't think we can, unless you are working with decimal
numbers. If you work with floats you are basically saying that
the program should choose the best approximation it can. That
approximation can differ according to circumstances. So one
must be prepared that round(0.225,3) can give different results
in different circumstances.
The fact is that for some reason
there are two schemes being used. Another fact is that this has nothing
to do with difference in the versions of Python he is using. Most of
Python's floating point behavior is a platform-dependent accident (as
Tim Peters always says), and Nils is using two slightly different platforms.


Yes and he wouldn't have blamed it on round, had he known or thought
about FP representations .

--
Antoon Pardon
Sep 15 '05 #15

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

Similar topics

4
10052
by: Jason Tesser | last post by:
I am using Rekall which uses Python as it's scripting language and I have a question about Python. I am developing a POS system and am working on the checkout form. I pass a parameter named SaleID and another one named Total. I am trying to take total and convert it to a float with 2 decimals top be used as money obviously. What would be the proper syntax for this? I tried Total = round(Total ) i also tried that with float. Jason...
11
2531
by: Russell E. Owen | last post by:
I realize this probably a stupid question, but...is it safe to round to the nearest integer by using int(round(val))? I suspect it is fine, but wanted to be sure that weird floating point representation on some platform might make it unsafe there (i.e. get the wrong value due to the floating point value being an approximation) and if so, is there a Better Way). -- Russell
2
3131
by: Matias Silva | last post by:
Can anybody tell me why I am getting rounding errors using the ROUND function. 3.7125 rounds to 3.70 when I use the following: TRUNCATE(ROUND(units_pay_amount * fees_amount, 2),2))) The correct value should be 3.71 I could round to the 3rd decimal place ROUND(X,3) and that would round it correctly to 3.71 but that would mean I would have to change the ROUND function in another
36
5912
by: Phat G5 (G3) | last post by:
Has anyone found a reliable way to force JS to round to a specific number of places? Every time I try I get different results. For example, I'd need to round 3.4589 to 2 places. What is the most reliable way to do it? Thanks -S
4
7254
by: Fuzzydave | last post by:
I have been using a round command in a few places to round a value to zero decimal places using the following format, round('+value+', 0) but this consistantly returns the rounded result of the value to one decimal place with a zero EG:
3
1834
by: Krishna.K.1900 | last post by:
Does round() always perfectly return the output expected or are there some artifacts which don't allow perfect functionality Using python 2.5: 12.23 12.234 12.199999999999999 but was expecting 12.2
4
10900
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
9
6558
by: josh logan | last post by:
Hello, I need a round function that _always_ rounds to the higher integer if the argument is equidistant between two integers. In Python 3.0, this is not the advertised behavior of the built-in function round() as seen below: 0 2 2
0
1556
by: Edwin.Madari | last post by:
>>round(76.1, -2) 100.0 80.0 76.0 builtin function round, will work for you...... Help on built-in function round in module __builtin__: round(...) round(number) -floating point number
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10341
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
10155
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...
1
10095
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
9954
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...
1
7502
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
6741
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
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
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

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.