Conditional after calculating Bhaskara

1

I'm trying to add a if to show the order of increasing numbers in the Bhaskara formula, though it shows an 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)
    
asked by anonymous 16.11.2017 / 18:07

1 answer

1

A function of degree n can be written as a product of three factors:

  • a constant a not null
  • a first degree function (x - r_n)
  • a function of degree n-1

Takingthisrecursively,weconcludethateveryfunctionofdegreenisaproductofnfirstdegreeequations:

Tofindtherootofthefunction,weneedtomatchittozero.Sincethenfunctionhasbeenrewrittenasaproduct(andabydefinitionisnon-zero),thismeansthatatleastoneofthefirst-degreefunctionsthatcomposethenfunctionmustbezero.Andwhatisthisvalue?Itisr_i.r_irepresentstherootofoneofthefunctions.

Take,forexample,r_n.Whenx=r_n,wehavethefollowing:

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

RegardlessofthevalueofF_n-1(r_n).

Thatsaid,someconsiderations:

  • Icanhaver_iandr_j,fori!=j,withr_i==r_j;thismeansthattherootappearedmultipletimesinthefunction,butthefunctioncontinueswithnroots
  • Icanhaverepeatedrootsyes
  • Thetruthofthedecompositionofthatdecompositionofafunctionofdegreenintoafunctionofdegreen-1appliesunderthefollowingconditions:
  • coefficientsofF_n(x)arerealcoefficients
  • theextractedrootisacomplexnumber(justrememberingthateveryrealnumberbelongstothesetofcomplexes)

Thatbeingsaid,thenIamasupporterthatyoushouldpresenttwoidenticalrootstotheequationandclassifythemasr_1andr_2.Thispreventsthe zero-comparison problem properly perceived by @AndersonCarlosWoss .

To present your roots in strictly non-decreasing order, you can take advantage of the sqrt : return the square root of the number. By definition, the square root of a nonnegative number is a positive number. With this, I can say:

x + sqrt(y) >= x - sqrt(y)

And the equality case only happens when y == 0 .

So, I would rewrite your code this way:

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)
    
17.11.2017 / 02:36