Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 17th, 2006, 12:25 AM
3c273
Guest
 
Posts: n/a
Default 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


  #2  
Old January 17th, 2006, 12:35 AM
Max Erickson
Guest
 
Posts: n/a
Default Re: Decimal ROUND_HALF_EVEN Default

"3c273" <nospam@nospam.com> wrote in
news:dqhb9v03088@enews4.newsguy.com:[color=blue]
> 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
>
>[/color]

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

  #3  
Old January 17th, 2006, 12:45 AM
Tim Peters
Guest
 
Posts: n/a
Default Re: Decimal ROUND_HALF_EVEN Default

[3c273][color=blue]
> I'm just curious as to why the default rounding in the decimal module is
> ROUND_HALF_EVEN instead of ROUND_HALF_UP.[/color]

Because it's the best (numerically "fairest") rounding method for most
people most of the time.
[color=blue]
> All of the decimal arithmetic I do is rounded half up and I can't think of
> why one might use round half even.[/color]

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.
  #4  
Old January 17th, 2006, 01:55 AM
LordLaraby
Guest
 
Posts: n/a
Default Re: Decimal ROUND_HALF_EVEN Default

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.

  #5  
Old January 17th, 2006, 02:35 AM
Steven D'Aprano
Guest
 
Posts: n/a
Default Re: Decimal ROUND_HALF_EVEN Default

LordLaraby wrote:
[color=blue]
> 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.[/color]


Google Is Your Friend:

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



--
Steven.

  #6  
Old January 17th, 2006, 04:45 AM
Raymond Hettinger
Guest
 
Posts: n/a
Default Re: Decimal ROUND_HALF_EVEN Default

LordLaraby wrote:[color=blue]
> If 'bankers rounding' is HALF_ROUND_EVEN, what is HALF_ROUND_UP? I
> confess to never having heard the terms.[/color]

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).

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

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


Raymond

  #7  
Old January 17th, 2006, 03:05 PM
Tim Peters
Guest
 
Posts: n/a
Default Re: Decimal ROUND_HALF_EVEN Default

[LordLaraby][color=blue]
> If 'bankers rounding' is HALF_ROUND_EVEN, what is HALF_ROUND_UP?[/color]

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.
[color=blue]
> I confess to never having heard the terms.[/color]

ROUND_HALF_UP etc are symbolic constants in Python's `decimal` module;
see the docs.
[color=blue]
> I usually do: Y = int(X + 0.5) scaled to proper # of decimal places.
> Which type of rounding is this? If either.[/color]

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).
  #8  
Old January 17th, 2006, 03:25 PM
Rocco Moretti
Guest
 
Posts: n/a
Default Re: Decimal ROUND_HALF_EVEN Default

LordLaraby wrote:[color=blue]
> If 'bankers rounding' is HALF_ROUND_EVEN, what is HALF_ROUND_UP? I
> confess to never having heard the terms.[/color]

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

http://developers.slashdot.org/artic.../01/05/1838214
  #9  
Old January 17th, 2006, 04:25 PM
3c273
Guest
 
Posts: n/a
Default Re: Decimal ROUND_HALF_EVEN Default

Thanks to all! Interesting reading.

Louis


  #10  
Old January 17th, 2006, 07:15 PM
Bengt Richter
Guest
 
Posts: n/a
Default Re: Decimal ROUND_HALF_EVEN Default

On 16 Jan 2006 20:36:12 -0800, "Raymond Hettinger" <python@rcn.com> wrote:
[color=blue]
>LordLaraby wrote:[color=green]
>> If 'bankers rounding' is HALF_ROUND_EVEN, what is HALF_ROUND_UP? I
>> confess to never having heard the terms.[/color]
>
>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).
>
>[color=green]
>> I usually do: Y = int(X + 0.5) scaled to proper # of decimal places.
>> Which type of rounding is this? If either.[/color]
>
>The interpreter shows that ties are rounded down towards zero:
>[color=green][color=darkred]
>>>> [int(x + 0.5) for x in range(-5, 6)][/color][/color]
>[-4, -3, -2, -1, 0, 0, 1, 2, 3, 4, 5]
>[/color]
Or more explicitly:[color=blue][color=green][color=darkred]
>>> for x in xrange(-5,6): print 'int(%2s + 0.5) == int(%4s) => %2s'%(x,x+.5,int(x+.5))[/color][/color][/color]
...
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[color=blue][color=green][color=darkred]
>>> for x in xrange(-5,6): print 'int(math.floor(%4s)) => %2s'%(x+.5,int(math.floor(x+.5)))[/color][/color][/color]
...
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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles