t | from math import fabs | t | from math import fabs |
| from decimal import Decimal | | from decimal import Decimal |
| from decimal import getcontext | | from decimal import getcontext |
| | | |
| | | |
| def f(x, eq): | | def f(x, eq): |
| return Decimal(eval(eq)) | | return Decimal(eval(eq)) |
| | | |
| | | |
| def root_finding(a, b, eps, eq): | | def root_finding(a, b, eps, eq): |
| c = Decimal(0) | | c = Decimal(0) |
| c = Decimal((a+b)/2) | | c = Decimal((a+b)/2) |
| while fabs(f(Decimal(c), eq)) > eps: | | while fabs(f(Decimal(c), eq)) > eps: |
| if sgn(f(a, eq))*sgn(f(Decimal(c), eq)) < 0: | | if sgn(f(a, eq))*sgn(f(Decimal(c), eq)) < 0: |
| b = Decimal(c) | | b = Decimal(c) |
| c = Decimal((a+c)/2) | | c = Decimal((a+c)/2) |
| | | |
| else: | | else: |
| a = Decimal(c) | | a = Decimal(c) |
| c = Decimal((b+c)/2) | | c = Decimal((b+c)/2) |
| | | |
| return c | | return c |
| | | |
| | | |
| def sgn(x): | | def sgn(x): |
| if (x > 0): | | if (x > 0): |
| return 1 | | return 1 |
| elif (x < 0): | | elif (x < 0): |
| return -1 | | return -1 |
| else: | | else: |
| return 0 | | return 0 |
| | | |
| | | |
| eq = input() | | eq = input() |
| a = Decimal(-1.5) | | a = Decimal(-1.5) |
| b = Decimal(1.5) | | b = Decimal(1.5) |
| e = int(input()) | | e = int(input()) |
| if eq == "x": | | if eq == "x": |
| str = "0." | | str = "0." |
| for i in range(e): | | for i in range(e): |
| str += "0" | | str += "0" |
| print(str) | | print(str) |
| else: | | else: |
| eps = Decimal(10**(-e)) | | eps = Decimal(10**(-e)) |
| getcontext().prec = e+20 | | getcontext().prec = e+20 |
| root = Decimal(root_finding(a, b, eps, eq)) | | root = Decimal(root_finding(a, b, eps, eq)) |
| print(round(root, e)) | | print(round(root, e)) |
| | | |