Path: news.mathworks.com!newsfeed-00.mathworks.com!solaris.cc.vt.edu!news.vt.edu!news.glorb.com!aioe.org!.POSTED!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Greater than problem, please help
Date: Sat, 21 Apr 2012 15:46:18 -0500
Organization: Aioe.org NNTP Server
Lines: 68
Message-ID: <jmv6ao$dbi$1@speranza.aioe.org>
References: <jmv550$mi9$1@newscl01ah.mathworks.com>
NNTP-Posting-Host: J10WVVLJReCVNEO5HfmTzw.user.speranza.aioe.org
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Complaints-To: abuse@aioe.org
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.27) Gecko/20120216 Thunderbird/3.1.19
X-Notice: Filtered by postfilter v. 0.8.2
Xref: news.mathworks.com comp.soft-sys.matlab:765452

On 4/21/2012 3:26 PM, Orhan Unal wrote:
...

> ... The following line
>
> Tree = S0*up.^((steps:-1:0)').*down.^((0:steps)')
>
> produces the following results
>
> Tree =
>
...
> 82.4567
> 67.9528
> 56.0000
> 46.1497
...

>
> Then the code
>
> for i=1:steps+1
>   if K-Tree > 0
>     Tree(i) = 1;
>   else
>     Tree(i) = 0;
>   end
> end

This isn't your problem (keep reading), but...

The above "the Matlab way" (tm) is just

Tree(K>Tree)=1;

...

> This works fine for all values that AREN'T K=56. But you see, when K=56
> notice how it corresponds to the value 56 in the first Tree results;
> When computing K-Tree, 56-56 should return 0 in the second Tree results
> in that position. However, I get the value 1 and it's seriously
> confusing me because I don't know how to fix it, I've tried so many
> things out and nothing seems to work. Please help me, thanks.

Try the following at the command line and I think you'll see the reason 
for the "problem".

num2str(Tree,15)

The value that appears to be identically 56 will, indeed show up to be 
just slightly off.  The reason is it was computed w/ a floating point 
expression whereas the integer value you're comparing to to is, indeed, 
exact.

You'll need a tolerance on the comparison if it's critical that the 
decision point be there; of course, the actual internally represented 
value that was calculated wasn't _quite_ there so that's a question of 
what should you do w/ the decision at other breakpoints.

Or, perhaps if this is pricing you should round to the nearest 
fractional cent that's traded; I don't know the answer to that.

Look at the following link to get the gist of what's going on in more 
detail...

<http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F>

--