Re: Rounding a number to nearest even

2008-04-17 Thread Lie
On Apr 13, 7:20 pm, Steve Holden [EMAIL PROTECTED] wrote: Lie wrote: On Apr 12, 3:44 am, hdante [EMAIL PROTECTED] wrote: [snip] In short, choosing that x.0 is rounded down and x.5 is rounded up is arbitrary but not without a reason. Don't arbitrary and not without a reason directly

Re: Rounding a number to nearest even

2008-04-15 Thread Thomas Dybdahl Ahle
On Fri, 2008-04-11 at 03:14 -0700, bdsatish wrote: 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

Re: Rounding a number to nearest even

2008-04-15 Thread Chris
On Apr 15, 11:22 am, Sjoerd Mullender [EMAIL PROTECTED] wrote: Thomas Dybdahl Ahle wrote: On Fri, 2008-04-11 at 03:14 -0700, bdsatish wrote: 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

Re: Rounding a number to nearest even

2008-04-15 Thread Sjoerd Mullender
Thomas Dybdahl Ahle wrote: On Fri, 2008-04-11 at 03:14 -0700, bdsatish wrote: 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) =

Re: Rounding a number to nearest even

2008-04-15 Thread Duncan Booth
Chris [EMAIL PROTECTED] wrote: even is closer to even.75 than even+1.25. Why should it be rounded up ? Because the OP wants to round values to the nearest integer. Only values of the form 'x.5' which have two nearest values use 'nearest even' to disambiguate the result. See

Re: Rounding a number to nearest even

2008-04-15 Thread colas . francis
On 14 avr, 20:02, Thomas Dybdahl Ahle [EMAIL PROTECTED] wrote: On Fri, 2008-04-11 at 03:14 -0700, bdsatish wrote: 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) =

Re: Rounding a number to nearest even

2008-04-15 Thread Chris
On Apr 15, 12:33 pm, Chris [EMAIL PROTECTED] wrote: On Apr 15, 11:47 am, Duncan Booth [EMAIL PROTECTED] wrote: Chris [EMAIL PROTECTED] wrote: even is closer to even.75 than even+1.25.  Why should it be rounded up ? Because the OP wants to round values to the nearest integer. Only

Re: Rounding a number to nearest even

2008-04-15 Thread Chris
On Apr 15, 11:47 am, Duncan Booth [EMAIL PROTECTED] wrote: Chris [EMAIL PROTECTED] wrote: even is closer to even.75 than even+1.25.  Why should it be rounded up ? Because the OP wants to round values to the nearest integer. Only values of the form 'x.5' which have two nearest values use

Re: Rounding a number to nearest even

2008-04-15 Thread Arnaud Delobelle
On 11 Apr, 21:29, Gabriel Genellina [EMAIL PROTECTED] wrote: ... If the numbers to be rounded come from a measurement, the left column is not just a number but the representant of an interval (as Mikael said, the're quantized). 2.3 means that the measurement was closer to 2.3 than to 2.2 or

Re: Rounding a number to nearest even

2008-04-14 Thread [EMAIL PROTECTED]
On Apr 11, 4:14 am, bdsatish [EMAIL PROTECTED] wrote: 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 ? Side note: A specialized use for this is in the US Field Artillery, where it's called artillery expression.

Re: Rounding a number to nearest even

2008-04-13 Thread Lie
On Apr 12, 3:44 am, hdante [EMAIL PROTECTED] wrote: (snip) In this table, we consider that a number is rounded down when the But then, the Round up table gives inconsistent results if, by the same argument, we consider 2.0 - 2 rounding up. (you get 12 round ups and 8 round downs just by

Re: Rounding a number to nearest even

2008-04-13 Thread Steve Holden
Lie wrote: On Apr 12, 3:44 am, hdante [EMAIL PROTECTED] wrote: [snip] In short, choosing that x.0 is rounded down and x.5 is rounded up is arbitrary but not without a reason. Don't arbitrary and not without a reason directly contradict one another? regards Steve -- Steve Holden

Re: Rounding a number to nearest even

2008-04-13 Thread Mark Dickinson
On Apr 13, 4:18 am, Lie [EMAIL PROTECTED] wrote: [...] it and there is nothing else in it, but in the second number range (barely above 1 to 2) the number 1.0 is not included while the number 2.0 is contained in it, clearly not a clean separation of numbers in the form of y.x where y is

Rounding a number to nearest even

2008-04-11 Thread bdsatish
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

Re: Rounding a number to nearest even

2008-04-11 Thread colas . francis
On 11 avr, 12:14, bdsatish [EMAIL PROTECTED] wrote: 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

Re: Rounding a number to nearest even

2008-04-11 Thread cokofreedom
couldn't you just do. #untested new_round(n): answer = round(n) # is answer now odd if answer % 2: return answer - 1 else: return answer -- http://mail.python.org/mailman/listinfo/python-list

Re: Rounding a number to nearest even

2008-04-11 Thread cokofreedom
On Apr 11, 1:19 pm, [EMAIL PROTECTED] wrote: couldn't you just do. #untested new_round(n): answer = round(n) # is answer now odd if answer % 2: return answer - 1 else: return answer Whoops, this also affects odd numbers... Will try and find a GOOD solution later...

Re: Rounding a number to nearest even

2008-04-11 Thread bdsatish
On Apr 11, 3:27 pm, [EMAIL PROTECTED] wrote: On 11 avr, 12:14, bdsatish [EMAIL PROTECTED] wrote: 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

Re: Rounding a number to nearest even

2008-04-11 Thread bdsatish
On Apr 11, 4:37 pm, Scott David Daniels [EMAIL PROTECTED] wrote: bdsatish wrote: The built-in function round( ) will always round up, that is 1.5 is def rounded(v): rounded = round(v) if divmod(v, 1)[1] == .5 and divmod(rounded, 2)[1] == 1: if v 0: return

Re: Rounding a number to nearest even

2008-04-11 Thread bdsatish
On Apr 11, 4:19 pm, [EMAIL PROTECTED] wrote: couldn't you just do. #untested new_round(n): answer = round(n) # is answer now odd if answer % 2: return answer - 1 else: return answer It fails for negative numbers: For -2.5 it gives -4.0 as answer whereas I expect -2.0 --

Re: Rounding a number to nearest even

2008-04-11 Thread bdsatish
On Apr 11, 4:24 pm, [EMAIL PROTECTED] wrote: On Apr 11, 1:19 pm, [EMAIL PROTECTED] wrote: couldn't you just do. #untested new_round(n): answer = round(n) # is answer now odd if answer % 2: return answer - 1 else: return answer Whoops, this also affects odd

Re: Rounding a number to nearest even

2008-04-11 Thread Scott David Daniels
bdsatish wrote: 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

Re: Rounding a number to nearest even

2008-04-11 Thread Gerard Flanagan
On Apr 11, 12:14 pm, bdsatish [EMAIL PROTECTED] wrote: 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

Re: Rounding a number to nearest even

2008-04-11 Thread Gerard Flanagan
On Apr 11, 2:05 pm, Gerard Flanagan [EMAIL PROTECTED] wrote: On Apr 11, 12:14 pm, bdsatish [EMAIL PROTECTED] wrote: 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

Re: Rounding a number to nearest even

2008-04-11 Thread bdsatish
HI Gerard, I think you've taken it to the best possible implementation. Thanks ! On Apr 11, 5:14 pm, Gerard Flanagan [EMAIL PROTECTED] wrote: In fact you can avoid the call to the builtin round: def myround(x): n = int(x) if abs(x -

Re: Rounding a number to nearest even

2008-04-11 Thread colas . francis
On 11 avr, 14:14, Gerard Flanagan [EMAIL PROTECTED] wrote: On Apr 11, 2:05 pm, Gerard Flanagan [EMAIL PROTECTED] wrote: On Apr 11, 12:14 pm, bdsatish [EMAIL PROTECTED] wrote: 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.

Re: Rounding a number to nearest even

2008-04-11 Thread bdsatish
On Apr 11, 5:33 pm, bdsatish [EMAIL PROTECTED] wrote: HI Gerard, I think you've taken it to the best possible implementation. Thanks ! On Apr 11, 5:14 pm, Gerard Flanagan [EMAIL PROTECTED] wrote: In fact you can avoid the call to the builtin round:

Re: Rounding a number to nearest even

2008-04-11 Thread Ivan Illarionov
On Apr 11, 2:14 pm, bdsatish [EMAIL PROTECTED] wrote: 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

Re: Rounding a number to nearest even

2008-04-11 Thread hdante
On Apr 11, 9:45 am, bdsatish [EMAIL PROTECTED] wrote: On Apr 11, 5:33 pm, bdsatish [EMAIL PROTECTED] wrote: HI Gerard, I think you've taken it to the best possible implementation. Thanks ! On Apr 11, 5:14 pm, Gerard Flanagan [EMAIL PROTECTED] wrote: In fact you can avoid the call to

Re: Rounding a number to nearest even

2008-04-11 Thread Ivan Illarionov
On Apr 11, 5:49 pm, hdante [EMAIL PROTECTED] wrote: On Apr 11, 9:45 am, bdsatish [EMAIL PROTECTED] wrote: On Apr 11, 5:33 pm, bdsatish [EMAIL PROTECTED] wrote: HI Gerard, I think you've taken it to the best possible implementation. Thanks ! On Apr 11, 5:14 pm, Gerard Flanagan

Re: Rounding a number to nearest even

2008-04-11 Thread hdante
On Apr 11, 11:13 am, Ivan Illarionov [EMAIL PROTECTED] wrote: Shorter version: def round3k(x): return x % 1 != 0.5 and round(x) or round(x / 2.) * 2. Strangely, a faster version is: def fast_round(x): if x % 1 != 0.5: return round(x) return 2.0*round(x/2.0) nums = [ 0, 2,

Re: Rounding a number to nearest even

2008-04-11 Thread Graham Breed
On Apr 11, 6:14 pm, bdsatish [EMAIL PROTECTED] wrote: 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

Re: Rounding a number to nearest even

2008-04-11 Thread Mikael Olofsson
[EMAIL PROTECTED] commented about rounding towards even numbers from mid-way between integers as opposed to for instance always rounding up in those cases: Strange request though, why do you need it that way, because 2.5 is CLOSER to 3 than to 2... That's exactly how I was taught to do

Re: Rounding a number to nearest even

2008-04-11 Thread Robert Kern
[EMAIL PROTECTED] wrote: Strange request though, why do you need it that way, because 2.5 is CLOSER to 3 than to 2... Uhhh, no it isn't. (3 - 2.5) == (2.5 - 2) -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad

Re: Rounding a number to nearest even

2008-04-11 Thread Mark Dickinson
On Apr 11, 10:29 am, hdante [EMAIL PROTECTED] wrote: Strangely, a faster version is: def fast_round(x): if x % 1 != 0.5: return round(x) return 2.0*round(x/2.0) You should be a little bit careful with the test x%1 == 0.5 if x might be negative: x = -0.5 + 2**-54 x # not an

Re: Rounding a number to nearest even

2008-04-11 Thread Lie
On Apr 11, 10:19 pm, Mikael Olofsson [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] commented about rounding towards even numbers from mid-way between integers as opposed to for instance always rounding up in those cases: Strange request though, why do you need it that way, because 2.5 is

Re: Rounding a number to nearest even

2008-04-11 Thread Mark Dickinson
On Apr 11, 2:33 pm, Lie [EMAIL PROTECTED] wrote: In this table, we consider that a number is rounded down when the number is equal to truncated value (the number without fractional part), while round up is equal to truncated value + 1 or truncated value -1 if value is negative (Actually this

Re: Rounding a number to nearest even

2008-04-11 Thread Gabriel Genellina
On 11 abr, 15:33, Lie [EMAIL PROTECTED] wrote: On Apr 11, 10:19 pm, Mikael Olofsson [EMAIL PROTECTED] wrote: That's exactly how I was taught to do rounding in what-ever low-level class it was. The idea is to avoid a bias, which assumes that the original values are already quantized. Assume

Re: Rounding a number to nearest even

2008-04-11 Thread hdante
On Apr 11, 3:33 pm, Lie [EMAIL PROTECTED] wrote: That old-school rounding method you're taught is based on a wrong assumption of the nature of number. In the past, rounding algorithm is based on this: Original = (RoundUp(u|d|n), RoundNearestEven(u|d|n) ... 1.0 = 1(n), 1(n) 1.1 = 1(d),

Re: Rounding a number to nearest even

2008-04-11 Thread Arnaud Delobelle
On Apr 11, 8:27 pm, Mark Dickinson [EMAIL PROTECTED] wrote: On Apr 11, 2:33 pm, Lie [EMAIL PROTECTED] wrote: [...] Another mistake, in an unquantized value the probability of getting exactly 0.5 (or any other number specified) is not 0 but an infinitesimal (i.e. lim(x) where x - 0 (BUT NOT