ArbitPrec/eBadalyan
ArbitPrec/Stephan
f1from math import fabsf1from math import fabs
2from decimal import Decimal2from decimal import Decimal
3from decimal import getcontext3from decimal import getcontext
44
55
6def f(x, eq):6def f(x, eq):
7    return Decimal(eval(eq))7    return Decimal(eval(eq))
88
99
n10def FindRoot(a, b, eps, eq):n10def root_finding(a, b, eps, eq):
11    c = Decimal(0)11    c = Decimal(0)
n12    c = Decimal((a + b) / 2)n12    c = Decimal((a+b)/2)
13    while fabs(f(Decimal(c), eq)) > eps:13    while fabs(f(Decimal(c), eq)) > eps:
n14        if sgn(f(a, eq)) * sgn(f(Decimal(c), eq)) < 0:n14        if sgn(f(a, eq))*sgn(f(Decimal(c), eq)) < 0:
15            b = Decimal(c)15            b = Decimal(c)
n16            c = Decimal((a + c) / 2)n16            c = Decimal((a+c)/2)
1717
18        else:18        else:
19            a = Decimal(c)19            a = Decimal(c)
20            c = Decimal((b+c)/2)20            c = Decimal((b+c)/2)
nn21 
21    return c22    return c
2223
2324
24def sgn(x):25def sgn(x):
25    if (x > 0):26    if (x > 0):
26        return 127        return 1
27    elif (x < 0):28    elif (x < 0):
28        return -129        return -1
29    else:30    else:
30        return 031        return 0
3132
3233
33eq = input()34eq = input()
34a = Decimal(-1.5)35a = Decimal(-1.5)
35b = Decimal(1.5)36b = Decimal(1.5)
36e = int(input())37e = int(input())
37if eq == "x":38if eq == "x":
38    str = "0."39    str = "0."
39    for i in range(e):40    for i in range(e):
40        str += "0"41        str += "0"
41    print(str)42    print(str)
42else:43else:
t43    eps = Decimal(10 ** (-e))t44    eps = Decimal(10**(-e))
44    getcontext().prec = e + 2045    getcontext().prec = e+20
45    root = Decimal(FindRoot(a, b, eps, eq))46    root = Decimal(root_finding(a, b, eps, eq))
46    print(round(root, e))47    print(round(root, e))
4748
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op