<div class="gmail_quote">On Thu, Mar 13, 2008 at 4:20 AM, Imri Goldberg &lt;<a href="mailto:lorgandon@gmail.com">lorgandon@gmail.com</a>&gt; wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
My suggestion is to do either of the following:<br>
1. Change floating point == to behave like a valid floating point<br>
comparison. That means using precision and some error measure.<br>
2. Change floating point == to raise an exception, with an error string<br>
suggesting using precision comparison, or the decimal module.<br></blockquote><div><br class="webkit-block-placeholder"></div><div>I don&#39;t much like either of these; &nbsp;I think option 1 would cause<br></div><div>a lot of confusion and difficulty---it changes a conceptually</div>
<div>simple operation into something more complicated.</div><div><br class="webkit-block-placeholder"></div><div>As for option 2., I&#39;d agree that there are situations where having</div><div>a warning (not an exception) for floating-point equality (and</div>
<div>inequality) tests might&nbsp;be helpful; &nbsp;but that warning should be</div><div>off by default, or at least&nbsp;easily turned off.</div><div><br class="webkit-block-placeholder"></div><div>Some Fortran compilers have such a (compile-time) warning,</div>
<div>I believe. &nbsp;But&nbsp;Fortran&#39;s users are much more likely to be</div><div>writing the sort of code that cares about this.</div><div>&nbsp;<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Since this change is not backwards compatible, I suggest it be added<br>
only to Python 3.<br>
</blockquote><div><br class="webkit-block-placeholder"></div><div>It&#39;s already too late for Python 3.0.</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><br>

3. Programmers will still need the regular ==:<br>
Maybe, and even then, only for very rare cases. For these, a special<br>
function\method might be used, which could be named floating_exact_eq.<br></blockquote><div><br class="webkit-block-placeholder"></div><div>I disagree with the &#39;very rare&#39; here. &nbsp;I&#39;ve seen, and written, code like:</div>
<div><br class="webkit-block-placeholder"></div><div>if a == 0.0:</div><div>&nbsp;&nbsp; &nbsp;# deal with exceptional case</div><div>else:</div><div>&nbsp;&nbsp; &nbsp;b = c/a</div><div>&nbsp;&nbsp; &nbsp;...</div><div><br class="webkit-block-placeholder"></div><div>
or similarly, a test (a==b) before doing a division by a-b. &nbsp;That</div><div>one&#39;s kind of dodgy, by the way: &nbsp;a != b doesn&#39;t always guarantee</div><div>that a-b is nonzero, though you&#39;re okay if you&#39;re on an IEEE 754</div>
<div>platform and a and b are both finite numbers.</div><div><br class="webkit-block-placeholder"></div><div>Or what if you wanted to generate random numbers in the open interval</div><div>(0.0, 1.0). &nbsp;random.random gives you numbers in [0.0, 1.0), so a</div>
<div>careful programmer might well write:</div><div><br class="webkit-block-placeholder"></div><div>while True:</div><div>&nbsp;&nbsp; &nbsp;x = random.random()</div><div>&nbsp;&nbsp; &nbsp;if x != 0.0:</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;break</div><div><br class="webkit-block-placeholder">
</div><div>(A less fussy programmer might just say that the chance</div><div>of getting 0.0 is about 1 in 2**53, so it&#39;s never going to happen...)</div><div><br class="webkit-block-placeholder"></div><div>Other thoughts:</div>
<div><br></div><div>&nbsp;- what should x == x do?</div><div>&nbsp;- what should<br></div><div><br class="webkit-block-placeholder"></div><div>1.0 in set([0.0, 1.0, 2.0])</div><div><br class="webkit-block-placeholder"></div><div>and&nbsp;</div>
<div><br class="webkit-block-placeholder"></div><div>3.0 in set([0.0, 1.0, 2.0])</div><div><br class="webkit-block-placeholder"></div><div>do?</div><div><br class="webkit-block-placeholder"></div><div>Mark</div></div>