Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
A bug with integers

Subject: A bug with integers

From: Dinesh Bhati

Date: 4 Aug, 2010 04:11:20

Message: 1 of 6

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;

Subject: A bug with integers

From: Neil

Date: 4 Aug, 2010 04:26:04

Message: 2 of 6

"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;

Works in 2010a and 9b. What is the output from your code.

Subject: A bug with integers

From: Neil

Date: 4 Aug, 2010 04:29:05

Message: 3 of 6

"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;

Wait sorry, it doesn't. The bug is with your understanding with floating point numbers.
(floor(n)==ceil(n)) is unlikely to be true

see 0.1+0.1+0.1 == 0.3

read up on floating point.

Subject: A bug with integers

From: Roger Stafford

Date: 4 Aug, 2010 05:24:04

Message: 4 of 6

"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;
- - - - - - - - - -
  As has been pointed out numerous, numerous times in this group, modern large digital computers (rather than hand calculators) almost always use binary arithmetic, which means that they cannot make exact computations with the majority of decimal fractions. In your case matlab can express neither 1/50000 nor 0.03 exactly. When you multiply your t(i) by 1000 even when t(i) is supposedly (but not actually) equal to .027 it will not result in an exact 27. If you were using a decimal computer your code would succeed for this, but what if you asked for t=0:1/3:10000? Even the decimal machine would then fail frequently on such a test because it could not express 1/3 exactly. You will soon learn that you must not ask for exact equality as you did with

 floor(n)==ceil(n)

in such situations as this.

Roger Stafford

Subject: A bug with integers

From: James Tursa

Date: 4 Aug, 2010 06:44:11

Message: 5 of 6

"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

Subject: A bug with integers

From: Dinesh Bhati

Date: 4 Aug, 2010 11:30:23

Message: 6 of 6

"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

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us