Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Curious Omission In New-Style Formats Date: Wed, 13 Jul 2016 00:21:17 -0600 Lines: 88 Message-ID: References: <834b1cce-38dd-474c-8915-4ff1cd6b27ec@googlegroups.com> <7fcc8c21-106f-41d4-a5ba-409f3b54a56d@googlegroups.com> <5783c91e$0$1622$c3e8da3$5496439d@news.astraweb.com> <57846636$0$1621$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de CGJFhvnVAOWAYQjHXppF9AIGsjAZdF7XbnPTmpathC0A== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'float': 0.05; '(of': 0.07; 'claimed': 0.07; 'formatting': 0.07; 'pretend': 0.07; 'trailing': 0.07; 'byte,': 0.09; 'int.': 0.09; 'integers': 0.09; 'output,': 0.09; 'output?': 0.09; 'worse': 0.09; 'example:': 0.10; 'thread': 0.10; 'output': 0.13; 'ignore': 0.14; 'instead.': 0.15; "(it's": 0.16; '2016': 0.16; 'calculates': 0.16; 'example?': 0.16; 'fixed- point': 0.16; 'hex': 0.16; 'hexadecimal': 0.16; 'integer.': 0.16; 'integers,': 0.16; 'measured': 0.16; 'measurement': 0.16; 'measuring': 0.16; 'printf': 0.16; 'printf-style': 0.16; 'received:io': 0.16; 'received:mail-oi0-x230.google.com': 0.16; 'received:psf.io': 0.16; 'wanted.': 0.16; 'zeroes': 0.16; 'wrote:': 0.16; 'obviously': 0.16; "wouldn't": 0.16; 'string': 0.17; 'byte': 0.18; 'integer': 0.18; "shouldn't": 0.18; 'stick': 0.18; 'programmer': 0.18; '>>>': 0.20; 'all,': 0.20; 'math': 0.20; 'purposes': 0.20; '(the': 0.22; 'either.': 0.22; 'int,': 0.22; 'interpret': 0.22; 'lawrence': 0.22; 'referring': 0.22; 'suppose': 0.22; 'bigger': 0.23; 'this:': 0.23; 'second': 0.24; 'header:In- Reply-To:1': 0.24; 'all.': 0.24; 'mon,': 0.24; "i've": 0.25; "doesn't": 0.26; 'example': 0.26; '(which': 0.26; 'sense': 0.26; 'followed': 0.27; 'not.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'converting': 0.27; 'function': 0.28; 'values': 0.28; 'decimal': 0.29; 'does,': 0.29; 'measure': 0.29; 'so-called': 0.29; 'convert': 0.29; "i'm": 0.30; "we're": 0.30; "i'd": 0.31; 'anyone': 0.32; 'related': 0.32; 'anybody': 0.32; 'possibly': 0.32; 'problem': 0.33; 'third': 0.33; 'common': 0.33; "d'aprano": 0.33; 'lets': 0.33; 'point,': 0.33; 'right?': 0.33; 'steven': 0.33; "skip:' 20": 0.34; 'except': 0.34; 'this?': 0.34; 'received:google.com': 0.35; 'behind': 0.35; 'something': 0.35; "isn't": 0.35; 'but': 0.36; 'should': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'beyond': 0.37; 'wanted': 0.37; 'doing': 0.38; 'difference': 0.38; 'no,': 0.38; 'mean': 0.38; 'represent': 0.38; 'why': 0.39; 'format': 0.39; 'does': 0.39; "didn't": 0.39; 'along': 0.39; 'subject:-': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'some': 0.40; 'field': 0.60; 'your': 0.60; 'claim': 0.61; "you'll": 0.61; 'leading': 0.61; 'design,': 0.61; 'no.': 0.62; 'total': 0.62; 'matter': 0.63; 'distance': 0.63; 'more': 0.63; 'different': 0.63; 'places': 0.64; 'between': 0.65; 'sum': 0.69; 'jul': 0.72; 'useful.': 0.72; 'power': 0.72; 'contour': 0.84; 'distinguish': 0.84; 'divide': 0.84; 'me!': 0.84; 'nice,': 0.84; 'tide': 0.84; 'to:name:python': 0.84; 'careful': 0.91; 'hassle': 0.91 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; bh=8IVLHTy0BcjsKTIuy49WprvydQBvdFgOXs4b5q1CivY=; b=O5nZR/E1BeOOyBsFaQ/W8/Yyw3cWXkznMNQ1zj7DrqjOdrq6Dowo6mLiASRvAQcn5+ KVhS2UldPzbJWmu2zg7J6NhnW9UnH2xvL5HfUVGGlUdjS2RbjrawrEw5TA716LxWNurS 1qLA8o8I0Zb1IIjKDQzfvSf8a8ScKcQOKmMwBxd5N83aOfJ6gULP9onqcy24jT9tm3rZ 3Kzg/15H6rgbk7oyrjEfHDntK7l0D+BKot20LQNlsF/uXxSaL93o+abNwZOkuOnCT7JF CxMZAuYBrmNpRHVHihhnkH/gxbrYP/2cpTNsRQWegnAt56xo5QjN1fy3cNnRIB2oULiP 71/g== 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:from:date :message-id:subject:to; bh=8IVLHTy0BcjsKTIuy49WprvydQBvdFgOXs4b5q1CivY=; b=RC0PG4Km96WDG5dZSSGir1p8PsrWfOvluYqwCU7bbNTA5Qlvbglwk4tb5jm/X4WPAa P58oQWPzh08L0i05SWTryQprO1BqtM3ntCY4hwWckwLJMpbepvET2Q5nf2rR1beC90Fw 86toXx52dC2FkUuELrPYh9T6xZcHYo3n65+dD3qYuEaZD6WBVrP8APR2iBLXaSLMF+Yd yfG1H3FJKKpVV+sWY30QmJJjceHA5kCDLDTYUR8TD1M7y7zxwVzgj/RmgYpBHvNWX8yh 0S6MpUTOY4tBFGoDRgm/MMALJBENu3BDhUBfUiXpHKWmlZbqwvZoBufIgThIvAbBhQXB ggPw== X-Gm-Message-State: ALyK8tJTZHgAvJkesTjbvIpwynGJbNVwHWTJI/p2gGHlqABtFad/uGcRjOpGSoMXoNU/qN42kLqAQwXt8/4T9Q== X-Received: by 10.157.5.99 with SMTP id 90mr4049939otw.190.1468390917007; Tue, 12 Jul 2016 23:21:57 -0700 (PDT) In-Reply-To: <57846636$0$1621$c3e8da3$5496439d@news.astraweb.com> 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: <834b1cce-38dd-474c-8915-4ff1cd6b27ec@googlegroups.com> <7fcc8c21-106f-41d4-a5ba-409f3b54a56d@googlegroups.com> <5783c91e$0$1622$c3e8da3$5496439d@news.astraweb.com> <57846636$0$1621$c3e8da3$5496439d@news.astraweb.com> Xref: csiph.com comp.lang.python:111376 On Mon, Jul 11, 2016 at 9:38 PM, Steven D'Aprano wrote: > For integers, printf and % interpret the so-called "precision" field of the > format string not as a measurement precision (number of decimal places), > but as the number of digits to use (which is different from the total field > width). For example: > > > py> "%10.8x" % 123 > ' 0000007b' > > How anyone can possibly claim this makes no sense is beyond me! Is the > difference between "number of digits" and "number of decimal places" really > so hard to grok? I think not. I never claimed it's not useful. I don't really have a problem with format supporting it, either. But if it does, then don't call it "precision". That's like writing a function that calculates a mean and calling it "mean", except that if passed a bunch of complex numbers, it just returns the sum instead. I don't know of anybody who would consider that good design, and the "precision" field in printf-style formatting isn't good design either. But it has history behind it, so does that put it in the right? > And it's clearly useful: with integers, particular if they represent > fixed-size byte quantities, it is very common to use leading zeroes. To a > programmer the hex values 7b, 007b and 0000007b have very different > meanings: the first is a byte, the second may be a short int, and the third > may be a long int. And what about 0007b? After all, the very example that started this thread wanted 5 hex digits, not a nice, even power of 2. > Why shouldn't we use the "precision" field for this? For the same reason that we shouldn't use the "mean" function to calculate sums. >>>> If you truly wanted to format the number with a precision >>>> of 5 digits, it would look like this: >>>> >>>> 0x123.00 >>> >>> Er, no, because its an integer. >> >> Which is why if you actually want to do this, you should convert it to >> a decimal or a float first (of course, those don't support hexadecimal >> output, so if you actually want hexadecimal output *and* digits after >> the (hexa)decimal point, then I think you would just have to roll your >> own formatting at that point). > > What? No no no. Didn't you even look at Lawrence's example? He doesn't want > to format the number with decimal places at all. I was referring to the example above. I'm completely aware that it's not the same as what Lawrence wanted. > Converting an integer to a float just to use the precision field is just > wrong. What if I've been doing my math with fixed-point integers (because I don't know about or just don't like decimals), and now I want to format them for output? Is this just wrong? '{:.2f}'.format(int_value / 100) > Now lets go the other way. How to you distinguish between a distance > measured using an unmarked metre stick, giving us an answer of 123 metres, > versus something measured with a 10km ruler(!) with one metre markings? > Obviously with *leading* zeroes rather than trailing zeroes. Fair enough, but I still wouldn't call that "precision". > It *does* matter for measuring curves, but paradoxically the bigger the > measuring stick (the more leading zeroes) the worse your measurement is > likely to be. This is the problem of measuring coastlines and is related to > fractal dimension. Suppose I lay my 10km long measuring stick along some > piece of coastline, and measure it as 00123 metres. (It's a *thought > experiment*, don't hassle me about the unfeasibly large stick. Divide > everything by a thousand and call it a 10m stick marked in millimetres if > you like.) Chances are that if I used a 1 metre measuring stick, and > followed the contour of the coast more closely, I'd get a different number. > So the more leading zeroes, the less accurate your measurement is likely to > be. But interesting as this is, for most purposes either we're not > measuring a curve, or we are but pretend we're not and ignore the fractal > dimension. If you use a 1 meter stick to measure the coastline, you'll go mad as the tide keeps ruining your careful measurements. Best to just use the 10 km stick and get it over with.