Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: for / while else doesn't make sense Date: Sat, 21 May 2016 12:05:12 +1000 Lines: 79 Message-ID: References: <573EC62F.4090401@lucidity.plus.com> <573f9322$0$1616$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de BeNwKcc7lkflEUEU5xrFcgw4A/OmDFSKxXsMVtTX1Jig== 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; 'root': 0.04; 'float': 0.05; '21,': 0.07; 'badly': 0.07; 'properly.': 0.07; 'cc:addr :python-list': 0.09; '0.1': 0.09; 'confuse': 0.09; 'subject:while': 0.09; 'type:': 0.09; 'working:': 0.09; 'python': 0.10; 'explicitly': 0.15; 'value.': 0.15; '2016': 0.16; 'anyone?': 0.16; 'argument.': 0.16; 'bring.': 0.16; 'float"': 0.16; 'floats;': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'integers.': 0.16; 'nonzero': 0.16; 'py3': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:make': 0.16; 'support)': 0.16; 'unhappy': 0.16; 'version?': 0.16; 'zero,': 0.16; 'wrote:': 0.16; 'instance,': 0.18; 'integer': 0.18; 'programmer': 0.18; 'language': 0.19; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; "aren't": 0.22; 'fraction': 0.22; 'programming': 0.22; 'am,': 0.23; 'needed.': 0.23; "python's": 0.23; 'sat,': 0.23; 'skip:( 40': 0.23; 'tried': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'example': 0.26; 'sense': 0.26; 'least': 0.27; 'message-id:@mail.gmail.com': 0.27; 'have,': 0.27; 'yield': 0.27; 'skip:( 20': 0.28; '0.5': 0.29; 'arithmetic': 0.29; 'behaviour': 0.29; 'convenience': 0.29; 'decimal': 0.29; 'division': 0.29; 'forces': 0.29; 'sure,': 0.29; 'themselves': 0.29; "i'm": 0.30; 'operations': 0.31; 'certain': 0.31; "can't": 0.32; 'core': 0.32; 'says': 0.32; 'point': 0.33; "d'aprano": 0.33; 'operations.': 0.33; 'steven': 0.33; 'surprised': 0.33; '(for': 0.34; 'languages': 0.34; 'handle': 0.34; 'received:google.com': 0.35; 'trouble': 0.35; 'done': 0.35; 'something': 0.35; 'received:74.125.82': 0.35; "isn't": 0.35; 'problem.': 0.35; 'but': 0.36; 'should': 0.36; '(and': 0.36; 'basic': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'say': 0.37; 'someone': 0.38; 'represent': 0.38; 'data': 0.39; 'where': 0.40; 'still': 0.40; 'some': 0.40; 'your': 0.60; 'avoid': 0.61; 'provide': 0.61; 'real': 0.62; 'making': 0.62; 'subject: / ': 0.63; 'between': 0.65; "they're": 0.66; 'biggest': 0.67; 'float:': 0.72; 'suited': 0.72; 'square': 0.76; '"very': 0.84; 'chrisa': 0.84; 'inherent': 0.84; 'proves': 0.84; 'received:74.125.82.41': 0.84; 'retrograde': 0.84; 'standing': 0.84; 'subject:else': 0.84; 'subject:sense': 0.84; 'terrible': 0.84; 'toy': 0.84; 'to:none': 0.91; 'ratio': 0.91; 'why?': 0.91; 'have.': 0.93; 'plays': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=6Qk9AUty5liJM1UhZc5+7vEaF4xafDbey6CAayHrRuM=; b=eDQzy2fW5ss+ODv9MRjks9g0htkixyK4pjoLvnbHQ6ImOUBYlId0pOutT0rlIRBpsW 64akR7bYGJxPq6xWp8kcdI2s0v8wuL0KSgZOSboWCx3wsHuJAtqhXqXYlWOWQwmRa74c IiEzxWvavTp05p/BDqzjEaA4VkZFSIENiiQaIv5VSjyywc8W0DaM5g9xkN5aW3/EnC46 ixK02IDeXZz5ydvzoTv36yX8yoLiqJskzppvoastuHOABSdN/3GRNvkPyqKbBswHcHVV nEzcOsW+JsaT3UzbPqSGgMYlRWF4S0R5b01l6YXKUOmfCArRjfhtkmZUftyeacLGRBBw S+Vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=6Qk9AUty5liJM1UhZc5+7vEaF4xafDbey6CAayHrRuM=; b=m/z3OzYQgsqgGE3KkVFtEJmWCnkQ0f+m1nG18Y7rOszwX79bd67sw1KFo30aTHNo+H HtonoBQE/zNsqRWmOQKRLcNsHEeufcgAnWACDTrm988gMAE1Jb+gsYk4bkg44NN1kGe2 XIdeM2TD0Aek+NetY0SH/UIbGbggPsgXij47eacUIo0FIPKXMMxwpOr30ealFfIJmPl0 FxSrmiEkV/4F6hp707ytjXpfDEzyWWtBIgLYPUgb1yP9rr+rNEoAHG/9j9GsNckQpM2C zW0niDQUjnSqEJHw5HxS7C7SMO4KT+K+mQzP0Va0TpIbhyhmllfqwYf3K4rVR3D+nOG5 HTxw== X-Gm-Message-State: AOPr4FW6GnIqVlata0bnhhH0OLwzhTF1wiLqgRpnPoqZQ4rh8cRrCy01LyAGPfJPvYP/zdOrSpMwp1GfazNXyA== X-Received: by 10.194.186.237 with SMTP id fn13mr6276484wjc.47.1463796313021; Fri, 20 May 2016 19:05:13 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <573EC62F.4090401@lucidity.plus.com> <573f9322$0$1616$c3e8da3$5496439d@news.astraweb.com> Xref: csiph.com comp.lang.python:108883 On Sat, May 21, 2016 at 10:35 AM, Jon Ribbens wrote: > On 2016-05-20, Steven D'Aprano wrote: >> By that logic, we ought to: >> >> - avoid using floats because their behaviour isn't intuitive and >> can be confusing; > > To be fair, I'm very sympathetic to that argument. I think programming > languages should never magically produce floats out of nowhere unless > the programmer has explicitly done "import float" or "float('3.23')" > or somesuch. They're misunderstood so often that any convenience > they provide is outweighed by the danger they bring. > > "(1/10) * (1/10) * 10 != (1/10)" anyone? I was distinctly unhappy with > the Python 3 "2/3 ~= 0.6666" thing and regard it as a very retrograde > change. The trouble is, what SHOULD 2/3 return? * An integer? Makes a lot of sense to a C programmer. Not so much to someone who is expecting a nonzero value. This isn't terrible (hey, Python 2 managed with it no problem), but will definitely confuse a number of people. * A float? That's what we currently have. Not perfect, but it's going to confuse less people than 0 will. * A decimal.Decimal? That has its own issues (for instance, (x+y)/2 can return a value that isn't between x and y), and it still can't represent two thirds properly. * A fractions.Fraction? Well, at least that can perfectly represent a ratio of integers. But it plays REALLY badly with other non-integer types, and other operations than basic arithmetic (ever tried to take the square root of a fraction?), so it's really only suited to situations where you're working exclusively with fractions. * Something else? You say that Py3 making 1/10 => 0.1 was a "very retrograde change". Why? Yes, now you're seeing floats; but would you be less surprised by the Py2 version? Sure, Py2 has your little toy example working: >>> (1/10) * (1/10) * 10 == (1/10) True but that's because 1/10 is zero, not because it's been represented accurately! The biggest advantage of having integer division yield an integer is that it forces people to think about their data types; but since Python's float has a standing (core language support) that Decimal and Fraction don't have, I don't think it's a problem. It's the same with the complex type: >>> 2 ** 0.5 1.4142135623730951 >>> (-2) ** 0.5 (8.659560562354934e-17+1.4142135623730951j) Core data types will migrate between themselves as needed. (And this proves some of the inherent inaccuracies in floating point arithmetic; real number arithmetic says that the square root of -2 has a zero real part.) Interestingly, fractions.Fraction doesn't handle non-integer exponentiation, and punts to float: >>> fractions.Fraction(4) ** fractions.Fraction(2) Fraction(16, 1) >>> fractions.Fraction(4) ** fractions.Fraction(2, 3) 2.5198420997897464 >>> fractions.Fraction(4) ** fractions.Fraction(3, 2) 8.0 >>> fractions.Fraction(-4) ** fractions.Fraction(1, 2) (1.2246467991473532e-16+2j) Some data types just aren't well suited to certain operations. ChrisA