Aug-27-2020, 11:15 AM
Hi,
I'm learning Python (3.7) and I have sometimes strange results, for example when using code to find the real roots of a single univariate quadratic function:
Isn't it a bit weird? Is there an easy/clean/pythonic way to prevent/correct that kind of outputs?
I'm learning Python (3.7) and I have sometimes strange results, for example when using code to find the real roots of a single univariate quadratic function:
a= -200 b = 600 c = 0 discr = b**2 - 4 * a * c if discr < 0: print("This equation has no real root") elif discr == 0: print("This equation has one real root:\n" f"\t{-b / (2 * a)}") else: print("This equation has two real roots:\n" f"\t{(-b + discr**0.5) / (2 * a)}\t and\n" f"\t{(-b - discr**0.5) / (2 * a)}")returns
Output:This equation has two real roots:
-0.0 and
3.0
From what I could try, results are similar in other cases where 0 is a solution and a is negative (it's like "0" takes its sign from "a"), if "a" is positive then it returns "0.0".Isn't it a bit weird? Is there an easy/clean/pythonic way to prevent/correct that kind of outputs?