www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/CompareFloatingPointNumbersNotEqualExample.m
%% Compare Floating-Point Numbers % Many numbers expressed in decimal text cannot be represented exactly as % binary floating numbers. This leads to small differences in results that % the |~=| operator reflects. % % Perform a few subtraction operations on numbers expressed in decimal and % store the result in |C|. C = 0.5-0.4-0.1 %% % With exact decimal arithmetic, |C| should be equal to _exactly_ |0|. Its % small value is due to the nature of binary floating-point arithmetic. %% % Compare |C| to |0| for inequality. C ~= 0 %% % Compare floating-point numbers using a tolerance, |tol|, instead of using % |~=|. tol = eps(0.5); abs(C-0) > tol %% % The two numbers, |C| and |0|, are closer to one another than two % consecutive floating-point numbers near |0.5|. In many situations, |C| % may act like |0|.