f

#### "==" is NOT TRUE "==", WHY?

```[CODE START]
x=5;
y=8;
if (x + y + 1E-15 == 13)
a = 3
else
a = 8
end
[CODE END]

When x + y + 1E-15, the code above returns a = 8.
When x + y + 1E-16, the code above returns a = 3.

``` 0 8/5/2008 8:40:19 PM comp.soft-sys.matlab  211266 articles. 24 followers. 5 Replies 987 Views Similar Articles

[PageSpeed] 9

```"onemilimeter Chen" <onemm@example.com> wrote in message
> [CODE START]
> x=5;
> y=8;
> if (x + y + 1E-15 == 13)
>     a = 3
> else
>     a = 8
> end
> [CODE END]
>
> When x + y + 1E-15, the code above returns a = 8.
> When x + y + 1E-16, the code above returns a = 3.
>

i believe it's due to the fact that 1e-16 is less than eps.
basically, it gets ignored as roundoff error so the value
remains "exactly" 13 for the addition. you shouldn't be
comparing floating point numbers to each other directly --
just check that they're within some tolerance.
``` 0 8/5/2008 8:50:04 PM
```"onemilimeter Chen" <onemm@example.com> wrote in message
> [CODE START]
> x=5;
> y=8;
> if (x + y + 1E-15 == 13)
>     a = 3
> else
>     a = 8
> end
> [CODE END]
>
> When x + y + 1E-15, the code above returns a = 8.
> When x + y + 1E-16, the code above returns a = 3.
>

Try help EPS. Everything equal or below eps(x+y)/2 %
8.8818e-016 is numerically 'zero' when adding to (x+y).

Bruno

``` 0 8/5/2008 9:00:19 PM
```if you use this command
>> format long

you will see that
>> x + y + 1E-15

ans =

13.000000000000002

>> x + y + 1E-16

ans =

13

notice that since 1E-16 is very small Matlab truncates it
while 1E-15 is "large" enough for Matlab to considerate it.

Thus 13.000000000000002 == 13 => FALSE!

Be happy!
Ivan

"onemilimeter Chen" <onemm@example.com> wrote in message
> [CODE START]
> x=5;
> y=8;
> if (x + y + 1E-15 == 13)
>     a = 3
> else
>     a = 8
> end
> [CODE END]
>
> When x + y + 1E-15, the code above returns a = 8.
> When x + y + 1E-16, the code above returns a = 3.
>

``` 0 8/5/2008 9:45:04 PM
```eps(13) = 1.776356839400251e-015

So anything smaller than half this, when added to 13, will
not produce a number greater than 13.

N = 13;
(N + eps(N)/2) == N

ans =

1
``` 0 8/5/2008 11:13:01 PM
```In article <g7adrj\$5tr\$1@fred.mathworks.com>, onemm@example.com says...
> [CODE START]
> x=5;
> y=8;
> if (x + y + 1E-15 == 13)
>     a = 3
> else
>     a = 8
> end
> [CODE END]
>
> When x + y + 1E-15, the code above returns a = 8.
> When x + y + 1E-16, the code above returns a = 3.
>
>

Numerical roundoff. Check out this post:
http://blogs.mathworks.com/loren/2006/08/23/a-glimpse-into-floating-
point-accuracy/

Look at the help for eps.

--
Loren
http://blogs.mathworks.com/loren/
``` 0 8/6/2008 11:58:16 AM