Main Content

Expression Contains Inf or NaN

Optimization modeling functions do not allow you to specify complex, Inf, or NaN values. However, Inf or NaN expressions can arise during ordinary operations. Often, these expressions lead to erroneous solutions.

Optimization expressions containing Inf or NaN cannot be displayed. For example, the largest real number in double precision arithmetic is about 1.8e308. So 2e308 overflows to Inf.

x = optimvar('x');
y = 1e308;
expr = 2*x*y
expr = 


    Expression contains Inf or NaN.

Similarly, because Inf - Inf = NaN, the following expression cannot be displayed.

expr = 2*x*y - 3*x*y
expr = 


    Expression contains Inf or NaN.

If any of your optimization expressions contain Inf or NaN, try to eliminate these values before calling solve. To do so:

  • Search for these expressions by using the show or write functions.

  • Check whether the expressions came from a division by zero or from the addition or multiplication of large quantities. If so, eliminate or correct the expressions.

  • Usually, these expressions appear as the result of errors. However, sometimes they arise from poor scaling. If necessary, divide each relevant expression by a large enough scalar so that the expression no longer overflows, or use another scaling operation.

See Also


Related Topics