%% CS3220 -- Jan 27 2010 % Save this in a file with extension ".m" % Then you can open it in Matlab and execute the "cells" one by one % using "Evaluate Current Cell." % % First: a simple rounding error. a = 1 a + 10^-6 a + 1e-8 a + 1e-14 a + 1e-16 %% Now try that with single precision a = single(1) a + 1e-6 a + 1e-8 a + 1e-14 a + 1e-16 %% Overflow. a = single(1e38) b = a + a + a b - a - a b = a + a + a + a b - a - a - a %% Discretization, graphically % Generate a lot of points in the square [-0.5,0.5]^2 y = single(rand(100000,1)) - 0.5; x = single(rand(100000,1)) - 0.5; % plot them. Use the zoom tool to zoom in. Any structure visible? plot(x,y, '.') %% % Shift them far from the origin (not even that far...) plot(x + 1e4, y + 1e4, '.') % Now zoom in; is there structure? %% % Shift them farther... plot(x + 1e6, y + 1e6, '.') %% % ...and farther plot(x + 2e6, y + 2e6, '.') %% % Now shift them to a boundary between exponent values plot(x + 32768, y + 32768, '.') % Zoom in. What is going on here? %% Rounding in the homework example % show results in raw bits format hex single(4/3) ans - 1 3 * ans ans - 1 format %% % Now look at them in normal decimal output format long single(4/3) ans - 1 3 * ans ans - 1 format % Try this in double precision -- why is the result different?