Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: str(float) python 3 versus 2.7 Date: Mon, 23 May 2016 11:05:00 -0600 Lines: 46 Message-ID: References: <7c1af35c-1ac7-96c7-3cba-ab9c8e542d8e@chamonix.reportlab.co.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de SfvABU8nPmUKwUn2eoIB+wAFq6zNC/1qeTCixuLU0rvw== 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; 'essentially': 0.04; 'float': 0.05; 'formatting': 0.07; 'nicely': 0.07; 'trailing': 0.07; 'type,': 0.07; "'n'": 0.09; 'rounding': 0.09; 'subject:2.7': 0.09; 'received:209.85.218': 0.10; 'python': 0.10; '2.7': 0.13; 'subject:python': 0.14; 'explicitly': 0.15; '0.6': 0.16; '2016': 0.16; '23,': 0.16; 'labels,': 0.16; 'naive': 0.16; 'received:209.85.218.51': 0.16; 'received:io': 0.16; 'received :mail-oi0-f51.google.com': 0.16; 'received:psf.io': 0.16; 'subject:versus': 0.16; 'wrote:': 0.16; 'string': 0.17; 'figures': 0.18; 'numerical': 0.18; '>>>': 0.20; 'all,': 0.20; 'prevent': 0.20; 'object.': 0.22; 'precise': 0.22; 'strip': 0.22; 'suppose': 0.22; 'am,': 0.23; 'leave': 0.23; 'errors': 0.23; 'header:In- Reply-To:1': 0.24; 'mon,': 0.24; 'url:bugs': 0.24; 'message- id:@mail.gmail.com': 0.27; 'format,': 0.27; 'function': 0.28; 'this.': 0.28; 'behaviour': 0.29; 'issues.': 0.29; 'sensible': 0.29; 'str': 0.29; 'starts': 0.29; 'compared': 0.30; 'rules': 0.31; 'up.': 0.32; 'computing': 0.32; 'subject:) ': 0.32; 'changed': 0.33; 'class': 0.33; 'url:python': 0.33; "skip:' 20": 0.34; 'received:google.com': 0.35; 'skip:c 30': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'url:library': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'difference': 0.38; 'received:209': 0.38; 'skip:- 20': 0.39; 'format': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'url:3': 0.60; 'suitable': 0.61; 'show': 0.62; 'covers': 0.72; '2.7.': 0.84; '3.4': 0.84; 'becker': 0.84; 'to:name:python': 0.84; 'url:string': 0.84 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=Pyb8F0MDBQy26nJlIiHV3uXDlUQopyrnlXSRBh+XZQ0=; b=hHXEShF90u+EK9AdFYvNqI9ogiZtA5wgPQqLMf9DPH/F8rbtnvsECCLHULuwvqB4Le 7LXbbDabfPrl3andjGNUO9gwG9siylZ9jIrVuhCCrfmmFti2/hV/LmelQrE6hVz90YtB V2GjqzPrZGfH2f+IVPTYcxxEc62zAY+2UdG/SWmFAw8otEAxmZSgMiE3s8+IE3c948j0 +Dd4wVL/vGcGm60RPf26IdNV3OunSvKUCAzWLWvNq5ZxwJjZIopuB0bksG/v6ht84LHB ue+avVR6kx7HG7umbQf6/gOP3v+epUb+lsgcJlrfdNo+ROHDKKx9mIFzbVLclRSLGHj2 wFSQ== 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=Pyb8F0MDBQy26nJlIiHV3uXDlUQopyrnlXSRBh+XZQ0=; b=Z4oAu+fmrktMrSa2pJnHZ7qGD3wREuvLfMAUV3bvje1m1WjL7vMJ7I2ZlgRQWxnasC vVV8p2Mex5UDoTfDWgj1ta5SYsLGxfRpto/ByBo3eJh9CZnEzmfstyiWE6pR0x1BWjH+ T3UvLX8DPebSApjRQ0dmGE+TG9WF8ZiF35VPap8NJ3VQr1qSwdzUGOwqHSPCTW6Q7TXL o1NHzK2P592YEkvw6YWFnLEuofNfd4DUMqFRTvSUI8s+TVqnsjpgsTYqRYPsXOL/Aovj WHV2Y7ELYVoblwLE4k1+oSSB6DnOkaS727wn0gYn8pSQixr/kLjIz4Tc67C9AkeS7+sR 9KHA== X-Gm-Message-State: ALyK8tLyn/2UjShtH/WuhvO+Zdz7UKeczYMQA8qCrS7Vfcb7d209d8IlaaftutQNFX2FYsL6GYAmNc5DS2NupA== X-Received: by 10.202.214.208 with SMTP id n199mr959879oig.144.1464023139611; Mon, 23 May 2016 10:05:39 -0700 (PDT) In-Reply-To: <7c1af35c-1ac7-96c7-3cba-ab9c8e542d8e@chamonix.reportlab.co.uk> 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: <7c1af35c-1ac7-96c7-3cba-ab9c8e542d8e@chamonix.reportlab.co.uk> Xref: csiph.com comp.lang.python:109011 On Mon, May 23, 2016 at 10:39 AM, Robin Becker wrote: > I had always imagined that the str founction did some kind of rounding on > floats to prevent small numerical errors from showing up. The 2.7 > documentation starts like this > >> class str(object='') >> Return a string containing a nicely printable representation of an object. >> For s > > > > > However, I see a difference in the behaviour of python3.3, 3.4 & 3.5 when > compared to python 2.7. > >> C:\Users\rptlab>\python33\python.exe -c"print(str(3*0.2))" >> 0.6000000000000001 >> >> C:\Users\rptlab>\python34\python.exe -c"print(str(3*0.2))" >> 0.6000000000000001 >> >> C:\Users\rptlab>\python35\python.exe -c"print(str(3*0.2))" >> 0.6000000000000001 >> >> C:\Users\rptlab>\python27\python.exe -c"print(str(3*0.2))" >> 0.6 > > > I suppose I am being naive and should use the round function when computing > tick labels, but that leads to other issues. > > Is there a sensible way to take a set of floats and find a suitable format > to show significant figures for all, but leave off the noise? This was changed in 3.2; see https://bugs.python.org/issue9337 If you want to show the float in a less noisy format, you can explicitly format it using the 'g' or 'n' presentation type, which essentially round to a given precision and strip off trailing zeros: >>> '{:g}'.format(3*0.2) '0.6' It may also be worthwhile to read the section at https://docs.python.org/3/library/string.html#format-specification-mini-language that covers the precise formatting rules used by this.