From: Grant Olson on
Doh!

Well the problem is that int's are integers. So yeah, you can't even do
that with normal value "int ('2.1')" will also throw an error. And
floats don't support radix conversion, because no-one really writes
numbers that way. (At least computer programmers...)


On 3/30/2010 11:43 AM, Shashwat Anand wrote:
> The conversion is not supported for decimal integers AFAIK, however
> '0b123.456' is always valid. I guess you can always get a decimal number
> convertor onto Python-recipes
>
>
>
> On Tue, Mar 30, 2010 at 9:05 PM, Grant Olson <kgo(a)grant-olson.net
> <mailto:kgo(a)grant-olson.net>> wrote:
>
> 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.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>

From: MRAB on
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.
>
int() returns an integer (hence the name!), so it should never return a
float anyway.

What you want is for float() to accept a base, but that is rarely
needed.
From: aditya on
On Mar 30, 10:37 am, Benjamin Kaplan <benjamin.kap...(a)case.edu> wrote:
> On Tue, Mar 30, 2010 at 11: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
> > --
>
> 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/python-list
>
>

That makes sense. The closest thing I've found is this question on
StackOverflow: http://stackoverflow.com/questions/1592158/python-convert-hex-to-float

It seems to me that adding a conversion feature to floats would be a
lot more intuitive.
From: aditya on
On Mar 30, 10:49 am, Raymond Hettinger <pyt...(a)rcn.com> wrote:
> 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 non-integral values.
> Binary representation isn't supported yet,
> but we do have hex:
>
>     >>> float.fromhex('1.8')
>     1.5
>
> Raymond

That looks very elegant, thanks!
From: Steven D'Aprano on
On Tue, 30 Mar 2010 08:28:50 -0700, Patrick Maupin wrote:

> 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?


>>> int('1.1')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1.1'


int('1.1', 2) shouldn't return 1.5 because 1.5 isn't an integer.


The obvious question is, why doesn't float('1.1', 2) work? The answer is
that Python doesn't support floats in any base except 10. It's not
something needed very often, and it's harder to get right than it might
seem.



--
Steven