calculating the roots of ax**2+bx+c

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MR 010
    New Member
    • Nov 2012
    • 3

    calculating the roots of ax**2+bx+c

    hello :

    This is my code :

    ------------------------------------------
    Code:
    while True :
        try:
            ' enter the coefficients of the equation :'
    
            a=input('a=' )
            b=input('b=' )
            c=input('c=' )
    
            r=b**2-4*a*c
            import cmath
            sq=cmath.sqrt
    
            if r==0 :
                x=(-b)/(2*a)
                y=(-b)/(2*a)
                print 'the roots are :',x,',',y
    
            elif r>0 :
                x=((-b)+sq(r))/(2*a)
                y=((-b)-sq(r))/(2*a)
                print 'the roots are :',x,',',y
    
            if r<-1 :
                i=sq(-1)
                x=(-b+(sq(r*(i**2))))/(2*a)
                y=(-b-(sq(r*(i**2))))/(2*a)
                print 'the roots are :',x,',',y
        except :
            print 'wrong inputs , please enter numbers only'
    ------------------

    when r<-1
    ############### ############### ############### ########
    ### the results are not like the casio calculater ###
    ############### ############### ############### ########
    Last edited by bvdet; Nov 27 '12, 06:46 PM. Reason: Please use code tags when posting code [code].......[/code]
  • MR 010
    New Member
    • Nov 2012
    • 3

    #2
    I found it myself :)

    i should use abs(r) :) , thanks for giving me a chance to ask you :)

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      MR 010 - Kudos to you for finding the issue. Thanks for the update.

      Comment

      Working...