Set dynamic maxsteps value for the bisect method

Closes #285
Closes #339
This commit is contained in:
Sergey B Kirpichev
2026-06-07 05:13:28 +03:00
parent bce4dfb5b4
commit 429c1d5ec2
2 changed files with 17 additions and 2 deletions
+2 -2
View File
@@ -306,8 +306,8 @@ class Bisection:
if len(x0) != 2:
raise ValueError('expected interval of 2 points, got %i' % len(x0))
self.f = f
self.a = x0[0]
self.b = x0[1]
self.a, self.b = x0
self.maxsteps = 2*ctx.prec + ctx.ceil(ctx.log2(abs(self.a - self.b)))
def __iter__(self):
ctx = self.ctx
+15
View File
@@ -53,6 +53,21 @@ def test_bisection():
with pytest.raises(ValueError):
findroot(lambda x: x**2-1, (4, 2), solver='bisect') == 1
# issue 285
mp.dps = 240
sol = -mp.ceil(mp.log(abs(findroot(lambda x: mp.sign(x - 3), (1, 4),
solver='bisect', verify=False,
tol=1e-200) - 3))/mp.log(10))
assert sol.ae(200)
# issue 339
mp.dps = 15
res = mpf('0.73908513321516064')
for dps in [100, 200, 300, 1000]:
with mp.workdps(dps):
sol = findroot(lambda x: cos(x) - x, [0, 1], solver='bisect')
assert (+sol).ae(res)
def test_mnewton():
f = lambda x: polyval([1, 3, 3, 1], x)
x = findroot(f, -0.9, solver='mnewton')