How can I have a warning issued when matlab "rounds" a large number to Inf?

4 views (last 30 days)
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 Comments
per isakson
per isakson on 15 Dec 2016
Something like this might do it
if sum( isinf( V(:) ) >= large_number
warning( ... )
end
Oriel Nofekh
Oriel Nofekh on 16 Dec 2016
(I guess you meant: >= 1 ?) Maybe I didn't explain myself well enough. I would like to tell the Matlab system that whenever it decides to assign Inf instead of a big number, it will also issue a warning. There are cases where 0 is a legitimate value, but there are cases it is not legitimate but it was just rounded to 0. There are also some cases where Inf is a legitimate value, but there are cases it was only rounded to Inf. Also, the issue of 0 or Inf is not necessariliy in the final result, it might be an intermediate value inside a formula. I can analyze each case and make many if's in my program, but that would make the code less clear, and it might increase the time of running significantly. I also might miss something.

Sign in to comment.

Answers (4)

per isakson
per isakson on 16 Dec 2016
Edited: per isakson on 17 Dec 2016
I have a hammer and thus, to me your problem looks like a nail. The "hammer" is side effects of &nbsp dbstop ... if foo &nbsp where foo always returns false. Below is a simple demo, in which &nbsp with_conditional_warning &nbsp 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
  1 Comment
Oriel Nofekh
Oriel Nofekh on 17 Dec 2016
Thanks for the very elaborate solution. I don't think it will fit in my current code, but I might use it in the future for other programs. Thanks!

Sign in to comment.


John D'Errico
John D'Errico on 15 Dec 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.
  1 Comment
Oriel Nofekh
Oriel Nofekh on 16 Dec 2016
So you're saying that is not possible. Too bad :(
I agree that making sure as much as possible that the code performs the computation well is first-priority, but as I said it is quite complicated and I might overlook a certain situation that may occur, that is why I was looking for such feature.
Thanks.

Sign in to comment.


Roger Stafford
Roger Stafford on 16 Dec 2016
Edited: Roger Stafford on 16 Dec 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
Jan on 16 Dec 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.

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!