Skip to content

Instantly share code, notes, and snippets.

@pyrtsa
Created February 14, 2016 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pyrtsa/8a497b8a23f7b01b8641 to your computer and use it in GitHub Desktop.
Save pyrtsa/8a497b8a23f7b01b8641 to your computer and use it in GitHub Desktop.
Rounding and representing as string quantities with uncertainty in MATLAB.
function y = approx(x, n)
% APPROX Round the number to 1+n significant digits.
% y = APPROX(x) shorthand for round(x, 4, 'significant').
% y = APPROX(x, n) shorthand for round(x, 1 + n, 'significant').
% Copyright 2016 Pyry Jahkola
if nargin < 2
n = 3;
end
m = floor(log10(abs(x)));
unit = 10 .^ (max(m) - n);
y = bsxfun(@times, round(bsxfun(@rdivide, x, unit)), unit);
function str = concise(x, d)
% CONCISE Concise representation of a quantity value x ± d.
% str = CONCISE(x, d) returns the concise form string representation of
% x ± d where the precision in x is kept up to the two most significant
% digits of d and those two digits are shown in parentheses.
%
% The result is shown with power-of-ten representation which is omitted
% if the exponent is zero.
%
% Examples
%
% CONCISE(3.14159, 0.0485) %=> '3.142(49)'
% CONCISE(6.02214129e23, 2.7e16) %=> '6.02214129(27) × 10^23'
% CONCISE(6.67408e-11, 3.1e-15) %=> '6.67408(31) × 10^-11'
% Copyright 2016 Pyry Jahkola
xmag = floor(log10(abs(x)));
dmag = floor(log10(abs(d)));
mag = max(xmag, dmag);
prec = mag - dmag + 1;
x = round(x, 1 - dmag);
if dmag >= 0
D = round(d / 10 ^ (dmag - 1));
else
D = round(d * 10 ^ (1 - dmag));
end
if mag > 0
X = x / 10 ^ mag;
fmt = sprintf('%%.%df(%%02d) × 10^%%d', prec);
str = sprintf(fmt, X, D, mag);
elseif mag < 0
X = x * 10 ^ -mag;
fmt = sprintf('%%.%df(%%02d) × 10^%%d', prec);
str = sprintf(fmt, X, D, mag);
else
X = x / 10 ^ mag;
fmt = sprintf('%%.%df(%%02d)', prec);
str = sprintf(fmt, X, D);
end
function m = magnitude(x)
% MAGNITUDE Get the power-of-ten order of a number.
% m = MAGNITUDE(x) returns the largest integer m where 10^m <= abs(x).
% m = MAGNITUDE(0) returns -Inf.
% Copyright 2016 Pyry Jahkola
m = floor(log10(abs(x)));
function str = plusminus(x, d)
% PLUSMINUS Full representation of quantity value x ± d
% str = PLUSMINUS(x, d) returns the value x ± d as a string of
% form of either '(x ± d) × 10^e' or 'x ± d', keeping precision
% up to two most significant digits of d, and omitting the
% power-of-ten representation for values within range [0.1, 1000).
%
% Examples
%
% PLUSMINUS(3.14159, 0.0485) %=> '3.142 ± 0.049'
% PLUSMINUS(6.02214129e23, 2.7e16) %=> '(6.02214129 ± 0.00000027) × 10^23'
% PLUSMINUS(6.67408e-11, 3.1e-15) %=> '(6.67408 ± 0.00031) × 10^-11'
% Copyright 2016 Pyry Jahkola
xmag = floor(log10(abs(x)));
dmag = floor(log10(abs(d)));
mag = max(xmag, dmag);
prec = mag - dmag + 1;
x = round(x, 1 - dmag);
d = round(d, 1 - dmag);
if mag > 3
X = x / 10 ^ mag;
D = d / 10 ^ mag;
fmt = sprintf('(%%.%df ± %%.%df) × 10^%%d', prec, prec);
str = sprintf(fmt, X, D, mag);
elseif mag < -2
X = x * 10 ^ -mag;
D = d * 10 ^ -mag;
fmt = sprintf('(%%.%df ± %%.%df) × 10^%%d', prec, prec);
str = sprintf(fmt, X, D, mag);
elseif xmag > dmag && mag < 0
fmt = sprintf('%%#.%dg ± %%.%dg', prec + 1, prec + 1);
str = sprintf(fmt, x, d);
else
fmt = sprintf('%%.%df ± %%.%df', max(0, 1 - dmag), max(0, 1 - dmag));
str = sprintf(fmt, x, d);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment