ArbitPrec/Stephan
ArbitPrec/yervandsargsyan
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 n17            # print(Decimal(c))
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            # print(Decimal(c))
22    return c
2123
tt24    #print(fabs(f(Decimal(c), eq)))
25#    if Decimal(fabs(f(Decimal(c), eq)))<eps:
22    return c26#        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)
2331
2432
25def sgn(x):33def sgn(x):
26    if (x > 0):34    if (x > 0):
27        return 135        return 1
28    elif (x < 0):36    elif (x < 0):
29        return -137        return -1
30    else:38    else:
31        return 039        return 0
3240
3341
34eq = input()42eq = input()
35a = Decimal(-1.5)43a = Decimal(-1.5)
36b = Decimal(1.5)44b = Decimal(1.5)
37e = int(input())45e = int(input())
38if eq == "x":46if eq == "x":
39    str = "0."47    str = "0."
40    for i in range(e):48    for i in range(e):
41        str += "0"49        str += "0"
42    print(str)50    print(str)
43else:51else:
44    eps = Decimal(10**(-e))52    eps = Decimal(10**(-e))
45    getcontext().prec = e+2053    getcontext().prec = e+20
46    root = Decimal(root_finding(a, b, eps, eq))54    root = Decimal(root_finding(a, b, eps, eq))
47    print(round(root, e))55    print(round(root, e))
4856
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op