473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Decimal ROUND_HALF_EVEN Default

Hello,

I'm just curious as to why the default rounding in the decimal module is
ROUND_HALF_EVEN instead of ROUND_HALF_UP. All of the decimal arithmetic I do
is rounded half up and I can't think of why one might use round half even. I
googled a bit and didn't really find a good answer. Any insight is
appreciated.

Louis
Jan 17 '06 #1
9 6057
"3c273" <no****@nospam.com> wrote in
news:dq*********@enews4.newsguy.com:
Hello,

I'm just curious as to why the default rounding in the decimal module
is ROUND_HALF_EVEN instead of ROUND_HALF_UP. All of the decimal
arithmetic I do is rounded half up and I can't think of why one might
use round half even. I googled a bit and didn't really find a good
answer. Any insight is appreciated.

Louis


see http://www2.hursley.ibm.com/decimal/...tml#refdefcont

decimal.py was written following the specification at

http://www2.hursley.ibm.com/decimal/decarith.html

max

Jan 17 '06 #2
[3c273]
I'm just curious as to why the default rounding in the decimal module is
ROUND_HALF_EVEN instead of ROUND_HALF_UP.
Because it's the best (numerically "fairest") rounding method for most
people most of the time.
All of the decimal arithmetic I do is rounded half up and I can't think of
why one might use round half even.


Because you want better numeric results, or because your application
requires it. "Half-even" is also called "banker's rounding" in the
United States, because it's required in many (but not all) banking
applications.
Jan 17 '06 #3
If 'bankers rounding' is HALF_ROUND_EVEN, what is HALF_ROUND_UP? I
confess to never having heard the terms.
I usually do: Y = int(X + 0.5) scaled to proper # of decimal places.
Which type of rounding is this? If either.

Jan 17 '06 #4
LordLaraby wrote:
If 'bankers rounding' is HALF_ROUND_EVEN, what is HALF_ROUND_UP? I
confess to never having heard the terms.
I usually do: Y = int(X + 0.5) scaled to proper # of decimal places.
Which type of rounding is this? If either.

Google Is Your Friend:

http://www.diycalculator.com/popup-m-round.shtml

--
Steven.

Jan 17 '06 #5
LordLaraby wrote:
If 'bankers rounding' is HALF_ROUND_EVEN, what is HALF_ROUND_UP? I
confess to never having heard the terms.
The terms are defined in the docs for the Context object:
http://docs.python.org/lib/decimal-decimal.html

The rounding option is one of:
------------------------------
ROUND_CEILING (towards Infinity),
ROUND_DOWN (towards zero),
ROUND_FLOOR (towards -Infinity),
ROUND_HALF_DOWN (to nearest with ties going towards zero),
ROUND_HALF_EVEN (to nearest with ties going to nearest even integer),
ROUND_HALF_UP (to nearest with ties going away from zero), or
ROUND_UP (away from zero).

I usually do: Y = int(X + 0.5) scaled to proper # of decimal places.
Which type of rounding is this? If either.


The interpreter shows that ties are rounded down towards zero:
[int(x + 0.5) for x in range(-5, 6)]

[-4, -3, -2, -1, 0, 0, 1, 2, 3, 4, 5]
Raymond

Jan 17 '06 #6
[LordLaraby]
If 'bankers rounding' is HALF_ROUND_EVEN, what is HALF_ROUND_UP?
Not banker's rounding ;-). Same answer if you had said ROUND_HALF_UP
instead (which I assume you intended) -- most of these don't have
cute names.
I confess to never having heard the terms.
ROUND_HALF_UP etc are symbolic constants in Python's `decimal` module;
see the docs.
I usually do: Y = int(X + 0.5) scaled to proper # of decimal places.
Which type of rounding is this? If either.


If you meant what you said, it's not "rounding" at all, because it's
insane for negative inputs. For example, int(-2 + 0.5) = int(-1.5) =
-1, and no _rounding_ method changes an exact integer (like -2) to a
_different_ exact integer (like -1).

If you were assuming X >= 0.0, then int(X+0.5) coincides with
ROUND_HALF_UP on that domain. For X < 0.0, ROUND_HALF_UP works like
int(X-0.5).
Jan 17 '06 #7
LordLaraby wrote:
If 'bankers rounding' is HALF_ROUND_EVEN, what is HALF_ROUND_UP? I
confess to never having heard the terms.


There was a Slashdot article on rounding a short while back:

http://developers.slashdot.org/artic.../01/05/1838214
Jan 17 '06 #8
Thanks to all! Interesting reading.

Louis
Jan 17 '06 #9
On 16 Jan 2006 20:36:12 -0800, "Raymond Hettinger" <py****@rcn.com> wrote:
LordLaraby wrote:
If 'bankers rounding' is HALF_ROUND_EVEN, what is HALF_ROUND_UP? I
confess to never having heard the terms.


The terms are defined in the docs for the Context object:
http://docs.python.org/lib/decimal-decimal.html

The rounding option is one of:
------------------------------
ROUND_CEILING (towards Infinity),
ROUND_DOWN (towards zero),
ROUND_FLOOR (towards -Infinity),
ROUND_HALF_DOWN (to nearest with ties going towards zero),
ROUND_HALF_EVEN (to nearest with ties going to nearest even integer),
ROUND_HALF_UP (to nearest with ties going away from zero), or
ROUND_UP (away from zero).

I usually do: Y = int(X + 0.5) scaled to proper # of decimal places.
Which type of rounding is this? If either.


The interpreter shows that ties are rounded down towards zero:
[int(x + 0.5) for x in range(-5, 6)][-4, -3, -2, -1, 0, 0, 1, 2, 3, 4, 5]

Or more explicitly:
for x in xrange(-5,6): print 'int(%2s + 0.5) == int(%4s) => %2s'%(x,x+.5,int(x+.5)) ...
int(-5 + 0.5) == int(-4.5) => -4
int(-4 + 0.5) == int(-3.5) => -3
int(-3 + 0.5) == int(-2.5) => -2
int(-2 + 0.5) == int(-1.5) => -1
int(-1 + 0.5) == int(-0.5) => 0
int( 0 + 0.5) == int( 0.5) => 0
int( 1 + 0.5) == int( 1.5) => 1
int( 2 + 0.5) == int( 2.5) => 2
int( 3 + 0.5) == int( 3.5) => 3
int( 4 + 0.5) == int( 4.5) => 4
int( 5 + 0.5) == int( 5.5) => 5

So one must be careful not to use int to convert relative screen positions to relative pixel deltas
to add to some pixel screen position, even when it's a single add that can't accumulate error or
round differentely after the add.

I guess one would want for x in xrange(-5,6): print 'int(math.floor(%4s)) => %2s'%(x+.5,int(math.floor(x+.5)))

...
int(math.floor(-4.5)) => -5
int(math.floor(-3.5)) => -4
int(math.floor(-2.5)) => -3
int(math.floor(-1.5)) => -2
int(math.floor(-0.5)) => -1
int(math.floor( 0.5)) => 0
int(math.floor( 1.5)) => 1
int(math.floor( 2.5)) => 2
int(math.floor( 3.5)) => 3
int(math.floor( 4.5)) => 4
int(math.floor( 5.5)) => 5

Which ISTM is the way some other int conversions have worked, but I can't recall the context ATM.
I.e., just trim the fractional bits from the theoretical fixed point twos complement number, which
always subtracts those positive-value fractional bits algebraically.

Regards,
Bengt Richter
Jan 17 '06 #10

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

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
2
by: Mike in Paradise | last post by:
Can you use the DefaultValueAttribute on Decimal PropertyFields? If so what is format for setting the Default value. Thanks... Example /// <summary> /// Field: increment value ///...
1
by: | last post by:
eg. "19.121" to 19.12 decimal eg. "12.567 " to 12.56 decimal
3
by: Daniel | last post by:
I'm writing an application that (among other things) evaluates mathematical expressions. The user enters strings containing literals and names that later get evaluated using the Python interpreter....
3
by: Boot2TheHead | last post by:
This one cost me a solid half hour yesterday. I'm wondering why on earth the default precision for a decimal type is 18,0. Maybe I'm mistaken. A decimal datatype sort of implies that you'd want...
23
by: neha_chhatre | last post by:
which is the best format specifier(data type) if i have to work with decimal number. also please tell me the syntax for truncating a decimal number please reply as soon as possible
6
by: Terry Reedy | last post by:
Gerhard Häring wrote: The new fractions module acts differently, which is to say, as most would want. True Traceback (most recent call last): File "<pyshell#20>", line 1, in <module> F(1.0)...
10
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.