<br clear="all">Chris<br>
<br><br><div class="gmail_quote">On Tue, Mar 30, 2010 at 11:14 AM, John Nagle <span dir="ltr"><<a href="mailto:nagle@animats.com">nagle@animats.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">aditya wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Mar 30, 10:49 am, Raymond Hettinger <<a href="mailto:pyt...@rcn.com" target="_blank">pyt...@rcn.com</a>> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Mar 30, 8:13 am, aditya <<a href="mailto:bluemangrou...@gmail.com" target="_blank">bluemangrou...@gmail.com</a>> wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
To get the decimal representation of a binary number, I can just do<br>
this:<br>
int('11',2) # returns 3<br>
But decimal binary numbers throw a ValueError:<br>
int('1.1',2) # should return 1.5, throws error instead.<br>
Is this by design? It seems to me that this is not the correct<br>
behavior.<br>
</blockquote>
The int() constructor returns integers.<br>
So, look to float() for non-integral values.<br>
Binary representation isn't supported yet,<br>
but we do have hex:<br>
<br>
>>> float.fromhex('1.8')<br>
1.5<br>
<br>
Raymond<br>
</blockquote>
<br>
That looks very elegant, thanks!<br>
</blockquote>
<br></div></div>
Hex floats are useful because you can get a string representation<br>
of the exact value of a binary floating point number. It should<br>
always be the case that<br>
<br>
float.fromhex(float.hex(x)) == x<br>
<br>
That's not always true of decimal representations, due to rounding problems.<br></blockquote><div><br></div><div>Actually, any number you can represent exactly in base 2 (or any power there of) you can also represent exactly in decimal - its the other way around thats a problem.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
Long discussion of this here: "<a href="http://bugs.python.org/issue1580" target="_blank">http://bugs.python.org/issue1580</a>"<br><font color="#888888">
<br>
<br>
John Nagle</font><div><div></div><div class="h5"><br>
<br>
<br>
<br>
-- <br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br>