How can I have a warning issued when matlab "rounds" a large number to Inf?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am working with a code that performs some calculations that probably cannot be simplified in a way that they will never result extremely large numbers that are "rounded" by Matlab to Inf, or extremely small numbers that are rounded to 0.
I coded the calculations in a way that theoretically these roundings won't be problematic, but I still want to inform the user (myself included) that these roundings have occured, by issuing some warning.
So as the header says: How can I make matlab issue a warning when it "rounds" a number to Inf and/or to 0?
2 commentaires
per isakson
le 15 Déc 2016
Something like this might do it
if sum( isinf( V(:) ) >= large_number
warning( ... )
end
Réponses (4)
per isakson
le 16 Déc 2016
Modifié(e) : per isakson
le 17 Déc 2016
I have a hammer and thus, to me your problem looks like a nail. The "hammer" is side effects of   dbstop ... if foo   where foo always returns false. Below is a simple demo, in which   with_conditional_warning   is executed and two red "warnings" appears in the command window.
Pro: The code can be run with "warnings" turned on or off. With off, there is no performance penalty. All sorts of tests can be done in foo.
Con: The lines to be monitored must be stamped with an appropriate comment. The line following a monitored line must not be else, catch, etc.
Demo:
>> filespec = 'h:\m\cssm\with_conditional_warning.m';
>> set_cw(filespec)
>> b = with_conditional_warning();
INF: Func: with_conditional_warning, Line: 2, Var: a
INF: Func: with_conditional_warning, Line: 3, Var: b
where
function b = with_conditional_warning()
a = 1/0; %@ cw(a)
b = a + 17; %@ cw(b)
17;
end
and
function set_cw( filespec )
%%set_cw sets conditional break points at the lines following
% lines with the comment "%@ cw( name_of_variable )"
%
fid = fopen( filespec, 'r' );
cac = textscan( fid, '%s', 'Delimiter','\n' );
[~] = fclose( fid );
ca_rows = regexp( cac{1}, '(?<=%@).+', 'match', 'dotexceptnewline' );
ca_rows = strtrim( ca_rows );
ix_rows = find( not( cellfun( @isempty, ca_rows, 'uni',true ) ) );
for jj = permute( ix_rows, [2,1] )
dbstop( 'in', filespec, 'at', num2str(jj+1), 'if', char(ca_rows{jj}) )
end
end
and
function out = cw( val )
narginchk( 1, 1 )
[name,~,line] = caller();
%
if isinf( val )
fprintf( 2, 'INF: Func: %s, Line: %d, Var: %s\n' ...
, name, line-1, inputname(1) );
end
out = false;
end
and
function [ name, ffs, line ] = caller()
% caller - returns the name of the caller of the currently
% running function/method
%
stk = dbstack('-completenames');
%
if length( stk ) >=3
name = stk(3).name;
ffs = stk(3).file;
line = stk(3).line;
else
name = 'base';
ffs = 'base';
line = nan;
end
end
John D'Errico
le 15 Déc 2016
You cannot set a warning whenever an inf of NaN is created. You can set the debugger to intercept this event, but that would make your code less inefficient, and it would only create a breakpoint at that line, so it would not generate a warning yet continue running.
If your code is written well, handling these numerical issues, then there is no problem. So spend the time making the computations robust to such an event.
Roger Stafford
le 16 Déc 2016
Modifié(e) : Roger Stafford
le 16 Déc 2016
Matlab overflows to infinity (or minus infinity) whenever the result of an operation would round an answer beyond the largest number Matlab can store, which in the double format is just shy of 2^1024. According to the IEEE 754 Standard specification, such an overflow should generate an ‘overflow’ exception. However, my understanding is that this exception is not implemented in Matlab’s use of the standard. A somewhat similar situation prevails for underflows to zero.
Jan
le 16 Déc 2016
The debugger option is:
dbstop if naninf
But this does not check for underflows. In addition there are wanted Infs also, e.g. for automatic axes limits. As John has mentioned already, activating the debugger will reduce the performance remarkably, because the JIT acceleration is disabled.
While checking for Inf and NaN is easy and straight forward, detecting underflows is critical. But in numerics, treating an EPS as non-zero is bad idea in every case. What about sin(pi), which replies 1.22e-16? And this is far beyond an underflow.
0 commentaires
Voir également
Catégories
En savoir plus sur Debugging and Analysis dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!