User Tools

Site Tools


gams:specify_a_conditional_parameter_assignment_to_avoid_a_division_by_zero_error

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
gams:specify_a_conditional_parameter_assignment_to_avoid_a_division_by_zero_error [2021/05/06 21:48]
Atharv Bhosekar removed
— (current)
Line 1: Line 1:
-===== 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: 
-<​code>​ 
- ​parameter c(i) = b(i) / a(i)  if a(i) ~= 0 
-           c(i) = 0            if a(i)  = 0 
-</​code>​ 
- 
-  
-There are some subtle points to consider about EPS and zero tolerances, but something like this should work 
-<​code>​ 
-c(i) = 0; 
-c(i)$a(i) ​      = b(i)/a(i); 
-</​code>​ 
- 
-You can do this if you don't want to zero the entire c: 
-<​code>​ 
-c(i)$[not a(i)] = 0; 
-</​code>​ 
- 
-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 
-<​code>​ 
-c(i)$[a(i) <> 0] = b(i)/a(i) 
-</​code>​ 
- 
-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.