Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #87648

Re: Odd ValueError using float

From emile <emile@fenx.com>
Subject Re: Odd ValueError using float
Date 2015-03-17 13:48 -0700
References (4 earlier) <me1mdb$nri$1@ger.gmane.org> <me1ptc$gna$1@ger.gmane.org> <me1ucs$n98$1@ger.gmane.org> <me2439$cvf$1@ger.gmane.org> <me43c8$i7r$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.501.1426625323.21433.python-list@python.org> (permalink)

Show all headers | View raw


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 "<stdin>", line 1, in <module>
> ValueError: invalid literal for float(): 41.700000000000003
>>>> int(s)
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> 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.



Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Odd ValueError using float emile <emile@fenx.com> - 2015-03-13 15:10 -0700
  Re: Odd ValueError using float Paul Rubin <no.email@nospam.invalid> - 2015-03-13 19:33 -0700
    Re: Odd ValueError using float Chris Angelico <rosuav@gmail.com> - 2015-03-14 14:09 +1100
    Re: Odd ValueError using float emile <emile@fenx.com> - 2015-03-14 08:28 -0700
    Re: Odd ValueError using float Chris Angelico <rosuav@gmail.com> - 2015-03-15 02:52 +1100
    Re: Odd ValueError using float Peter Otten <__peter__@web.de> - 2015-03-14 17:08 +0100
    Re: Odd ValueError using float emile <emile@fenx.com> - 2015-03-14 10:08 -0700
    Re: Odd ValueError using float emile <emile@fenx.com> - 2015-03-14 10:17 -0700
    Re: Odd ValueError using float Peter Otten <__peter__@web.de> - 2015-03-14 19:24 +0100
    Re: Odd ValueError using float emile <emile@fenx.com> - 2015-03-14 13:01 -0700
    Re: Odd ValueError using float Peter Otten <__peter__@web.de> - 2015-03-15 15:01 +0100
    Re: Odd ValueError using float emile <emile@fenx.com> - 2015-03-17 13:48 -0700

csiph-web