473,748 Members | 2,891 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Where is the correct round() method?

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:
>>round(0.5)
0
>>round(1.5)
2
>>round(2.5)
2
I would think this is a common need, but I cannot find a function in
the Python library to do it. I wrote my own, but did I miss such a
method in my search of the Python library?

Thanks
Jul 27 '08 #1
9 6555
josh logan wrote:
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:

>>>round(0.5)
0
>>>round(1.5)
2
>>>round(2.5)
2

Huh?
>>round(2.5)
3.0
Works for me on Python 2.5 on Linux running on "Intel(R) Core(TM)2 Duo
CPU". What system are you on?
It could be that 2.5 is really 2.49999... which would round down to 2,
but on any modern CPU (using IEEE floating point), 2.5 should be
representable exactly.

However, as with any floating point calculations, if you expect exact
representation or calculations with any numbers, then you are misusing
floating points.

Gary Herron
I would think this is a common need, but I cannot find a function in
the Python library to do it. I wrote my own, but did I miss such a
method in my search of the Python library?

Thanks
--
http://mail.python.org/mailman/listinfo/python-list
Jul 28 '08 #2
it could be that 3.0 is using "banker's rounding" --- rounding to the
even digit. the idea behind it behind it being to reduce error
accumulation when working with large sets of values.

Works for me on Python 2.5 on Linux running on "Intel(R) Core(TM)2 Duo
CPU". What system are you on?
It could be that 2.5 is really 2.49999... which would round down to 2,
but on any modern CPU (using IEEE floating point), 2.5 should be
representable exactly.

Jul 28 '08 #3
On Jul 27, 7:58*pm, Gary Herron <gher...@island training.comwro te:
josh logan wrote:
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:
>>round(0.5)
0
>>round(1.5)
2
>>round(2.5)
2

Huh?

*>>round(2.5)
3.0

Works for me on Python 2.5 on Linux running on "Intel(R) Core(TM)2 Duo
CPU". *What system are you on?

It could be that 2.5 is really 2.49999... which would round down to 2,
but on any modern CPU (using IEEE floating point), 2.5 should be
representable exactly.

However, as with any floating point calculations, if you expect exact
representation or calculations with any numbers, then you are misusing
floating points.

Gary Herron
I would think this is a common need, but I cannot find a function in
the Python library to do it. I wrote my own, but did I miss such a
method in my search of the Python library?
Thanks
--
http://mail.python.org/mailman/listinfo/python-list

I should reiterate that I am using Python 3.0 and not Python 2.x.
It looks like the behavior round() has changed between these two
versions.
Here is the documentation for round() in Python 3.0:
http://docs.python.org/dev/3.0/libra...ons.html#round

Of interest in this discussion is the second paragraph, which explains
the change:

Does anyone know the reason behind this change, and what replacement
method I can use to get the original behavior?
Jul 28 '08 #4
On Jul 27, 8:45*pm, pigmartian <scottp...@comc ast.netwrote:
it could be that 3.0 is using "banker's rounding" --- rounding to the
even digit. *the idea behind it behind it being to reduce error
accumulation when working with large sets of values.
Works for me on Python 2.5 on Linux running on "Intel(R) Core(TM)2 Duo
CPU". *What system are you on?
It could be that 2.5 is really 2.49999... which would round down to 2,
but on any modern CPU (using IEEE floating point), 2.5 should be
representable exactly.

That's exactly what's happening, pigmartian. Thank you for explaining
the reasoning behind this change.
So am I relegated to building my own round() function that behaves
like the original function? Or did they move the functionality to a
new method somewhere for backwards-compatibility?
Jul 28 '08 #5


Gary Herron wrote:
josh logan wrote:
>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:
>>>>round(2.5 )
>
2
Huh?
>>round(2.5)
3.0
As the OP said, PY 3.0, where statisticians' unbiased round to even was
explicitly adopted. (I think before it was maybe left to the C library?)
>I would think this is a common need,
If you need any particular rounding for legal/finance-rule reasons, you
probably need the decimal module, which has several rounding modes and
other features catering to money calculation rules.

For general data analysis with floats, round to even is better.

tjr

Jul 28 '08 #6
josh logan wrote:
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:
>>>round(0.5)
0
>>>round(1.5)
2
>>>round(2.5)
2
I would think this is a common need, but I cannot find a function in
the Python library to do it. I wrote my own, but did I miss such a
method in my search of the Python library?

Thanks
I think what you want is something like:

math.ceil(x-0.4999999999999 )

-Larry
Jul 28 '08 #7
On Jul 27, 8:55*pm, Larry Bates <larry.ba...@we bsafe.com`wrote :
josh logan wrote:
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:
>>round(0.5)
0
>>round(1.5)
2
>>round(2.5)
2
I would think this is a common need, but I cannot find a function in
the Python library to do it. I wrote my own, but did I miss such a
method in my search of the Python library?
Thanks

I think what you want is something like:

math.ceil(x-0.4999999999999 )

-Larry- Hide quoted text -

- Show quoted text -
The version I learned back in my FORTRAN days was:

int(x + 0.5)

-- Paul
Jul 28 '08 #8
josh logan wrote:
On Jul 27, 8:45 pm, pigmartian <scottp...@comc ast.netwrote:
>it could be that 3.0 is using "banker's rounding" --- rounding to the
even digit. the idea behind it behind it being to reduce error
accumulation when working with large sets of values.

>>Works for me on Python 2.5 on Linux running on "Intel(R) Core(TM)2 Duo
CPU". What system are you on?

It could be that 2.5 is really 2.49999... which would round down to 2,
but on any modern CPU (using IEEE floating point), 2.5 should be
representab le exactly.

That's exactly what's happening, pigmartian. Thank you for explaining
the reasoning behind this change.
So am I relegated to building my own round() function that behaves
like the original function? Or did they move the functionality to a
new method somewhere for backwards-compatibility?
This will work as you wish:
math.floor(x+0. 5)

Gary Herron

--
http://mail.python.org/mailman/listinfo/python-list
Jul 28 '08 #9
On Jul 28, 12:34*am, Gary Herron <gher...@island training.comwro te:
This will work as you wish:
* math.floor(x+0. 5)
This works fine for positive x but what about negative:
>>round(2.5)
3.0
>>floor(2.5 + 0.5)
3.0
>>round(-2.5)
-3.0
>>floor(-2.5 + 0.5)
-2.0

Maybe:

def round2(x):
return math.floor(x + (0.5 if x >= 0 else -0.5))
Jul 28 '08 #10

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

Similar topics

17
5655
by: nomenklatura | last post by:
Hi, System.Math.Round function is confused me. for example i want to round 3.245 in with decimal symbol Result should be = 3.25 When i try to this in vb: A = 3.245 X = Round(A, 2) then x=3.24 , result is is false
9
7383
by: Ronald W. Roberts | last post by:
I'm having a problem understanding the Round function. Below are quotes from two books on VB.NET. The first book shows examples with one argument and how it rounds. The second book something different. Programming Microsoft Windows with Microsoft Visual Basic.NET "The Round method with a single argument return the whole number nearest to the argument. If the argument to Round is midway between two whole numbers,
6
1513
by: Paul | last post by:
I can't round my numbers. i have a single, and i'm trying to round it so that there's only one decimal place. here's what i've tried: ..Val = CStr(Format(sngTimeToTarget, "#0.0")) and i also tried ..Val = CStr(round(CDbl(sngTimeToTarget), 1)) neither of which do anything. i've even tried multiplying by 10, coercing to an integer, coercing back to a single, and then dividing by 10 - this will cause an overflow arithmatic error.
4
3442
by: Chris Davoli | last post by:
The folllowing will round to 526, but it should round to 527. It works correctly for all other numbers, except for this one. Does anybody know of a bug in Math.Round? Dim ldecWater As Decimal = 526.5 CType(Math.Round(ldecWater, 0), String) -- Chris Davoli
6
9112
by: Zeng | last post by:
Math.Round has good behavior as following: Math.Round(3.45, 1); //Returns 3.4. The last '5' is thrown away because 4 is even Math.Round(3.75, 1); //Returns 3.8. The last '5' is used because '7' is odd However, if format.NumberDecimalDigits is 1 decimal d = 3.45M; d.ToString( "F", format ); //Return 3.5 - this is different from Math.Round;
4
3497
by: Jassim Rahma | last post by:
I have a number, for example 0.152 or 1.729 and I want to allow to round to the first 0.010 or 0.050 or 0.100
4
10892
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 !!!!
0
1012
by: Maric Michaud | last post by:
Le Monday 28 July 2008 02:35:08 Herman, vous avez écrit : Hmm, I don't have the same result in python2.6, I suspect it's a floating point precision problem, try just to type "0.5" in the console to see the exact representation of this value on your system, it may be just over or just down by a small quantity. On mine with 2.6 this typically give : ...: 0.5
10
3745
karthickkuchanur
by: karthickkuchanur | last post by:
Hai All I am ask to do the rounding the Double value Value =16.415; public static double roundoff(double value,int roundingFactor){ double roundOffValue; BigDecimal bigdecimal = new BigDecimal(value); bigdecimal = bigdecimal.setScale(roundingFactor, bigdecimal.ROUND_HALF_EVEN);
0
8987
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
8826
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
9366
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
9316
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
9241
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
8239
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
6793
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...
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
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.