I don&#39;t know which university you went to, but I certainly never observed any serious attempt to enforce such a rule at university. That said, it&#39;s a good one for all of us to learn if we don&#39;t apply it already.<br>
<br><div class="gmail_quote">On Feb 17, 2008 8:56 PM,  &lt;<a href="mailto:johnps11@bigpond.com">johnps11@bigpond.com</a>&gt; wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Cedric,<br><br>Part of the issue is that different architectures will have different<br>values for machine epsilon (1e-09 in your post). &nbsp;Machine epsilon is<br>defined to be the largest float such that<br><br>0.0 + epsilon = 0.0<br>
<br>Its value varies according to your machine though - typically a machine<br>with a much larger machine word size in its floating point processor will<br>have a much smaller machine epsilon. (The following is an example of a<br>
Simple Lie, for an introduction to the Complicated Truth I suggest you get<br>hold of a book on Numerical Computation, which is an important study in<br>Computer Science).<br><br>Things are complicated further in that in any radix based number system<br>
there are very simple numbers that cannot be written exactly. &nbsp;In the<br>decimal number system any fraction with a denominator relatively prime to<br>the radix tends to be an infinitely repeating fraction. 1/3 or 3/7 are<br>
examples.<br><br>Computers use binary numbers inside. 1/10 is not expressible as a<br>nonrecurring binary fraction, nor is 1/3, 3/10, 2/5 or 1/20.<br><br>Squeak does a good job of trying to promote numbers to ScaledDecimals, but<br>
they are much more expensive to calculate than IEEE floats or Integers.<br><br>If we repeat your examples using Fractions we get<br><br>1 - (2/10) - (5/100) - ( 3/10) - (1/10) -(1/10) = (1/4) true<br><br>because Fractions are exact - in fact the definition of equality for<br>
fractions is defined to be<br><br>( a/b ) = ( c/d ) if and only if ( a * d ) = (b * c)<br><br>which only uses Integer and BigNum arithmetic and allows arbitrary<br>precision (unless the numbers are so big you run out of memory).<br>
<br>Your idea of redefining equality for floats is a very bad idea because (1)<br>it assumes all hardware has the same value for machine epsilon and (2)<br>will break 50 years worth of programs that do numerical computation.<br>
<br>The simplest rule is any attempt to test floats (or doubles, or IEEE<br>decwords) for equality is a bug. &nbsp;If there&#39;s a decimal point in your<br>calculations (or if there could be) then don&#39;t test for equality. &nbsp;This is<br>
taught to computing students at University in the first few weeks. &nbsp;Any<br>work done by a Uni student who breaks this rule gets marked down very<br>severely!<br><br>I commend you on discovering this on your own by the way - it shows an<br>
inquiring mind. &nbsp;It&#39;s not everyone who hits one of the key issues in<br>Computer Science by themselves, and then thinks about it.<br><br>Yours,<br><br>John.<br><div><div></div><div class="Wj3C7c"><br>&gt;&gt; &gt; Hi,<br>
&gt;&gt; &gt;<br>&gt;&gt; &gt; I noticed in my image (damien last beta so 3.10 - windows XP and<br>&gt;&gt; &gt; Vista) that I have a strange bug when soustracting floats<br>&gt;&gt; &gt; successively. Here is how I reproduce it in a workspace:<br>
&gt;&gt; &gt;<br>&gt;&gt; &gt; 1 - 0.2 -0.05 -0.3 = 0.45 &quot;true&quot;<br>&gt;&gt; &gt; 1 - 0.2 -0.05 -0.3 -0.1= 0.35 &nbsp;&quot;true&quot;<br>&gt;&gt; &gt; 1 - 0.2 -0.05 -0.3 -0.1 - 0.10= 0.25 &nbsp;&quot;*****false*****&quot;<br>
&gt;&gt; &gt; 1 - 0.2 -0.05 -0.3 -0.1 - 0.10= 0.24999999999999995 &nbsp; true<br>&gt;&gt; &gt;<br>&gt;&gt; &gt; or again:<br>&gt;&gt; &gt; 1 - 0.1 = 0.9 &nbsp;true<br>&gt;&gt; &gt; 1 - 0.1 - 0.1 = 0.8 true<br>&gt;&gt; &gt; 1 - 0.1 - 0.1 - 0.1 = 0.7 false<br>
&gt;&gt; &gt;<br>&gt;&gt; &gt;<br>&gt;&gt; &gt; Can somebody explain that or is it a bug ? I don&#39;t know yet if it&#39;s<br>&gt;&gt; &gt; specific to my image, so does other have the same behavior?<br>&gt;&gt; &gt; I can force the rounding of the result but find it strange.<br>
&gt;&gt; &gt;<br>&gt;&gt; &gt; Cédrick<br>&gt;<br>&gt; 2008/2/17, danil osipchuk &lt;<a href="mailto:danil.osipchuk@gmail.com">danil.osipchuk@gmail.com</a>&gt;:<br>&gt;&gt; This is not even squeak specific. Decimal floats not always can be<br>
&gt;&gt; represented precisely in binary form:<br>&gt;&gt;<br>&gt;&gt; <a href="http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems" target="_blank">http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems</a><br>
&gt;&gt;<br>&gt;<br>&gt;<br>&gt;&gt; Hi Cédrick,<br>&gt;&gt;<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; if you need to operate with exact precision just don&#39;t trust<br>&gt;&gt; floats. Use ScaledDecimals instead. In Squeak ScaledDecimals are<br>
&gt;&gt; implemented with the fraction so it&#39;s matematically stronger<br>&gt;&gt; than floats. Use floats at presentation or user interface time<br>&gt;&gt; and evade them at domain model.<br>&gt;&gt;<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; cheers,<br>
&gt;&gt;<br>&gt;&gt; Sebastian Sastre<br>&gt;<br>&gt; uhm ok, good to know.<br>&gt; Actually, it was just a test that failed so I was surprised.<br>&gt;<br>&gt; I still find the result odd.<br>&gt;<br>&gt; Seeing ScaledDecimal&gt;&gt;testConvertFromFloat, I&#39;m wandering if it would<br>
&gt; be better to have Float&gt;&gt;= &nbsp; considering egality if the difference is<br>&gt; less than 1.0e-9.<br>&gt;<br>&gt; &lt;primitive: 47&gt;<br>&gt; aNumber isNumber ifFalse: [^ false].<br>&gt; ^ (aNumber asFloat - self) abs &nbsp;&lt; &nbsp;1.0e-9<br>
&gt;<br>&gt; I would prefer having = working as I expect, even if it would be wrong<br>&gt; for smaller values than 1.0e-9. For strict egality, == can be used.<br>&gt;<br>&gt; Is it possible to disable a primitive call like here (it&#39;s optimized no ?)<br>
&gt; ?<br>&gt;<br>&gt; Anyway, thanks for your answers.<br>&gt;<br>&gt; Cédrick<br></div></div>&gt; _______________________________________________<br>&gt; Beginners mailing list<br>&gt; <a href="mailto:Beginners@lists.squeakfoundation.org">Beginners@lists.squeakfoundation.org</a><br>
&gt; <a href="http://lists.squeakfoundation.org/mailman/listinfo/beginners" target="_blank">http://lists.squeakfoundation.org/mailman/listinfo/beginners</a><br>&gt;<br><br><br>_______________________________________________<br>
Beginners mailing list<br><a href="mailto:Beginners@lists.squeakfoundation.org">Beginners@lists.squeakfoundation.org</a><br><a href="http://lists.squeakfoundation.org/mailman/listinfo/beginners" target="_blank">http://lists.squeakfoundation.org/mailman/listinfo/beginners</a><br>
</blockquote></div><br>