By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,663 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,663 IT Pros & Developers. It's quick & easy.

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

P: 3
hello :

This is my code :

------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. while True :
  2.     try:
  3.         ' enter the coefficients of the equation :'
  4.  
  5.         a=input('a=' )
  6.         b=input('b=' )
  7.         c=input('c=' )
  8.  
  9.         r=b**2-4*a*c
  10.         import cmath
  11.         sq=cmath.sqrt
  12.  
  13.         if r==0 :
  14.             x=(-b)/(2*a)
  15.             y=(-b)/(2*a)
  16.             print 'the roots are :',x,',',y
  17.  
  18.         elif r>0 :
  19.             x=((-b)+sq(r))/(2*a)
  20.             y=((-b)-sq(r))/(2*a)
  21.             print 'the roots are :',x,',',y
  22.  
  23.         if r<-1 :
  24.             i=sq(-1)
  25.             x=(-b+(sq(r*(i**2))))/(2*a)
  26.             y=(-b-(sq(r*(i**2))))/(2*a)
  27.             print 'the roots are :',x,',',y
  28.     except :
  29.         print 'wrong inputs , please enter numbers only'
------------------

when r<-1
################################################## ###
### the results are not like the casio calculater ###
################################################## ###
Nov 27 '12 #1

✓ answered by MR 010

I found it myself :)

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

Share this Question
Share on Google+
2 Replies

P: 3
I found it myself :)

i should use abs(r) :) , thanks for giving me a chance to ask you :)
Nov 27 '12 #2

bvdet
Expert Mod 2.5K+
P: 2,851
MR 010 - Kudos to you for finding the issue. Thanks for the update.
Nov 30 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.