% unitround.m % k stewart - spring 1998 - written for CS 205 % We will compute the Unit Round Off - now that we have a % feeling for how the "arithmetic" works % u = 1.0; % need to start somewhere - why not at 1.0? % count how many times we go through this loop count = 0; while (1.0 + u > 1.0), count = count + 1; % in c, you could simplify to count += 1; ubig = u; % u still changed 1.0, so keep track of that value in ubig u = u/2.0; end; % we now have the first value of u so that 1.0+u <= 1.0, although % we were searching for the value that DID change 1.0, so % that occurred in the iteration right before the loop exit % % after running the "while" loop fprintf('After %4.0f iterations of while loop, we find\n',count) fprintf(' unitroundoff = %14.8g \n',ubig) echo on % % matlab provides this value through the variable "eps" help eps % and we can see this value without computing our own % version in the while loop above by simply printing it % out by given the variables name on the MATLAB command line format short e eps