Re: round function error???

2008-07-20 Thread Mark Dickinson
On Jul 19, 12:20 am, John Machin [EMAIL PROTECTED] wrote:
 On Jul 19, 8:05 am, Mark Dickinson [EMAIL PROTECTED] wrote:
  for more information.  But I'm guessing that you're
  questioning the fact that a value that's apparently
  *less* than 3499.35 is rounded up to 3499.4, rather
  than down to 3499.3.  ?

 apparently being the operative word.

Well, it's not just an apparent problem:  the closest
floating-point number to 3499.35 really *is* less than
3499.35.  A nice way to check this is using the new
fractions module in 2.6, which allows exact conversions
of floats and Decimals into rational numbers:

Python 2.6b2+ (trunk:65155, Jul 20 2008, 15:39:46)
[GCC 4.0.1 (Apple Inc. build 5484)] on darwin
Type help, copyright, credits or license for more information.
 from decimal import Decimal
 from fractions import Fraction
 x = 3499.35
 y = Decimal('3499.35')
 Fraction.from_float(x)
Fraction(7695152029315891, 219902322)
 Fraction.from_decimal(y)
Fraction(69987, 20)
[54933 refs]
 Fraction.from_float(x)  Fraction.from_decimal(y)
True

Mark
--
http://mail.python.org/mailman/listinfo/python-list


Re: round function error???

2008-07-20 Thread John Machin
On Jul 21, 12:56 am, Mark Dickinson [EMAIL PROTECTED] wrote:
 On Jul 19, 12:20 am, John Machin [EMAIL PROTECTED] wrote:

  On Jul 19, 8:05 am, Mark Dickinson [EMAIL PROTECTED] wrote:
   for more information.  But I'm guessing that you're
   questioning the fact that a value that's apparently
   *less* than 3499.35 is rounded up to 3499.4, rather
   than down to 3499.3.  ?

  apparently being the operative word.

 Well, it's not just an apparent problem:  the closest
 floating-point number to 3499.35 really *is* less than
 3499.35.

I'm well aware of that. My point is that I hope that you weren't
planning on changing that behaviour in an unannounced unstaged
manner.

--
http://mail.python.org/mailman/listinfo/python-list


round function error???

2008-07-18 Thread Anthony
Isn't this a mistake???

 round(3499.349439034,44)
3499.3494390340002
 round(_,2)
3499.34999
 round(_,1)
3499.40001

My Python 2.5.1 spat that out..
--
http://mail.python.org/mailman/listinfo/python-list


Re: round function error???

2008-07-18 Thread Mark Dickinson
On Jul 18, 10:17 pm, Anthony [EMAIL PROTECTED] wrote:
 Isn't this a mistake???

Which 'this'?  That is, what were you expecting?

If you're objecting to the fact that the second result
produces 3499.34999 instead of 3499.35, then
no, that's not a mistake;  see

http://www.python.org/doc/faq/general/#why-are-floating-point-calculations-so-inaccurate

for more information.  But I'm guessing that you're
questioning the fact that a value that's apparently
*less* than 3499.35 is rounded up to 3499.4, rather
than down to 3499.3.  ?

Then yes, I'd agree that's less than ideal, though I
don't consider it a particularly serious bug.
It's been on my list of things to fix for a while.
(See http://bugs.python.org/issue1869 ).
Contributions welcome!

Mark
--
http://mail.python.org/mailman/listinfo/python-list


Re: round function error???

2008-07-18 Thread Terry Reedy



Anthony wrote:

Isn't this a mistake???


round(3499.349439034,44)

3499.3494390340002

round(_,2)

3499.34999

round(_,1)

3499.40001

My Python 2.5.1 spat that out..


No, round() return binary floats that, in general, cannot represent 
decimal floats exactly.  Formatted printing gives what you expect.

 '%8.2f' % x
' 3499.35'

--
http://mail.python.org/mailman/listinfo/python-list


Re: round function error???

2008-07-18 Thread Mark Dickinson
On Jul 18, 11:15 pm, Terry Reedy [EMAIL PROTECTED] wrote:
 No, round() return binary floats that, in general, cannot represent
 decimal floats exactly.  Formatted printing gives what you expect.
   '%8.2f' % x
 ' 3499.35'

Sure.  But it's still true that the second printed value
(printed as 3499.34999) is strictly less than 3499.35,
so when rounding to 1 decimal place an ideal rounding routine
would round it *down* to 3499.3 instead of up to 3499.4.  This
is what '%.1f' does, for example, on a platform where printf
does correct rounding:

Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type help, copyright, credits or license for more information.
 x = 3499.35
 x
3499.34999
 print '%.1f' % x
3499.3
 print round(x, 1)
3499.4

Mark
--
http://mail.python.org/mailman/listinfo/python-list