<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#ffffff" text="#000000">
    Hi,<br>
    <br>
    This is a nice one...<br>
    <br>
    To understand the issue, you may run the following lines :<br>
    <br>
    atomsInstall(['floatingpoint','0.2']);<br>
    atomsLoad(['floatingpoint','0.2']);<br>
    flps = flps_systemnew ( "IEEEdouble" );<br>
    flpn = flps_numbernew ( "double" , flps , 0.4 )<br>
    <br>
    This will print the binary decomposition of 0.4 :<br>
    <br>
    Significand = 1001100110011001100110011001100110011001100110011010<br>
    <br>
    You see that the significand makes use of the pattern 1100. This
    means that x=0.4 can be represented in base 2 only with an infinite
    sequence of ones and zeros. You may see the same behaviour with
    x=0.1. But only 53 binary digits can be considered, because this is
    the precision of doubles in Scilab. This leads to rounding, i.e. the
    floating point number which is stored is the floating point number
    which is the closest (but not equal) to 0.4.<br>
    <br>
    If you cannot change the test, you may change the condition to :<br>
    <br>
    i = 3;<br>
    test = .4*i - 1.2 ;<br>
    abs(test)<= %eps<br>
    <br>
    If you can change the test, you can simply use integers, which can
    be safely represented exactly in the range [-2^52,2^52] :<br>
    <br>
    i = 3;<br>
    test = 4*i - 12 ;<br>
    test==0<br>
    <br>
    Best regards,<br>
    <br>
    Michaël Baudin<br>
    <br>
    PS<br>
    A more detailed explanation for x=0.1 is given in "Scilab is not
    naive", Digiteo, Baudin, 2010, provided in the Tutorials section of
    the site scilab.org :<br>
    <br>
    <a href="http://www.scilab.org/en/support/documentation/tutorials">http://www.scilab.org/en/support/documentation/tutorials</a><br>
    <br>
    See the section A.1, "Why 0.1 is rounded".<br>
    <br>
    Le 15/12/2010 14:47, Frédéric Mabille a écrit :
    <blockquote
      cite="mid:366F5457-0D8D-4982-A7EB-4468ACE054D2@supagro.inra.fr"
      type="cite">Hi,
      <div><br>
      </div>
      <div>During the computation of my program, my function "if"
        produce a strange result.</div>
      <div>I fact, after many investigations, I have observed a very
        surprising result.</div>
      <div><br>
      </div>
      <div><i>for i = 1:10</i></div>
      <div><i>test = .4*i - 1.2 ;</i></div>
      <div><i>if test == 0 then ...</i></div>
      <div><br>
      </div>
      <div>But when i = 3, scilab give me : test = %eps = -2.220D-16
        <> 0 !!!</div>
      <div>It is really awkward.</div>
      <div><br>
      </div>
      <div>Could someone have a simple solution ?</div>
      <div>Thx </div>
      <div><br>
      </div>
      <div>
        <div>
          <span class="Apple-style-span" style="border-collapse:
            separate; color: rgb(0, 0, 0); font-family: Helvetica;
            font-size: medium; font-style: normal; font-variant: normal;
            font-weight: normal; letter-spacing: normal; line-height:
            normal; orphans: 2; text-indent: 0px; text-transform: none;
            white-space: normal; widows: 2; word-spacing: 0px;"><span
              class="Apple-style-span" style="border-collapse: separate;
              color: rgb(0, 0, 0); font-family: Helvetica; font-size:
              medium; font-style: normal; font-variant: normal;
              font-weight: normal; letter-spacing: normal; line-height:
              normal; orphans: 2; text-indent: 0px; text-transform:
              none; white-space: normal; widows: 2; word-spacing: 0px;">
              <div style="word-wrap: break-word;">
                <div><span class="Apple-style-span" style="font-size:
                    12px;">
                    <div><font class="Apple-style-span" face="Times"><span
                          class="Apple-style-span" style="font-family:
                          Times;">Frédéric </span></font></div>
                  </span></div>
              </div>
            </span></span></div>
      </div>
    </blockquote>
    <br>
    <br>
    <pre class="moz-signature" cols="72">-- 
Michaël Baudin
Ingénieur de développement
<a class="moz-txt-link-abbreviated" href="mailto:michael.baudin@scilab.org">michael.baudin@scilab.org</a>
-------------------------
Consortium Scilab - Digiteo
Domaine de Voluceau - Rocquencourt
B.P. 105 - 78153 Le Chesnay Cedex
Tel. : 01 39 63 56 87 - Fax : 01 39 63 55 94

</pre>
  </body>
</html>