From: Zachary on
I am completed dumbfounded as to why this is happening

% code to reproduce break in the space time continuum

i = 5
test(i,6) = 0.1733
total = test(i,6) + 0.0067
if (total == 0.1800),disp('true'),else,disp('false'),end

% end

The last line returns 'false'. Why?

If I inspect the variables, total is a 1x1 double that has the proper value of 0.1800. If I set a new variable total2 = 0.1800 it looks identical to total in the variable editor but total2 == 0.1800 returns true.

I refuse to believe MATLAB is doing something wrong here but at the same time it is giving me no clue as to what it is doing and why.

Help appreciated.
-Zach

MATLAB Version 7.6.0.324 (R2008a)
MATLAB License Number: 193***
Operating System: Microsoft Windows Vista Version 6.0 (Build 6002: Service Pack 2)
Java VM Version: Java 1.6.0 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
From: TideMan on
On Mar 9, 8:10 pm, "Zachary " <zpar...(a)so.npam.yahoo.com> wrote:
> I am completed dumbfounded as to why this is happening
>
> % code to reproduce break in the space time continuum
>
> i = 5
> test(i,6) = 0.1733
> total = test(i,6) + 0.0067
> if (total == 0.1800),disp('true'),else,disp('false'),end
>
> % end
>
> The last line returns 'false'. Why?
>
> If I inspect the variables, total is a 1x1 double that has the proper value of 0.1800. If I set a new variable total2 = 0.1800 it looks identical to total in the variable editor but total2 == 0.1800 returns true.
>
> I refuse to believe MATLAB is doing something wrong here but at the same time it is giving me no clue as to what it is doing and why.
>
> Help appreciated.
> -Zach
>
> MATLAB Version 7.6.0.324 (R2008a)
> MATLAB License Number: 193***
> Operating System: Microsoft Windows Vista Version 6.0 (Build 6002: Service Pack 2)
> Java VM Version: Java 1.6.0 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode

Oh good.
Another newbie has just discovered the marvels of floating point
arithmetic.
Check out the FAQ.
This is probably the MOST frequently asked question.
From: James Tursa on
"Zachary " <zpardos(a)so.npam.yahoo.com> wrote in message <hn4s8s$6s$1(a)fred.mathworks.com>...
> I am completed dumbfounded as to why this is happening
>
> % code to reproduce break in the space time continuum
>
> i = 5
> test(i,6) = 0.1733
> total = test(i,6) + 0.0067
> if (total == 0.1800),disp('true'),else,disp('false'),end
>
> % end
>
> The last line returns 'false'. Why?
>
> If I inspect the variables, total is a 1x1 double that has the proper value of 0.1800. If I set a new variable total2 = 0.1800 it looks identical to total in the variable editor but total2 == 0.1800 returns true.
>
> I refuse to believe MATLAB is doing something wrong here but at the same time it is giving me no clue as to what it is doing and why.
>
> Help appreciated.
> -Zach
>
> MATLAB Version 7.6.0.324 (R2008a)
> MATLAB License Number: 193***
> Operating System: Microsoft Windows Vista Version 6.0 (Build 6002: Service Pack 2)
> Java VM Version: Java 1.6.0 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode

>> num2strexact(0.1733)
ans =
0.1733000000000000095923269327613525092601776123046875
>> num2strexact(0.0067)
ans =
6.700000000000000226207941267375645111314952373504638671875e-3
>> num2strexact(0.1733+0.0067)
ans =
0.1800000000000000210942374678779742680490016937255859375
>> num2strexact(0.1800)
ans =
0.179999999999999993338661852249060757458209991455078125

Using this utility:

http://www.mathworks.com/matlabcentral/fileexchange/22239-num2strexact-exact-version-of-num2str

James Tursa
From: Zachary on
TideMan <mulgor(a)gmail.com> wrote in message <519f3da8-e58d-4795-b3d3-0180b94ed052(a)f17g2000prh.googlegroups.com>...
> Oh good.
> Another newbie has just discovered the marvels of floating point
> arithmetic.
> Check out the FAQ.
> This is probably the MOST frequently asked question.

Laying down the infinite wisdom on noobs. Praise be to you.

Looking at this output though: http://i45.tinypic.com/2zxxgt4.jpg
It's certainly easy so see how one would be stumped.

At least when you do something like 0.3 - 0.2 - 0.1 you get some evidence of floating point mayhem. When I do 0.1733 + 0.0067 and MATLAB returns 0.1800 I unfortunately believed that's what's really being stored.
From: Zachary on
"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hn500b$qg8$1(a)fred.mathworks.com>...
>
> >> num2strexact(0.1733)
> ans =
> 0.1733000000000000095923269327613525092601776123046875
> >> num2strexact(0.0067)
> ans =
> 6.700000000000000226207941267375645111314952373504638671875e-3
> >> num2strexact(0.1733+0.0067)
> ans =
> 0.1800000000000000210942374678779742680490016937255859375
> >> num2strexact(0.1800)
> ans =
> 0.179999999999999993338661852249060757458209991455078125
>
> Using this utility:
>
> http://www.mathworks.com/matlabcentral/fileexchange/22239-num2strexact-exact-version-of-num2str
>
> James Tursa

Thanks James
 |  Next  |  Last
Pages: 1 2
Prev: Image Data Ouput
Next: Image Processing