| Programming | ![]() |
Infinity and NaN
MATLAB uses the special values inf, -inf, and NaN to represent values that are positive and negative infinity, and not a number respectively.
Infinity
MATLAB represents infinity by the special value inf. Infinity results from operations like division by zero and overflow, which lead to results too large to represent as conventional floating-point values. MATLAB also provides a function called inf that returns the IEEE arithmetic representation for positive infinity as a double scalar value.
Several examples of statements that return positive or negative infinity in MATLAB are shown here.
x = 1/0 |
x = 1.e1000 |
x = exp(1000) |
x = log(0) |
Use the isinf function to verify that x is positive or negative infinity:
NaN
MATLAB represents values that are not real or complex numbers with a special value called NaN, which stands for Not a Number. Expressions like 0/0 and inf/inf result in NaN, as do any arithmetic operations involving a NaN.
For example, the statement n/0, where n is complex, returns NaN:
Use the isnan function to verify that x is NaN:
MATLAB also provides a function called NaN that returns the IEEE arithmetic representation for NaN as a double scalar value:
Logical Operations on NaN. Because two NaNs are not equal to each other, logical operations involving NaN always return false, except for a test for inequality, (NaN ~= NaN):
Infinity and NaN Functions
See Infinity and NaN Functions for a list of functions most commonly used with inf and NaN in MATLAB.
| Complex Numbers | Identifying Numeric Types | ![]() |
© 1994-2005 The MathWorks, Inc.