A grade function n
can be written as a product of three factors:
- a constant
a
nonzero
- a first-degree function
(x - r_n)
- a grade function
n-1
Taking this recursively, we come to the conclusion that every function of degree n
is a product of n
first-degree equations:
To find the function root, we need to equal it to zero. As the degree function n
was rewritten as a product (and a
by definition is non-zero), this means that at least one of the first-degree functions that make up the function of the n
-nth degree needs to be zero. And what is that value? It is r_i
. r_i
represents the root of one of the functions.
Take, for example, r_n
. When x = r_n
, we have the following:
F_n(x) = a * (x - r_n) * F_n-1(x)
F_n(r_n) = a * (r_n - r_n) * F_n-1(r_n) = a * (0) * F_n-1(r_n) ==>
F_n(r_n) = 0
Independent of the value of F_n-1(r_n)
.
That said, some considerations:
- I may have
r_i
and r_j
, for i != j
, with r_i == r_j
; this means that the root appeared multiple times in the function, but the function continues with n
roots
- i can have repeated roots yes
- the truth of the decomposition of that decomposition of a degree function
n
for a grade function n-1
applies under the following conditions:
- the coefficients of
F_n(x)
are real coefficients
- the extracted root is a complex number (just remembering that every real number belongs to the set of complexes)
That said, so I’m a proponent that you should present two identical roots for the equation and classify them as r_1
and r_2
. This avoids the problem of comparison by zero properly perceived by @Andersoncarloswoss.
To present your roots in strictly non-degressing order, you can take advantage of the definition of sqrt
: return the square root of the number. By definition, the square root of a non-negative number is a positive number. With this, I can affirm:
x + sqrt(y) >= x - sqrt(y)
And the equality case only happens when y == 0
.
So I’d rewrite your code like this:
import math
def delta(a,b,c):
return b**2 - 4*a*c
def main():
a = float(input("digite o valor de a: "))
b = float(input("digite o valor de b: "))
c = float(input("digite o valor de c: "))
imprime_raizes(a,b,c)
def imprime_raizes(a, b, c):
d = delta(a, b, c)
if d < 0:
print("A equação não possui raízes reais")
else:
raiz1 = (-b + math.sqrt(d))/(2 * a)
raiz2 = (-b - math.sqrt(d))/(2 * a)
print("A maior raiz é: ", raiz1)
print("A menor raiz é: ", raiz2)
Have you tried checking the value of
d
? He isfloat
, then it is not recommended to dod == 0
, because there can be errors of representation and the value is not really zero.– Woss
In fact, the last
if
uses the values ofraiz1
andraiz2
, but if the equation has no roots, these variables will never be defined, which can cause the mentioned error. I believe this one lacked an indentationif
to leave you inside theelse
.– Woss