Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'explicitly': 0.05; 'subject:Python': 0.06; 'explicit': 0.07; 'float': 0.07; 'result,': 0.07; 'integers': 0.09; 'pep': 0.09; 'subject:Why': 0.09; 'type,': 0.09; 'python': 0.11; '2.2,': 0.16; '238': 0.16; '__future__': 0.16; 'accuracy,': 0.16; 'arbitrarily': 0.16; 'division.': 0.16; 'fine.': 0.16; 'floats;': 0.16; 'implemented,': 0.16; 'integer,': 0.16; 'integers,': 0.16; 'integers.': 0.16; 'intersection': 0.16; 'losing': 0.16; 'operands': 0.16; 'operation,': 0.16; 'pass;': 0.16; 'types,': 0.16; 'sat,': 0.16; 'appropriate': 0.16; 'wrote:': 0.18; "python's": 0.19; 'result.': 0.19; '>>>': 0.22; 'import': 0.22; 'affects': 0.24; 'alternate': 0.24; 'fraction': 0.24; 'integer': 0.24; 'logical': 0.24; 'mathematical': 0.24; 'math': 0.24; 'regardless': 0.24; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'chris': 0.29; 'am,': 0.29; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; '(which': 0.31; '(although': 0.31; 'decimal': 0.31; 'division': 0.31; 'int,': 0.31; 'says': 0.33; '(i.e.': 0.33; 'something': 0.35; 'case,': 0.35; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'acceptable': 0.36; 'false': 0.36; 'useful': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'should': 0.36; 'two': 0.37; 'expressed': 0.37; 'performance': 0.37; 'problems': 0.38; 'branch': 0.38; 'generic': 0.38; 'needed': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'does': 0.39; 'use.': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'even': 0.60; 'ian': 0.60; 'hope': 0.61; 'numbers': 0.61; 'range': 0.61; 'simple': 0.61; "you're": 0.61; "you'll": 0.62; 'real': 0.63; 'skip:n 10': 0.64; 'become': 0.64; 'more': 0.64; 'different': 0.65; 'within': 0.65; 'believe': 0.68; 'importantly,': 0.68; '7:25': 0.84; 'divide': 0.84; 'float,': 0.84; "it'd": 0.84; 'technically': 0.84; 'bless': 0.91; 'whereas': 0.91; 'won': 0.96 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=IVpqBxcSTonnJaSuCfQX0EhOvGq5cCH486zLKSGT248=; b=huDWKuWuuzkePWOqPKa1LM9G4Gsm+zNMVc2hlcxookSVGRZYQ9Sqb8Z2sbv622od7L KGNHSIWZN6jXUEXnChts3hxLQQwsuIaY+VpzRU7aQlEc1kYaammPY53MvrAsfqVbAD0J 41oFMFoYFXQtOzdPMptypZEP1RHxIvoz1O0l0woqU+kQGK5jfHIVyc6EpnIg49z9rtOK Kd9HuYply7rcObn93Ac0lgTObiFgA32FzrVFPVUVRCDauSvt7gB23xmnkmPndA/Ezlf5 k8QQ9DpYnmQDK5BIH6m9xBCVyd9jQcgF1rUsMFuPak+EiHzZ+uXYuRxLdPfSKzybm1L5 cxWw== X-Received: by 10.68.170.131 with SMTP id am3mr28843121pbc.97.1397937545719; Sat, 19 Apr 2014 12:59:05 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <7x8ur1esa5.fsf@ruckus.brouhaha.com> From: Ian Kelly Date: Sat, 19 Apr 2014 13:58:24 -0600 Subject: Re: Why Python 3? To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 69 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1397937555 news.xs4all.nl 2863 [2001:888:2000:d::a6]:56318 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70396 On Sat, Apr 19, 2014 at 3:37 AM, Chris Angelico wrote: > On Sat, Apr 19, 2014 at 7:25 PM, Ian Kelly wrote: >> The change from / denoting "classic >> division" to "true division" really only affects the case where both >> operands are integers, so far as I'm aware. If you want to divide two >> integers and get a decimal result, then convert one or both of them to >> decimals first; you would have needed to do the same with classic >> division. > > If float were a perfect superset of int, and the only logical superset > when you want non-integers, then it'd be fine. Decimal is also not a perfect superset of int (although I believe it is a superset of the intersection of int and float). Even if it were, I'm not sure it would be appropriate to bless Decimal in this way either, because they have no place in Python's number type hierarchy: >>> from decimal import Decimal >>> from numbers import Real >>> isinstance(Decimal('1.23'), Real) False > But if you're mixing > int and Decimal, you have to explicitly convert, whereas if you're > mixing int and float, you don't. Why is it required to be explicit > with Decimal but not float? Of all the possible alternate types, why > float? Only because... > >> As for "why float" specifically, the division __future__ import has >> been around since 2.2, longer than either decimals or fractions. > > ... it already existed. There's no particular reason to up-cast to > float, specifically, and it can cause problems with large integers - > either by losing accuracy, or by outright overflowing. The authors of PEP 238 expressed their hope that when a rational type (i.e. Fraction) was implemented, it would become the result type for true division on two integers. I don't know why that never came to pass; perhaps performance considerations won out. > In Python 3, you have to say "Oh but I want my integer division to > result in an integer": > > x * 10 // 5 == x * 2 Technically this says "I want the result of floor division", not "I want the result as an integer". If you apply the floor division operator to a non-int type, you'll get a non-int result. It just so happens that the result of floor division of two integers can be given as an integer, whereas the result of true division cannot. Considering that Fraction and Decimal did not exist yet, what type do you think the PEP 238 implementers should have chosen for the result of dividing two ints? If float is not acceptable, and int is not acceptable (which was the whole point of the PEP), then the only alternative I can see would have been to raise a TypeError and force the user to upcast explicitly. In that case, dividing arbitrary ints using floating-point math would not be possible for those ints that are outside the range of floats; you would get OverflowError on the upcast operation, regardless of whether the result of division would be within the range of a float. > Yes, I can see that it's nice for simple interactive use. More importantly, it's useful for implementers of generic mathematical routines. If you're passed arbitrary inputs, you don't have to check the types of the values you were given and then branch if both of the values you were about to divide happened to be ints just because the division operator arbitrarily does something different on ints.