Prev: gui, genetic algorithm
Next: Plotting
From: Dinesh Bhati on 4 Aug 2010 00:11 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;
From: neil on 4 Aug 2010 00:26 "Dinesh Bhati" <bhatidinesh(a)gmail.com> wrote in message <i3ap98$qpm$1(a)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.
From: neil on 4 Aug 2010 00:29 "Dinesh Bhati" <bhatidinesh(a)gmail.com> wrote in message <i3ap98$qpm$1(a)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.
From: Roger Stafford on 4 Aug 2010 01:24 "Dinesh Bhati" <bhatidinesh(a)gmail.com> wrote in message <i3ap98$qpm$1(a)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
From: James Tursa on 4 Aug 2010 02:44
"Dinesh Bhati" <bhatidinesh(a)gmail.com> wrote in message <i3ap98$qpm$1(a)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 |