Conditional upon Bhaskara’s calculation

Asked

Viewed 655 times

1

I’m trying to add a if to show the order of the increasing numbers in Bhaskara’s formula, however it shows error message if the root is negative or there are no roots. How can I adjust?

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:
            raiz1 = (-b + math.sqrt(d))/(2 * a)
            print("A única raiz da equação é: ", raiz1)

        else:
            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 primeira raiz é: ", raiz1)
                print("A segunda raiz é: ", raiz2)

            if raiz1 > raiz2:
                    print("ordem crescente:  ", raiz1, raiz2)
  • Have you tried checking the value of d? He is float, then it is not recommended to do d == 0, because there can be errors of representation and the value is not really zero.

  • 1

    In fact, the last if uses the values of raiz1 and raiz2, 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 indentation if to leave you inside the else.

1 answer

2

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

a vezes <code>(x - r_n)</code> vezes função de grau <code>n-1</code>

Taking this recursively, we come to the conclusion that every function of degree n is a product of n first-degree equations:

função de grau <code>n</code> representada como produto de <code>n</code> funções de primeiro grau

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:
    1. the coefficients of F_n(x) are real coefficients
    2. 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)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.