1function ok = isequal_roundoff (A,B,tol)
2%ISEQUAL_ROUNDOFF compare two matrices, allowing for roundoff errors
3%
4% returns true if A == B to within relative tolerance tol.
5% tol = 64*eps if not present.  NaNs and Infs are ignored in the
6% tol, but the NaN and +/-Inf pattern must be the same.
7
8% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
9% SPDX-License-Identifier: Apache-2.0
10
11% if (~isequal (GB_spec_type (A), GB_spec_type (B)))
12%     ok = false ;
13%     return ;
14% end
15
16if (isequalwithequalnans (A, B))
17    ok = true ;
18    return
19end
20
21% floating-point NaN pattern must match exactly
22if (~isequal (isnan (A), isnan (B)))
23    % pattern of NaNs is different
24    ok = false ;
25    return
26end
27
28% remove NaNs
29A (isnan (A)) = 0 ;
30B (isnan (B)) = 0 ;
31
32% floating-point Inf pattern must match exactly;
33% treat +Inf and -Inf differently
34if (~isequal (sign (A) .* isinf (A), sign (B) .* isinf (B)))
35    % pattern of +Inf/-Inf is different
36    ok = false ;
37    return
38end
39
40% remove the Infs
41A (isinf (A)) = 0 ;
42B (isinf (B)) = 0 ;
43
44% values must close, to within relative tol
45err = norm (A - B, 1) ;
46anorm = norm (A, 1) ;
47if (nargin < 3)
48    tol = 64*eps ;
49end
50anorm = max (anorm, 1) ;
51ok = (err == 0) || (err <= tol * anorm) ;
52
53