Mark Dickinson wrote:It's actually quite common in finance and time calculations to
> On Wed, Sep 26, 2012 at 7:51 PM, Mike Graham <mikeg...@gmail.com> wrote:
>> The builtin round function is completely useless. I've never seen
>> anyone use it constructively
>
> I disagree that it's *completely* useless: the one-argument form
> (take a float, return the closest integer) is a fundamental and useful
> mathematics operation, just like floor and ceiling. It would be crazy
> to get rid of that. I could live with it moving from builtins into
> the math module, though.
>
> Agreed that the two-argument form causes a lot of confusion, though.
round to the nearest say basis point, cent or say micro second
in calculations (rather than just string formatting). round()
is perfect for that and easy to use.
Normally deprecation means you keep it forever but don't mention it much in the docs...
Pretty much *everything* about binary floats is surprising to people
who expect decimal semantics. Unless we're going to make Decimal the
default floating point type, and shift binary floats to a module, I
don't see any way around that, and a particularly don't see any reason
to single round() out as more confusing than any of the other float
gotchas.
Speaking from my experience on the tutor@ and pytho...@python.org
mailing lists, I believe that problems with round are vanishingly rare,
probably an order of magnitude fewer than "why doesn't Python add my
two floats correctly?" type questions.
> Mark Dickinson wrote:
> >>>> round(2.675, 2)
> > 2.67
> >
> > So you end up explaining again and again that computing binary
> > approximations to decimal rounds of binary approximations of decimal
> > halfway cases is a bad idea.
>
> But that's the fault of round(), is it ? ;-) It's more one of
> educating people of what to expect when working with floats.
It's the fault of the two-parameter ‘round’, yes. It is an attractive
nuisance that appears to promise one thing but doesn't deliver, and
makes it easy to do the wrong thing by mistake.
Where feasible, and where it doesn't unreasonably restrict
functionality, IMO Python should make it difficult to do the wrong thing
by mistake.
> Your example is a typical case that comes up when people enter
> examples and wonder why they don't see the expected results.
I think this is an argument that deprecating, and eventually removing,
the two-parameter form of ‘round’, would be helpful overall.
--
\ “Consider the daffodil. And while you're doing that, I'll be |
`\ over here, looking through your stuff.” —Jack Handey |
_o__) |
Ben Finney
Why suggest adding new round-like functions to the math module rather than defining a new round method on all numerical objects?
This seems like a problem for the proposal, though: we can't have it in the math library if it's a method!
math.ceil(x), math.floor(x), and math.trunc(x) and round(x) alreadyOn Sun, Sep 30, 2012 at 2:51 PM, Joshua Landau
<joshua.l...@gmail.com> wrote:
> On 30 September 2012 22:48, Joshua Landau <joshua.l...@gmail.com>
> wrote:
>>
>> This seems like a problem for the proposal, though: we can't have it in
>> the math library if it's a method!
>
>
> Now I think about it: yeah, it can be. We just coerce to float/decimal
> first. *sigh*
call the special methods x.__ceil__, x.__floor__, x.__round__, and
x.__trunc__. So those four functions already work with decimal
instances (and other numeric types that support those methods.)
>>> math.ceil("")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a float is required