help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Numeric issue


From: c.
Subject: Re: Numeric issue
Date: Fri, 4 Jul 2014 15:31:25 +0200

On 4 Jul 2014, at 11:46, JokerOne <address@hidden> wrote:

> 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

This question is not about Octave, it is a more fundamental issue about
floating point arithmetics.

Usually Jordi likes to respond to this question by referring to this link:
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

There is also an entry about this in the FAQ page on the wiki:
http://wiki.octave.org/FAQ#Why_is_this_floating_point_computation_wrong.3F

Anyway, a good "rule of thumbs" is: never compare two floating point numbers 
with "==".

c.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]