ArbitPrec/yervandsargsyan
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
10def root_finding(a, b, eps, eq):10def root_finding(a, b, eps, eq):
11    c = Decimal(0)11    c = Decimal(0)
12    c = Decimal((a+b)/2)12    c = Decimal((a+b)/2)
13    while fabs(f(Decimal(c), eq)) > eps:13    while fabs(f(Decimal(c), eq)) > eps:
14        if sgn(f(a, eq))*sgn(f(Decimal(c), eq)) < 0:14        if sgn(f(a, eq))*sgn(f(Decimal(c), eq)) < 0:
15            b = Decimal(c)15            b = Decimal(c)
16            c = Decimal((a+c)/2)16            c = Decimal((a+c)/2)
n17            # print(Decimal(c))n17 
18        else:18        else:
19            a = Decimal(c)19            a = Decimal(c)
20            c = Decimal((b+c)/2)20            c = Decimal((b+c)/2)
n21            # print(Decimal(c))n21 
22    return c22    return c
t23 t
24    #print(fabs(f(Decimal(c), eq)))
25#    if Decimal(fabs(f(Decimal(c), eq)))<eps:
26#        return c
27#    elif sgn(f(a,eq))*sgn(f(c,eq))<0:
28#        return root_finding(a,c,eps)
29#    else:
30#        return root_finding(c,b,eps)
3123
3224
33def sgn(x):25def sgn(x):
34    if (x > 0):26    if (x > 0):
35        return 127        return 1
36    elif (x < 0):28    elif (x < 0):
37        return -129        return -1
38    else:30    else:
39        return 031        return 0
4032
4133
42eq = input()34eq = input()
43a = Decimal(-1.5)35a = Decimal(-1.5)
44b = Decimal(1.5)36b = Decimal(1.5)
45e = int(input())37e = int(input())
46if eq == "x":38if eq == "x":
47    str = "0."39    str = "0."
48    for i in range(e):40    for i in range(e):
49        str += "0"41        str += "0"
50    print(str)42    print(str)
51else:43else:
52    eps = Decimal(10**(-e))44    eps = Decimal(10**(-e))
53    getcontext().prec = e+2045    getcontext().prec = e+20
54    root = Decimal(root_finding(a, b, eps, eq))46    root = Decimal(root_finding(a, b, eps, eq))
55    print(round(root, e))47    print(round(root, e))
5648
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op