fipref を使用したアンダーフローとオーバーフローのログ作成
オーバーフローとアンダーフローの警告のログ作成
fipref
LoggingMode
プロパティが on
に設定されていると、代入、加算、減算、乗算のすべての演算に対するオーバーフローとアンダーフローの警告のログが作成されます。たとえば、次のようにしてみます。
1 ~ 5 の値をもつベクトルで、語長が 8 ビット、小数部の長さが 6 ビットの符号付き
fi
オブジェクトを作成します。a = fi(1:5,1,8,6);
a
に関連付けられるfimath
オブジェクトを定義し、和と乗算の語長と小数部の長さを指定するように設定します。F = a.fimath; F.SumMode = 'SpecifyPrecision'; F.ProductMode = 'SpecifyPrecision'; a.fimath = F;
fipref
オブジェクトを定義し、オーバーフローとアンダーフローのログ作成を有効にします。P = fipref; P.LoggingMode = 'on';
numerictype
表示とfimath
表示を非表示にします。P.NumericTypeDisplay = 'none'; P.FimathDisplay = 'none';
和と乗算の語長と小数部の長さを指定します。
a.SumWordLength = 16; a.SumFractionLength = 15; a.ProductWordLength = 16; a.ProductFractionLength = 15;
代入演算でオーバーフローとアンダーフローの警告が表示されます。たとえば、以下のように入力します。
a(1) = pi
Warning: 1 overflow(s) occurred in the fi assignment operation. a = 1.9844 1.9844 1.9844 1.9844 1.9844
a(1) = double(eps(a))/10
Warning: 1 underflow(s) occurred in the fi assignment operation. a = 0 1.9844 1.9844 1.9844 1.9844
加算演算と減算演算でオーバーフローとアンダーフローの警告が表示されます。たとえば、以下のように入力します。
a+a
Warning: 12 overflow(s) occurred in the fi + operation. > In + (line 24) ans = 0 1.0000 1.0000 1.0000 1.0000
a-a
Warning: 8 overflow(s) occurred in the fi - operation. > In - (line 24) ans = 0 0 0 0 0
乗算演算でオーバーフローとアンダーフローの警告が表示されます。たとえば、以下のように入力します。
a.*a
Warning: 4 product overflow(s) occurred in the fi .* operation. > In .* (line 24) ans = 0 1.0000 1.0000 1.0000 1.0000
a*a'
Warning: 4 product overflow(s) occurred in the fi * operation. > In * (line 25) Warning: 3 sum overflow(s) occurred in the fi * operation. > In * (line 25) ans = 1.0000
上の最後の例は複素数乗算で、乗算演算と加算演算の両方が必要です。両方の演算でオーバーフローとアンダーフローの警告が表示されます。
オーバーフローとアンダーフローの警告のログが作成されるため、関数 dbstop
MATLAB® を次の構文で使用できます。
dbstop if warning
これにより、オーバーフローとアンダーフローの原因になっているファイルの行を正確に検出できます。
次のように使用します。
dbstop if warning fi:underflow
これにより、アンダーフローを起こしている行で停止します。次のように使用します。
dbstop if warning fi:overflow
これにより、オーバーフローを起こしている行で停止します。
関数によるログ情報へのアクセス
fipref
LoggingMode
プロパティを on
に設定しておくと、次の関数を使用して、代入と作成の演算に関するログ情報を MATLAB コマンド ラインに返すことができます。
maxlog
— 最大の実際値を返すminlog
— 最小値を返すnoverflows
— オーバーフローの数を返すnunderflows
— アンダーフローの数を返す
演算情報のログを作成するには、演算を行う前に LoggingMode
を on
に設定しておかなければなりません。ログを消去するには、関数 resetlog
を使用します。
たとえば、次の式を考えます。最初にログ作成を有効にし、次に演算を行い、最後に演算に関する情報を取得します。
fipref('LoggingMode','on'); x = fi([-1.5 eps 0.5], true, 16, 15); x(1) = 3.0;
maxlog(x)
ans = 1.0000
minlog(x)
ans = -1
noverflows(x)
ans = 2
nunderflows(x)
ans = 1
次にログをリセットし、同じ情報をもう一度要求します。演算の実行後にログをリセットしたため、関数が空の []
を返すことに注意してください。
resetlog(x)
maxlog(x)
Warning: Logging is turned on in 'maxlog'. However, no values have been logged for this variable yet. ans = []
minlog(x)
Warning: Logging is turned on in 'minlog'. However, no values have been logged for this variable yet. ans = []
noverflows(x)
Warning: Logging is turned on in 'noverflows'. However, no values have been logged for this variable yet. ans = []
nunderflows(x)
Warning: Logging is turned on in 'nunderflows'. However, no values have been logged for this variable yet. ans = []