File: isequal_roundoff.m

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 506; asm: 369; python: 125; awk: 10
file content (52 lines) | stat: -rw-r--r-- 1,252 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function ok = isequal_roundoff (A,B,tol)
%ISEQUAL_ROUNDOFF compare two matrices, allowing for roundoff errors
% 
% returns true if A == B to within relative tolerance tol.
% tol = 64*eps if not present.  NaNs and Infs are ignored in the
% tol, but the NaN and +/-Inf pattern must be the same.

% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
% SPDX-License-Identifier: Apache-2.0

% if (~isequal (GB_spec_type (A), GB_spec_type (B)))
%     ok = false ;
%     return ;
% end

if (isequalwithequalnans (A, B))
    ok = true ;
    return
end

% floating-point NaN pattern must match exactly
if (~isequal (isnan (A), isnan (B)))
    % pattern of NaNs is different
    ok = false ;
    return
end

% remove NaNs
A (isnan (A)) = 0 ;
B (isnan (B)) = 0 ;

% floating-point Inf pattern must match exactly;
% treat +Inf and -Inf differently
if (~isequal (sign (A) .* isinf (A), sign (B) .* isinf (B)))
    % pattern of +Inf/-Inf is different
    ok = false ;
    return
end

% remove the Infs
A (isinf (A)) = 0 ;
B (isinf (B)) = 0 ;

% values must close, to within relative tol
err = norm (A - B, 1) ;
anorm = norm (A, 1) ;
if (nargin < 3)
    tol = 64*eps ;
end
anorm = max (anorm, 1) ;
ok = (err == 0) || (err <= tol * anorm) ;