delta = 1e-06 local function bisect (f, a, b, fa, fb) local c = (a + b) / 2 io.write (n, " c=", c, " a=", a, " b=", b, "\n") if (c == a or c == b) or math.abs (a - b) < delta then return c, b - a end n = n + 1 local fc = f (c) if fa * fc < 0 then return bisect (f, a, c, fa, fc) else return bisect (f, c, b, fc, fb) end end local function f (x) return ((x * x) * x - x) - 1 end local __v4f, __v3a, __v1b = f, 1, 2 n = 0 local __v2z, __v5e = bisect (__v4f, __v3a, __v1b, __v4f (__v3a), __v4f (__v1b)) io.write (string.format ("after %d steps, root is %.17g with error %.1e, f=%.1e\n", n, __v2z, __v5e, __v4f (__v2z)))