Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

[issue2748] ceil(), floor() and round() broken in Decimal

 

 

Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

May 3, 2008, 12:10 PM

Post #1 of 11 (1186 views)
Permalink
[issue2748] ceil(), floor() and round() broken in Decimal

New submission from Mark Dickinson <dickinsm [at] gmail>:

In Python 3.0, the Decimal __round__, __ceil__ and __floor__ functions
don't work as intended: they all return 1, 0, or -1.

This is easy to fix. The only reason I'm making an issue (literally) of
it is that I remember some discussion of whether these functions should
be implemented at all for Decimal, but I don't remember what the outcome
of that discussion was. Adding Jeffrey to the nosy list in case he
remembers.

Either all three functions should be removed, or they should be
corrected and tests should be added for them.

----------
assignee: facundobatista
components: Library (Lib)
messages: 66164
nosy: facundobatista, jyasskin, marketdickinson
severity: normal
status: open
title: ceil(), floor() and round() broken in Decimal
versions: Python 3.0

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue2748>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 3, 2008, 12:29 PM

Post #2 of 11 (1146 views)
Permalink
[issue2748] ceil(), floor() and round() broken in Decimal [In reply to]

Jeffrey Yasskin <jyasskin [at] gmail> added the comment:

I remember the answer being that they shouldn't be supported, but then I
stopped paying attention and some patches went in bringing Decimal
closer to the Real API again, so I'm not sure if the earlier discussion
still applies. I'm happy to let the decimal maintainers decide.

----------
nosy: +rhettinger

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue2748>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 3, 2008, 1:02 PM

Post #3 of 11 (1148 views)
Permalink
[issue2748] ceil(), floor() and round() broken in Decimal [In reply to]

Changes by Raymond Hettinger <rhettinger [at] users>:


----------
assignee: facundobatista -> rhettinger

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue2748>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 3, 2008, 1:04 PM

Post #4 of 11 (1148 views)
Permalink
[issue2748] ceil(), floor() and round() broken in Decimal [In reply to]

Mark Dickinson <dickinsm [at] gmail> added the comment:

I've removed __round__, __ceil__ and __floor__ in r62669; the code was
undocumented, untested and just plain wrong, so there doesn't seem to be
any point in leaving it in. (I'm not quite sure how it got there in the
first place.)

This still leaves the question of whether to implement these functions.
I can provide a patch if it's desirable.

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue2748>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 3, 2008, 1:13 PM

Post #5 of 11 (1149 views)
Permalink
[issue2748] ceil(), floor() and round() broken in Decimal [In reply to]

Raymond Hettinger <rhettinger [at] users> added the comment:

Thanks Mark. I'll review your patch when it's ready.

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue2748>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 3, 2008, 6:35 PM

Post #6 of 11 (1129 views)
Permalink
[issue2748] ceil(), floor() and round() broken in Decimal [In reply to]

Mark Dickinson <dickinsm [at] gmail> added the comment:

Here's a patch that implements __ceil__, __floor__ and __round__. (It
seems that just removing __ceil__, __floor__ and __round__ is not an
option, as I just discovered when r62669 turned all the buildbots red.)

Points to note:

(1) Two-argument round has essentially the same semantics as quantize.
To be precise, for a Decimal instance x and an int n,

round(x, n)

is exactly interchangeable with

x.quantize(Decimal('1E%s' % -n))

In particular, this means that round uses the rounding mode from the
current context (which will usually, but not always, be
ROUND_HALF_EVEN), and that an InvalidOperation exception will be raised
(or NaN returned) if the rounded value has too many digits for the
current context precision.

After thinking about it, it seemed better to make the two expressions
above identical than to have subtle and potentially confusing
differences between them.

(2) Decimal.__round__ takes two optional arguments, 'context' and
'rounding', again with exactly the same semantics as the corresponding
optional arguments for quantize. At the moment, these arguments aren't
considered public, and aren't documented. (And they're only accessible
through __round__ anyway, not directly through the round() builtin.)

(3) For one-argument round, ceil, and floor, the only real decision to
be made is what to do with NaNs and infinities. The spirit of IEEE
754/854/754r suggests that an attempt to turn an infinity into an
integer should signal the 'overflow' floating-point exception, while
turning a NaN into an integer should signal 'invalid'; correspondingly,
the patch raises OverflowError or ValueError respectively in these
situations.

----------
keywords: +patch
Added file: http://bugs.python.org/file10186/decimal_ceilfloor.patch

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue2748>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 4, 2008, 4:52 AM

Post #7 of 11 (1132 views)
Permalink
[issue2748] ceil(), floor() and round() broken in Decimal [In reply to]

Raymond Hettinger <rhettinger [at] users> added the comment:

The patch basically looks good. The signature for __round__() should
not grow beyond the spec for the built-in round() function that calls
it: round(x[, n]). The context and rounding arguments are not part of
that API. Just like __int__(), the __round__() method should stick to
its basic purpose.

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue2748>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 4, 2008, 5:45 AM

Post #8 of 11 (1133 views)
Permalink
[issue2748] ceil(), floor() and round() broken in Decimal [In reply to]

Mark Dickinson <dickinsm [at] gmail> added the comment:

Here's a revised patch, with the following changes

- no keyword arguments to round
- check that the second argument to round is an integer.

Added file: http://bugs.python.org/file10188/decimal_ceilfloor2.patch

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue2748>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 4, 2008, 1:19 PM

Post #9 of 11 (1127 views)
Permalink
[issue2748] ceil(), floor() and round() broken in Decimal [In reply to]

Raymond Hettinger <rhettinger [at] users> added the comment:

Thanks for the patch. Please apply.

----------
resolution: -> accepted

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue2748>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 8, 2008, 10:24 AM

Post #10 of 11 (1101 views)
Permalink
[issue2748] ceil(), floor() and round() broken in Decimal [In reply to]

Changes by Raymond Hettinger <rhettinger [at] users>:


----------
assignee: rhettinger -> marketdickinson

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue2748>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 9, 2008, 6:43 AM

Post #11 of 11 (1097 views)
Permalink
[issue2748] ceil(), floor() and round() broken in Decimal [In reply to]

Mark Dickinson <dickinsm [at] gmail> added the comment:

Thanks for reviewing this, Raymond!

Committed, r62938.

----------
status: open -> closed

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue2748>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.