Path: news.mathworks.com!not-for-mail
From: "Dinesh Bhati" <bhatidinesh@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: A bug with integers
Date: Wed, 4 Aug 2010 11:30:23 +0000 (UTC)
Organization: asda
Lines: 89
Message-ID: <i3bj0f$cr3$1@fred.mathworks.com>
References: <i3ap98$qpm$1@fred.mathworks.com> <i3b27r$80v$1@fred.mathworks.com>
Reply-To: "Dinesh Bhati" <bhatidinesh@gmail.com>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1280921423 13155 172.30.248.37 (4 Aug 2010 11:30:23 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 4 Aug 2010 11:30:23 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1244691
Xref: news.mathworks.com comp.soft-sys.matlab:658941

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <i3b27r$80v$1@fred.mathworks.com>...
> "Dinesh Bhati" <bhatidinesh@gmail.com> wrote in message <i3ap98$qpm$1@fred.mathworks.com>...
> > Please run the following script and observe the bug in ver 7.5.0.342(R2007b)
> > clear
> > clc
> > t=0:1/50000:0.03;
> > disp('Following script is able to catch the integer 27');
> > for i =1:length(t)
> > 
> >             n=t(i)*1000;
> >                 if n> 26.96 && n < 27.04
> > n
> >             if (floor(n)==ceil(n)) disp(n);
> >             end;end;
> > end;
> > 
> > disp('But this script is not able to catch the integer 28? IS IT A BUG IN MATLAB');
> > for i =1:length(t)
> > 
> >             n=t(i)*1000;
> >                 if n> 27.96 && n < 28.04
> > n
> >             if (floor(n)==ceil(n)) disp(n);
> >             end;end;
> > end;
> 
> If you want to explore what is going on in more detail, you can use my FEX submission num2strexact. e.g.,
> 
> >> t=0:1/50000:0.03;
> >> num2strexact(t(1351)*1000)
> ans =
> 2.7e1
> >> num2strexact(t(1401)*1000)
> ans =
> 2.7999999999999996447286321199499070644378662109375e1
> 
> As you can see, the 27 *happens* to be exactly represented in the array, but the 28 is not. If you are really interested in the "integer" values to be exact in the t*1000 array, you should form the t*1000 array result differently. e.g., ponder these results:
> 
> >> w=(0:1500)/50;
> >> num2strexact(w(1351))
> ans =
> 2.7e1
> >> num2strexact(w(1401))
> ans =
> 2.8e1
> 
> 
> num2strexact can be found here:
> 
> http://www.mathworks.com/matlabcentral/fileexchange/22239-num2strexact-exact-version-of-num2str
> 
> 
> James Tursa
Thanks James , Can you please help me in sampling following sine waves
close all;
clear
clc
f1=100;f2=900;f3=1100;f4=1900;f5=2100;z2=[];z1=[];
z3=[];z4=[];z5=[];
t=0:1/100000:0.03;
z1s=zeros(1,length(t));
for i =1:length(t)
    p1=sin(2*pi*f1*t(i));
    z1=[z1 p1];
   p2=sin(2*pi*f2*t(i));
    z2=[z2 p2];
     p3=sin(2*pi*f3*t(i));
    z3=[z3 p3];
    p4=sin(2*pi*f4*t(i));
    z4=[z4 p4];
     p5=sin(2*pi*f5*t(i));
    z5=[z5 p5];
   n=t(i)*1000;
            if (floor(n)==ceil(n)) z1s(i)=p1;z2s(i)=p2;z3s(i)=p3;z4s(i)=p4;z5s(i)=p5;
                disp(n)
            end;
end;
subplot(4,1,1);plot(t,z1s,t,z1);
subplot(4,1,2);plot(t,z2s,t,z2);
subplot(4,1,3);plot(t,z3s,t,z3);
pol=z1+z2+z3+z4;
subplot(4,1,4);plot(t,z1s,t,z1,t,pol);
figure
plot(t,z1s,t,pol);
figure
subplot(211);plot(t,z1s);
subplot(212);plot(t,pol);
Regards,
Dinesh