floating point woes

2011-02-15 Thread Hans-Peter Jansen
Hi,

while I usually cope with the woes of floating point issues, this is 
one, that I didn't expect:

 round(2.385, 2)
2.3799

Doesn't the docs say, it's rounded up for this case?

quote
Values are rounded to the closest multiple of 10 to the power minus n; 
if two multiples are equally close, rounding is done away from 0
/quote

Well, that one is clearly rounding down.

What's up, eh, down here?

Pete

Python 2.6 (r26:66714, Feb  8 2011, 08:50:11) 
[GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2

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


Re: floating point woes

2011-02-15 Thread André Roberge
On Tuesday, February 15, 2011 7:49:34 PM UTC-4, Hans-Peter Jansen wrote:
 Hi,
 
 while I usually cope with the woes of floating point issues, this is 
 one, that I didn't expect:
 
  round(2.385, 2)
 2.3799
 
 Doesn't the docs say, it's rounded up for this case?

The problem is probably that 2.385 can not be represented as 2.3850

 a = 2.385
 a
2.3848

André
 
 quote
 Values are rounded to the closest multiple of 10 to the power minus n; 
 if two multiples are equally close, rounding is done away from 0
 /quote
 
 Well, that one is clearly rounding down.
 
 What's up, eh, down here?
 
 Pete
 
 Python 2.6 (r26:66714, Feb  8 2011, 08:50:11) 
 [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2

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


Re: floating point woes

2011-02-15 Thread Westley Martínez
On Wed, 2011-02-16 at 00:49 +0100, Hans-Peter Jansen wrote:
 Hi,
 
 while I usually cope with the woes of floating point issues, this is 
 one, that I didn't expect:
 
  round(2.385, 2)
 2.3799
 
 Doesn't the docs say, it's rounded up for this case?
 
 quote
 Values are rounded to the closest multiple of 10 to the power minus n; 
 if two multiples are equally close, rounding is done away from 0
 /quote
 
 Well, that one is clearly rounding down.
 
 What's up, eh, down here?
 
 Pete
 
 Python 2.6 (r26:66714, Feb  8 2011, 08:50:11) 
 [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2
 
It's actually rounding up, but 2.38 cannot be represented precisely by
floating points. This was fixed in Python 3.1.

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


Re: floating point woes

2011-02-15 Thread Benjamin Kaplan
On Tue, Feb 15, 2011 at 6:49 PM, Hans-Peter Jansen h...@urpla.net wrote:
 Hi,

 while I usually cope with the woes of floating point issues, this is
 one, that I didn't expect:

 round(2.385, 2)
 2.3799

 Doesn't the docs say, it's rounded up for this case?

 quote
 Values are rounded to the closest multiple of 10 to the power minus n;
 if two multiples are equally close, rounding is done away from 0
 /quote

 Well, that one is clearly rounding down.

 What's up, eh, down here?

 Pete


The number you are rounding is not 2.385. It is not possible to
represent that number in binary, just like you cannot represent the
value 1/3 in decimal. So instead, you're using the nearest
approximation that an IEEE 754 Double-Precision Floating Point number
can get you, which happens to be about 2.3848. And that
rounds down to 2.38. Which also cannot be precisely represented in
binary, so you get 2.3799 instead.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: floating point woes

2011-02-15 Thread Ian Kelly
On Tue, Feb 15, 2011 at 4:49 PM, Hans-Peter Jansen h...@urpla.net wrote:
 Hi,

 while I usually cope with the woes of floating point issues, this is
 one, that I didn't expect:

 round(2.385, 2)
 2.3799

 Doesn't the docs say, it's rounded up for this case?

 quote
 Values are rounded to the closest multiple of 10 to the power minus n;
 if two multiples are equally close, rounding is done away from 0
 /quote

 Well, that one is clearly rounding down.

 What's up, eh, down here?

Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on
win32
Type help, copyright, credits or license for more information.
 2.385
2.3848

Looks to me like it's working as expected...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: floating point woes

2011-02-15 Thread Chris Rebert
On Tue, Feb 15, 2011 at 3:49 PM, Hans-Peter Jansen h...@urpla.net wrote:
 Hi,

 while I usually cope with the woes of floating point issues, this is
 one, that I didn't expect:

 round(2.385, 2)
 2.3799

 Doesn't the docs say, it's rounded up for this case?

 quote
 Values are rounded to the closest multiple of 10 to the power minus n;
 if two multiples are equally close, rounding is done away from 0
 /quote

 Well, that one is clearly rounding down.

 What's up, eh, down here?

http://docs.python.org/library/functions.html#round :

Note: The behavior of round() for floats can be surprising: for
example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This
is not a bug: it’s a result of the fact that most decimal fractions
can’t be represented exactly as a float. See Floating Point
Arithmetic: Issues and Limitations[1] for more information.

[1]: http://docs.python.org/tutorial/floatingpoint.html

And indeed:
 from decimal import Decimal
 Decimal(2.385)
Decimal('2.3847868371792719699442386627197265625')

Which, rounded to 2 decimal places, gives us 2.38, which is in turn
approximated as:
2.37989341858963598497211933135986328125

I encourage reading [1].

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: floating point woes

2011-02-15 Thread Chris Rebert
On Tue, Feb 15, 2011 at 4:09 PM, Chris Rebert c...@rebertia.com wrote:
 On Tue, Feb 15, 2011 at 3:49 PM, Hans-Peter Jansen h...@urpla.net wrote:
 Hi,

 while I usually cope with the woes of floating point issues, this is
 one, that I didn't expect:

 round(2.385, 2)
 2.3799

 Doesn't the docs say, it's rounded up for this case?

 quote
 Values are rounded to the closest multiple of 10 to the power minus n;
 if two multiples are equally close, rounding is done away from 0
 /quote

 Well, that one is clearly rounding down.

 What's up, eh, down here?

 http://docs.python.org/library/functions.html#round :
 
 Note: The behavior of round() for floats can be surprising: for
 example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This
 is not a bug: it’s a result of the fact that most decimal fractions
 can’t be represented exactly as a float. See Floating Point
 Arithmetic: Issues and Limitations[1] for more information.
 
 [1]: http://docs.python.org/tutorial/floatingpoint.html

 And indeed:
 from decimal import Decimal
 Decimal(2.385)
 Decimal('2.3847868371792719699442386627197265625')

 Which, rounded to 2 decimal places, gives us 2.38, which is in turn
 approximated as:

[*whacks forehead hard*]
Nevermind.

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


Re: floating point woes

2011-02-15 Thread Hans-Peter Jansen
On Wednesday 16 February 2011, 01:06:08 Benjamin Kaplan wrote:
 On Tue, Feb 15, 2011 at 6:49 PM, Hans-Peter Jansen h...@urpla.net 
wrote:
  Hi,
 
  while I usually cope with the woes of floating point issues, this
  is
 
  one, that I didn't expect:
  round(2.385, 2)
 
  2.3799
 
  Doesn't the docs say, it's rounded up for this case?
 
  quote
  Values are rounded to the closest multiple of 10 to the power minus
  n; if two multiples are equally close, rounding is done away from 0
  /quote
 
  Well, that one is clearly rounding down.
 
  What's up, eh, down here?
 
  Pete

 The number you are rounding is not 2.385. It is not possible to
 represent that number in binary, just like you cannot represent the
 value 1/3 in decimal. So instead, you're using the nearest
 approximation that an IEEE 754 Double-Precision Floating Point number
 can get you, which happens to be about 2.3848. And that
 rounds down to 2.38. Which also cannot be precisely represented in
 binary, so you get 2.3799 instead.

Thanks for the explanation, Benjamin. Not that I like it, but anyway.
If I hadn't quitted smoking a long time ago, I would go and ask, what 
these engineers smoked during the course of inventing this sh*t. Even 
more probably, they took way too much of a special form of lysergic 
acid.

OTOH, cdecimals, as in Stefan Krah's package are long overdue to get 
into the core.

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


Re: floating point woes

2011-02-15 Thread Hans-Peter Jansen
On Wednesday 16 February 2011, 01:24:59 Chris Rebert wrote:
 On Tue, Feb 15, 2011 at 4:09 PM, Chris Rebert c...@rebertia.com 
wrote:
  On Tue, Feb 15, 2011 at 3:49 PM, Hans-Peter Jansen h...@urpla.net 
wrote:
  Hi,
 
  while I usually cope with the woes of floating point issues, this
  is
 
  one, that I didn't expect:
  round(2.385, 2)
 
  2.3799
 
  Doesn't the docs say, it's rounded up for this case?
 
  quote
  Values are rounded to the closest multiple of 10 to the power
  minus n; if two multiples are equally close, rounding is done away
  from 0 /quote
 
  Well, that one is clearly rounding down.
 
  What's up, eh, down here?
 
  http://docs.python.org/library/functions.html#round :
  
  Note: The behavior of round() for floats can be surprising: for
  example, round(2.675, 2) gives 2.67 instead of the expected 2.68.
  This is not a bug: it’s a result of the fact that most decimal
  fractions can’t be represented exactly as a float. See Floating
  Point Arithmetic: Issues and Limitations[1] for more information.
  
  [1]: http://docs.python.org/tutorial/floatingpoint.html
 
  And indeed:
  from decimal import Decimal
  Decimal(2.385)
 
  Decimal('2.3847868371792719699442386627197265625')
 
  Which, rounded to 2 decimal places, gives us 2.38, which is in turn
  approximated as:

If that only wouldn't be so arkward to use:

 from cdecimal import Decimal, ROUND_HALF_UP
 d = Decimal(2.385)
 d
Decimal('2.385')
 d.quantize(Decimal('1.00'))
Decimal('2.38')

hrmpf.

 d.quantize(Decimal('1.00'), ROUND_HALF_UP)
Decimal('2.39')

Oh, well. This is a bit too Cobolesque. (Yes, sure, I know, I can define 
any context, I like.)

 [*whacks forehead hard*]
 Nevermind.

Too true.

 - Chris

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


Re: floating point woes

2011-02-15 Thread Grant Edwards
On 2011-02-16, Ian Kelly ian.g.ke...@gmail.com wrote:
 On Tue, Feb 15, 2011 at 4:49 PM, Hans-Peter Jansen h...@urpla.net wrote:

 while I usually cope with the woes of floating point issues, this is
 one, that I didn't expect:

 round(2.385, 2)
 2.3799

 Doesn't the docs say, it's rounded up for this case?
[...]

 Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] 
 on
 win32
 Type help, copyright, credits or license for more information.
 2.385
 2.3848

 Looks to me like it's working as expected...

Well, it's working as it's supposed to...

Whether it's working as expected depends on the user. :)

-- 
Grant



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


Re: floating point woes

2011-02-15 Thread Grant Edwards
On 2011-02-16, Hans-Peter Jansen h...@urpla.net wrote:

 Thanks for the explanation, Benjamin. Not that I like it, but anyway.
 If I hadn't quitted smoking a long time ago, I would go and ask, what
 these engineers smoked during the course of inventing this sh*t.

Like most tools, IEEE floating point works brilliantly for what it was
intended when used by people who know how to use it.  The problem is
that it's used for all sorts of things it shouldn't be by people who
don't understand how it works (again, like most tools).  The problem
is that it _appears_ easy to use, but it actually takes some study and
effort to use it right.

Back in the days before FP hardware was affordable, it used to be
fairly common for BCD to be the default FP representation in many
non-Fortran languages (BASIC, Pascal, etc.).  I think that probably
provided a lot fewer surprises to most users.

 Even more probably, they took way too much of a special form of
 lysergic acid.

 OTOH, cdecimals, as in Stefan Krah's package are long overdue to get 
 into the core.

There probably needs to be some sort of BCD FP option for the casual
user since most people would probably be better off with BCD.  Those
who need HW FP probably know it (and might even know how to use it).

[I may have just barely passed undergrad numerical analysis, but I
learned enough to know how ignorant I was.]

-- 
Grant



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


Re: floating point woes

2011-02-15 Thread Mel
Hans-Peter Jansen wrote:

 Hi,
 
 while I usually cope with the woes of floating point issues, this is
 one, that I didn't expect:
 
 round(2.385, 2)
 2.3799
 
 Doesn't the docs say, it's rounded up for this case?
 
 quote
 Values are rounded to the closest multiple of 10 to the power minus n;
 if two multiples are equally close, rounding is done away from 0
 /quote
 
 Well, that one is clearly rounding down.
 
 What's up, eh, down here?

2.385 isn't really 2.385:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type help, copyright, credits or license for more information.
 repr (2.385)
'2.3848'
 

so it correctly rounded down.  You need to use Decimal numbers if you want 
numbers that behave the way they look.

Mel.

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