near.m 731 Bytes
Newer Older
1
function [i, m, s] = near(nominalValue, actualValue, tolerance, relative)
2
3
4
5
6
7
8
9
10
11
12
13
14
%NEAR checks 'equality' for double numerical values.
%   i = near(nominalValue, actualValue, tolerance) checks if.
%       abs(nominalValue - actualValue) < tolerance.
%   The argument tolerance is optional and is 10*eps by default.

if nargin <= 2
    tolerance = 10*eps;
end

if nargin == 1
	actualValue = zeros(size(nominalValue));
end

15
16
17
18
19
20
21
22
23
24
if nargin < 4
	relative = 1;
else
	if islogical(relative)
		relative = max(abs(nominalValue(:)));
	end
end
	

absV = abs(nominalValue ./ relative - actualValue ./ relative);
25
26
27
i = all(absV(:) < tolerance);
m = max( absV(:) );

28
29
s = sprintf('Is near %i, maximal difference: %d', [i, m]);

30
if nargout == 0
31
	i = s;
32
33
34
end

end