User Tools

Site Tools


How do I specify a conditional parameter assignment to avoid a 'division by zero' error?

Q: Given parameters are a(i), b(i). I want to specify:

 parameter c(i) = b(i) / a(i)  if a(i) ~= 0
           c(i) = 0            if a(i)  = 0

There are some subtle points to consider about EPS and zero tolerances, but something like this should work

c(i) = 0;
c(i)$a(i)       = b(i)/a(i);

You can do this if you don't want to zero the entire c:

c(i)$[not a(i)] = 0;

You might find it cleaner to zero the entire c first. Depending on how c is declared, this might be faster too, but it's not likely to be very slow in any case. If you do

c(i)$[a(i) <> 0] = b(i)/a(i)

this will be like the statement above except where a(i) has the value EPS. EPS is a value that is stored (so $a(i) is true) but it has the value 0, so you might get division by zero without the explicit check for a(i) <> 0.

IMPRESSUM / LEGAL NOTICEPRIVACY POLICY gams/specify_a_conditional_parameter_assignment_to_avoid_a_division_by_zero_error.txt · Last modified: 2014/05/22 11:00 by support