From: aditya on 30 Mar 2010 11:13 To get the decimal representation of a binary number, I can just do this: int('11',2) # returns 3 But decimal binary numbers throw a ValueError: int('1.1',2) # should return 1.5, throws error instead. Is this by design? It seems to me that this is not the correct behavior.  Aditya
From: Patrick Maupin on 30 Mar 2010 11:28 On Mar 30, 10:13 am, aditya <bluemangrou...(a)gmail.com> wrote: > To get the decimal representation of a binary number, I can just do > this: > > int('11',2) # returns 3 > > But decimal binary numbers throw a ValueError: > > int('1.1',2) # should return 1.5, throws error instead. > > Is this by design? It seems to me that this is not the correct > behavior. > >  Aditya So, why should int('1.1', 2) throw an error when int('1.1') doesn't? Regards, Pat
From: Grant Olson on 30 Mar 2010 11:35 On 3/30/2010 11:13 AM, aditya wrote: > To get the decimal representation of a binary number, I can just do > this: > > int('11',2) # returns 3 > > But decimal binary numbers throw a ValueError: > > int('1.1',2) # should return 1.5, throws error instead. > > Is this by design? It seems to me that this is not the correct > behavior. > Well technically that would be a 'radix point', not a decimal point. But I think the problem is that computers don't store fractional values that way internally. They either use floating or fixed point math. You would never look at raw binary data on a computer and see something like '1010.1010', and no one would write it that way, and no language (that I know of) would accept that as a valid value if you did something like "x = 0b1010.1010" So in that sense, it might not be an intentional oversight, but it's not a very practical or useful feature.
From: Benjamin Kaplan on 30 Mar 2010 11:37 On Tue, Mar 30, 2010 at 11:13 AM, aditya <bluemangroupie(a)gmail.com> wrote: > To get the decimal representation of a binary number, I can just do > this: > > int('11',2) # returns 3 > > But decimal binary numbers throw a ValueError: > > int('1.1',2) # should return 1.5, throws error instead. > > Is this by design? It seems to me that this is not the correct > behavior. > >  Aditya >  Because int stands for integer and 1.1 is not an integer. You get the same error if you try doing int('1.1') > http://mail.python.org/mailman/listinfo/pythonlist >
From: Raymond Hettinger on 30 Mar 2010 11:49 On Mar 30, 8:13 am, aditya <bluemangrou...(a)gmail.com> wrote: > To get the decimal representation of a binary number, I can just do > this: > > int('11',2) # returns 3 > > But decimal binary numbers throw a ValueError: > > int('1.1',2) # should return 1.5, throws error instead. > > Is this by design? It seems to me that this is not the correct > behavior. The int() constructor returns integers. So, look to float() for nonintegral values. Binary representation isn't supported yet, but we do have hex: >>> float.fromhex('1.8') 1.5 Raymond

Next

Last
Pages: 1 2 3 Prev: It's a fact not lost on the opportunity to see yourself Next: (a==b) ? 'Yes' : 'No' 