Divide by Zero

I am wondering, why does MATLAB give infinity for the calculation 1 / 0?
Shouldn't this throw an error, return NaN, or be a complex infinity?
Just looking for some reasoning for this invalid calculation.
Thanks!

1 件のコメント

D G
D G 2012 年 6 月 26 日
I would also like to know if it is possible to have MATLAB throw an error on divide by zero.

サインインしてコメントする。

 採用された回答

Thomas
Thomas 2012 年 6 月 26 日

8 投票

Actually Inf returns the IEEE arithmetic representation for positive infinity.
Infinity results from operations like division by zero and overflow, which lead to results too large to represent as conventional floating-point values. An attempt to divide a floating point number by zero will lead to +-infinity by the IEEE 754 floating point standard
while NaN returns the IEEE arithmetic representation for Not-a-Number (NaN). These result from operations which have undefined numerical results. The IEEE floating-point standard, supported by almost all modern floating-point units, specifies that every floating point arithmetic operation, including division by zero, has a well-defined result.

7 件のコメント

D G
D G 2012 年 6 月 26 日
But MATLAB returns positive infinity. Shouldn't it return a cmoplex infinity?
Thomas
Thomas 2012 年 6 月 26 日
I guess its just the way MATLAB chose to represent it.. I know that Mathematica shows a ComplexInfinity result..
D G
D G 2012 年 6 月 26 日
Of course this makes things really easy when you may have a positive infinite and a divide by zero at the same time! Come on MATLAB, this would take 5 minutes.
Thomas
Thomas 2012 年 6 月 26 日
Look in the symbolic math toolbox http://www.mathworks.com/help/toolbox/mupad/stdlib/cinfty.html
Walter Roberson
Walter Roberson 2012 年 6 月 26 日
It would take a *lot* more than 5 minutes. MATLAB is allowing the CPU to determine the value, and the standards say that sign() of the numerator times infinity is the result so that is what is implemented in hardware. In order for MATLAB to catch it, MATLAB would have to add a test for the denominator being 0 _every_ place that it has a division, along with handling code that inserted proper answer and then continued. Changing to complex in mid calculation would be difficult as there are no hardware instructions to deal with complex values.
Mathematica does all its divisions in software, not in hardware, so it is much easier to make the change for it.
James Tursa
James Tursa 2012 年 6 月 26 日
MATLAB certainly does it the way I want, which is to generate a real result for real inputs, and to let the h/w do the native IEEE calculation without inserting a lot of performance killing checks like Walter mentioned. I certainly would not want those checks to be the default if it significantly slowed performance. You can always manually check your results for NaN or Inf, and if you need it to stop then maybe do what Jan suggests.
Sharon
Sharon 2012 年 6 月 27 日
+1 for the concise answer..

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2012 年 6 月 26 日

3 投票

For stopping on division by zero see:
warning on MATLAB:divideByZero
dbstop if warning MATLAB:divideByZero
and
dbstop if naninf

カテゴリ

タグ

質問済み:

D G
2012 年 6 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by