File: isequal_roundoff.m

package info (click to toggle)
suitesparse 1%3A7.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 254,920 kB
  • sloc: ansic: 1,134,743; cpp: 46,133; makefile: 4,875; fortran: 2,087; java: 1,826; sh: 996; ruby: 725; python: 495; asm: 371; sed: 166; awk: 44
file content (54 lines) | stat: -rw-r--r-- 1,288 bytes parent folder | download | duplicates (2)
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
53
54
function [ok, err, anorm] = 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-2025, All Rights Reserved.
% SPDX-License-Identifier: Apache-2.0

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

err = 0 ;
anorm = 0 ;

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) ;