help-octave
[Top][All Lists]

## Re: Numeric issue

 From: Juan Pablo Carbajal Subject: Re: Numeric issue Date: Fri, 4 Jul 2014 15:56:59 +0200

```On Fri, Jul 4, 2014 at 11:46 AM, JokerOne
> Hi community,
>
> I using Octave for quite a while now, however, apparently, I am not aware of
> some numeric issues going on, which is where I like to ask you for help:
>
> Basically, I noticed, that in the following dummy code, the results appear
> strange to me:
>
> clear all;
> format long;
>
> a   = [1.1:1.1:100];
> a(50)                     % expected to be == 55
> b   = 50*1.1;          % expected to be == 55
>
> ((a(50)-b) == 0)     % is 1
> ((a(50)-55) == 0)    % is 0 (!?)
> ceil(a(50))              % expected to be == 55, but results in 56 ??
> ceil(b)                    % same as above ??
> ceil(55)                   % == 55, as expected
>
> -->
> ans =  55.0000000000000
> ans =  1
> ans = 0
> ans =  56
> ans =  56
> ans =  55
>
> also,
> "
> a
> "
> results in:
> [...]
>  Columns 49 through 52:
>
>    53.90000000000001   55.00000000000001   56.10000000000000
> 57.20000000000000
> [...]
>
> with a strange result for 55 --> 55.00000000000001
>
> I guess, all this is known to you experts. Do you have a good advice for me,
> how to cope with such numeric issues, when exact results are important?
>
> I am using Octave 3.8.1 on a Win7 machine, btw.
>
> Any help is appreciated
>
> Max
>
>
>
>
> --
> View this message in context:
> http://octave.1599824.n4.nabble.com/Numeric-issue-tp4665228.html
> Sent from the Octave - General mailing list archive at Nabble.com.
>
> _______________________________________________
> Help-octave mailing list
> https://lists.gnu.org/mailman/listinfo/help-octave

Hi Max,

You are dealing with integer representation of doubles, so their are
not exact unless you force them to be integers (using int16(a(50)))

When comparing doubles you need to add a tolerance (or in general
check for bigger or lower because equal doesn't make much sense). A
reasonable tolerance is given by sqrt(eps).

If you check

a(50)-55

you will see that there is a small difference (of the order of eps in
your machine). You should include this in the comparision, something
like

abs(a(50)-55) < sqrt(eps)

Cheers

```