MATLAB Answers

Divide by Zero

377 views (last 30 days)
D G on 26 Jun 2012
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.
  1 Comment
D G on 26 Jun 2012
I would also like to know if it is possible to have MATLAB throw an error on divide by zero.

Sign in to comment.

Accepted Answer

Thomas on 26 Jun 2012
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.
Sharon on 27 Jun 2012
+1 for the concise answer..

Sign in to comment.

More Answers (1)

Jan on 26 Jun 2012
For stopping on division by zero see:
warning on MATLAB:divideByZero
dbstop if warning MATLAB:divideByZero
dbstop if naninf


Community Treasure Hunt

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

Start Hunting!