Mercurial > matrix-functions
diff toolbox/chop.m @ 2:c124219d7bfa draft
Re-add the 1995 toolbox after noticing the statement in the ~higham/mctoolbox/ webpage.
author | Antonio Pino Robles <data.script93@gmail.com> |
---|---|
date | Thu, 07 May 2015 18:36:24 +0200 |
parents | 8f23314345f4 |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/toolbox/chop.m Thu May 07 18:36:24 2015 +0200 @@ -0,0 +1,18 @@ +function c = chop(x, t) +%CHOP Round matrix elements. +% CHOP(X, t) is the matrix obtained by rounding the elements of X +% to t significant binary places. +% Default is t = 24, corresponding to IEEE single precision. + +if nargin < 2, t = 24; end +[m, n] = size(x); + +% Use the representation: +% x(i,j) = 2^e(i,j) * .d(1)d(2)...d(s) * sign(x(i,j)) + +% On the next line `+(x==0)' avoids passing a zero argument to LOG, which +% would cause a warning message to be generated. + +y = abs(x) + (x==0); +e = floor(log2(y) + 1); +c = pow2(round( pow2(x, t-e) ), e-t);