[scilab-Users] %eps ?!
Michaël Baudin
michael.baudin at scilab.org
Wed Dec 15 17:10:24 CET 2010
Hi,
This is a nice one...
To understand the issue, you may run the following lines :
atomsInstall(['floatingpoint','0.2']);
atomsLoad(['floatingpoint','0.2']);
flps = flps_systemnew ( "IEEEdouble" );
flpn = flps_numbernew ( "double" , flps , 0.4 )
This will print the binary decomposition of 0.4 :
Significand = 1001100110011001100110011001100110011001100110011010
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.
If you cannot change the test, you may change the condition to :
i = 3;
test = .4*i - 1.2 ;
abs(test)<= %eps
If you can change the test, you can simply use integers, which can be
safely represented exactly in the range [-2^52,2^52] :
i = 3;
test = 4*i - 12 ;
test==0
Best regards,
Michaël Baudin
PS
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 :
http://www.scilab.org/en/support/documentation/tutorials
See the section A.1, "Why 0.1 is rounded".
Le 15/12/2010 14:47, Frédéric Mabille a écrit :
> Hi,
>
> During the computation of my program, my function "if" produce a
> strange result.
> I fact, after many investigations, I have observed a very surprising
> result.
>
> /for i = 1:10/
> /test = .4*i - 1.2 ;/
> /if test == 0 then .../
>
> But when i = 3, scilab give me : test = %eps = -2.220D-16 <> 0 !!!
> It is really awkward.
>
> Could someone have a simple solution ?
> Thx
>
> Frédéric
--
Michaël Baudin
Ingénieur de développement
michael.baudin at scilab.org
-------------------------
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20101215/2eee8091/attachment.htm>
More information about the users
mailing list