Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'literal': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:using': 0.09; 'val': 0.09; 'valueerror:': 0.09; 'python': 0.11; 'int(s)': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.18; 'thanks.': 0.20; 'import': 0.22; 'header:User-Agent:1': 0.23; 'bytes': 0.24; 'issue,': 0.24; 'skip:" 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'thus': 0.29; 'code': 0.31; '"",': 0.31; '>>>>': 0.31; 'ctypes': 0.31; 'file': 0.32; 'probably': 0.32; 'skip:c 30': 0.32; '(most': 0.33; 'actual': 0.34; 'but': 0.35; 'skip:4 10': 0.37; 'to:addr:python- list': 0.38; 'issue': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'more': 0.64; 'mar': 0.68; 'invalid': 0.68; '10:': 0.84; '2014,': 0.84; 'object:': 0.84; 'otten': 0.84; 'received:pacbell.net': 0.84; 'discovering': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: emile Subject: Re: Odd ValueError using float Date: Tue, 17 Mar 2015 13:48:35 -0700 References: <877fuk71fz.fsf@nightsong.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-69-226-129-65.dsl.pltn13.pacbell.net User-Agent: Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1426625323 news.xs4all.nl 2866 [2001:888:2000:d::a6]:50679 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87648 On 03/15/2015 07:01 AM, Peter Otten wrote: > Probably not helpful, but I can provoke the behaviour you see by toggling > bytes with ctypes, thus simulating a corrupted str object: > > Python 2.7.6 (default, Mar 22 2014, 22:59:56) > [GCC 4.8.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> import ctypes >>>> s = "41.700000000000003" >>>> ctypes.c_ubyte.from_address(id(s) + 16).value = 1 >>>> s > '4' >>>> len(s) > 1 >>>> float(s) > Traceback (most recent call last): > File "", line 1, in > ValueError: invalid literal for float(): 41.700000000000003 >>>> int(s) > Traceback (most recent call last): > File "", line 1, in > ValueError: invalid literal for int() with base 10: '41.700000000000003' I dug through the code and ctypes was in the mix. Without discovering the actual issue, I did manage to work around the issue by replacing: val = round(float(decval),1) with val = round(float("".join([ii if ii=="." else str(int(ii)) for ii in decval])),1) Thanks.