can anybody give me a example showing how to avoid division by 0 by using try-catch?
18 ビュー (過去 30 日間)
古いコメントを表示
can anybody give me a example showing how to avoid division by 0 by using try-catch? thank you very much!
2 件のコメント
Jan
2011 年 10 月 7 日
TRY-CATCH does not *avoid* the division by zero. In opposite: The term "try" means, that MATLAB tries to calculate the division and calls the CATCH block, if an error occurs.
採用された回答
Grzegorz Knor
2011 年 10 月 7 日
Dividing by zero does not return an error in Matlab. So I suggest you to use if - else instead try - catch:
for k = -5:5
if k~=0
x = 1./k;
disp(x)
else
disp('you try divide by zero!')
end
end
3 件のコメント
Walter Roberson
2011 年 10 月 7 日
I do not remember the details at the moment, but I seem to recall that division by zero is noticed and reported via a warning or error (I do not remember which), except that that warning or error is turned off by default.
その他の回答 (1 件)
Walter Roberson
2011 年 10 月 7 日
Don't do that. Do like I recommended earlier: calculate the denominator and assign the result to a variable, and test the range of the variable, and take appropriate corrective action if it is smaller than you want.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Performance and Memory についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!