Why Python 3?

Chris Angelico rosuav at gmail.com
Sun Apr 20 19:24:09 EDT 2014


On Mon, Apr 21, 2014 at 8:52 AM, Gregory Ewing
<greg.ewing at canterbury.ac.nz> wrote:
> Chris Angelico wrote:
>
>> Truncating vs true is not the same as int vs float. If you mean to
>> explicitly request float division, you call float() on one or both
>> arguments. You're being explicit about something different.
>
>
> If you know you're dealing with either ints or floats,
> which is true in the vast majority of cases, then you
> know that / will always perform float division.

And that's what I mean about the common non-trivial case. It's easy
enough to come up with contrived or trivial cases that use any types,
but in most cases, it'd be fine to explicitly call float() on one of
the operands to explicitly request floating-point division. Choosing
between two division operators is not the same thing as choosing a
data type.

Explicitly choosing float division:

x / float(y)

Explicitly choosing to truncate:

math.trunc(x / y)

Both explicit forms can be done cleanly without empowering the
language with the magic of int/int->float.

ChrisA



More information about the Python-list mailing list