@@ -176,7 +176,10 @@ def calculate_variable_from_constraint(
176
176
intercept = (residual_1 - upper ) - slope * x1
177
177
if slope :
178
178
variable .set_value (- intercept / slope , skip_validation = True )
179
- body_val = value (body , exception = False )
179
+ try :
180
+ body_val = value (body , exception = False )
181
+ except OverflowError :
182
+ body_val = None
180
183
if body_val .__class__ not in _invalid_types and abs (body_val - upper ) < eps :
181
184
# Re-set the variable value to trigger any warnings WRT
182
185
# the final variable state
@@ -277,18 +280,25 @@ def calculate_variable_from_constraint(
277
280
while alpha > alpha_min :
278
281
# check if the value at xkp1 has sufficient reduction in
279
282
# the residual
280
- fkp1 = value (expr , exception = False )
281
- # HACK for Python3 support, pending resolution of #879
282
- # Issue #879 also pertains to other checks for "complex"
283
- # in this method.
284
- if fkp1 .__class__ in _invalid_types :
285
- # We cannot perform computations on complex numbers
286
- fkp1 = None
287
- if fkp1 is not None and fkp1 ** 2 < c1 * fk ** 2 :
288
- # found an alpha value with sufficient reduction
289
- # continue to the next step
290
- fk = fkp1
291
- break
283
+ try :
284
+ fkp1 = value (expr , exception = False )
285
+ # HACK for Python3 support, pending resolution of #879
286
+ # Issue #879 also pertains to other checks for "complex"
287
+ # in this method.
288
+ if fkp1 .__class__ in _invalid_types :
289
+ # We cannot perform computations on complex numbers
290
+ fkp1 = None
291
+ if fkp1 is not None and fkp1 ** 2 < c1 * fk ** 2 :
292
+ # found an alpha value with sufficient reduction
293
+ # continue to the next step
294
+ fk = fkp1
295
+ break
296
+ except OverflowError :
297
+ # Encountered an overflow, either from evaluating
298
+ # this point in the line search (to get fkp1) or
299
+ # from squaring fkp1. (The example from #3540
300
+ # actually triggers both). Reject this alpha value.
301
+ pass
292
302
alpha /= 2.0
293
303
xkp1 = xk + alpha * pk
294
304
variable .set_value (xkp1 , skip_validation = True )
0 commit comments