Python 3 and PEP238 division

Discussion in 'Python' started by Ninereeds, Mar 17, 2008.

  1. Ninereeds

    Ninereeds Guest

    Is the PEP238 change to division going into Python 3 as planned?

    I realise that the new integer division semantics have been available
    in "from __future__" for quite a few years now, but a warning might be
    appropriate now that Python 3 is in alpha. A lot of people have
    probably either forgotten, or else never knew about PEP238. The
    following wording is still included in the Python 2.5.1
    documentation...

    """
    3.1.1 Numbers
    The interpreter acts as a simple calculator: you can type an
    expression at it and it will write the value. Expression syntax is
    straightforward: the operators +, -, * and / work just like in most
    other languages (for example, Pascal or C); parentheses can be used
    for grouping. For example:

    .... 2+2
    4.... 7/3
    2-3
    """

    Which is interesting, since neither Pascal nor C division works like
    that. Pascal has a separate 'div' operator for integer division. C and
    C++ compilers usually round toward zero IIRC, but the rounding
    direction is not defined in the standards. In any case, after the
    final adoption of PEP238, integer division in Python will generally
    return float results - not the rounded-to-floor integer results shown
    here.

    It might be worth brainstorming some contexts where problems are
    likely to occur, to help anyone trying to prepare for the change.

    My contribution to that would be any code that needs to partition
    lists into slices. The obvious cases - binary searching, sorting - are
    covered by libraries which should be used in preference to hand-
    written code, but this kind of thing can happen elsewhere. I have some
    code that organises a sorted list of data into a balanced tree as part
    of a code generation task, for example, which relies on floor
    division.

    Also, if money amounts are stored as integer numbers of pennies (which
    they often are, since floats represent approximate values) problems
    could occur with various calculations since multiplication by a
    fractional quantity is often represented as a multiplication followed
    by a division. For example adding 5% is equivalent to multiplying by
    1.05, or to multiplying by 105 then dividing by 100. The latter idiom
    is often used to keep everything integer, which requires division
    results to be rounded.

    Use of the decimal module is probably a good idea for money amounts
    these days, of course. I've not used it myself but the whole point of
    a decimal number type would be to get exact results and the kind of
    rounding behaviour that accountants would expect.

    The real world fix would normally be to replace the / operator
    with //, though, in order to keep the old floor-division semantics.
     
    Ninereeds, Mar 17, 2008
    #1
    1. Advertisements

  2. Ninereeds

    Terry Reedy Guest

    | Is the PEP238 change to division going into Python 3 as planned?

    IDLE 3.0a30.5

    | I realise that the new integer division semantics have been available
    | in "from __future__" for quite a few years now, but a warning might be
    | appropriate now that Python 3 is in alpha.

    2.6, I have read, have optional 'Py3' warnings, and a 2to3 conversion
    program
     
    Terry Reedy, Mar 17, 2008
    #2
    1. Advertisements

  3. Ninereeds

    Ninereeds Guest

    The tools can work out the *intent* of any particular division
    operator? Can work out whether the result should be integer or float,
    independent of any particular set of arguments? Seems unlikely.
     
    Ninereeds, Mar 18, 2008
    #3
  4. The interpreter can at least print out warnings when a "normal" division
    operator is called with two `int`\s at runtime.

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Mar 18, 2008
    #4
  5. Ninereeds

    Terry Reedy Guest

    | >
    | >
    | > | Is the PEP238 change to division going into Python 3 as planned?
    | >
    | > IDLE 3.0a3>>> 1/2
    | >
    | > 0.5
    | >
    | > | I realise that the new integer division semantics have been available
    | > | in "from __future__" for quite a few years now, but a warning might
    be
    | > | appropriate now that Python 3 is in alpha.
    | >
    | > 2.6, I have read, have optional 'Py3' warnings, and a 2to3 conversion
    | > program
    |
    | The tools can work out the *intent* of any particular division
    | operator? Can work out whether the result should be integer or float,
    | independent of any particular set of arguments? Seems unlikely.

    where the conversion is not deterministic, the tool will either ask or
    point out the possible change or not. I have not used it, and it is still
    being developed -- and tested in use by developers.
     
    Terry Reedy, Mar 19, 2008
    #5
  6. Ninereeds

    Terry Reedy Guest

    | >
    | >
    | > | Is the PEP238 change to division going into Python 3 as planned?
    | >
    | > IDLE 3.0a3>>> 1/2
    | >
    | > 0.5
    | >
    | > | I realise that the new integer division semantics have been available
    | > | in "from __future__" for quite a few years now, but a warning might
    be
    | > | appropriate now that Python 3 is in alpha.
    | >
    | > 2.6, I have read, have optional 'Py3' warnings, and a 2to3 conversion
    | > program
    |
    | The tools can work out the *intent* of any particular division
    | operator? Can work out whether the result should be integer or float,
    | independent of any particular set of arguments? Seems unlikely.

    For the case of int division, one should have started using 1//2 already
    for floor division. Before running 2to3, a program should pass all tests
    with 'from __future__ import division', so that a/b definitely means
    fractions division even for int/int. Then the only thing 2to3 has to do in
    this regard is delete the future import.
     
    Terry Reedy, Mar 19, 2008
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.