% explore_eps22.m % k. stewart spring 1998 - written for CS 205 % we want to examine the behavior of the floating point % number system %** updated to write nicely formatted output to a file %********************************************************* % % assuming no background or familiarity with floating point % behavior, we'll examine first a finite list of values % % it's dangerous to use the variable named "eps" since this is % predefined MATLAB value which the user can reset % echo off u = 1/2^15; % start the search off at an arbitrary, but exact, binary value formax = 40; for i=1:formax if (1.0 + u > 1.0) ; fprintf('explore_eps2.out','For u = %12.8g \t 1.0+u> 1.0 \n',u) else fprintf('explore_eps2.out',... 'Finally, after %4.0f iterations we find that\n',i) fprintf('explore_eps2.out',... ' for u = %12.8g 1.0+u is NOT > 1.0 \n',u) fprintf('explore_eps2.out',... ' therefore unit roundoff is %12.8g\n',2*u) fprintf('explore_eps2.out',... ' Break out of the for loop\n') break; end % if-else block u = u/2; if (i == formax) fprintf('explore_eps2.out',... 'Used %g loop iterations without finding u\n',formax) end end % for i=1:10 loop