![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Oct 2007
Posts: 17
Rep Power: 0
![]() |
I have written a python code for quadratic formula
. However, I need both real and complex numbers for my results. I have done research and noticed two differences: real numbers uses import math and complex numbers uses import cmathHere's my code. I would like to know what I have done wrong. I have even put my comments on there: # quadraticcomplex.py
# A program that computes the complex roots of a quadratic equation.
# Illustrates use of the cmath library.
# The presence of complex numbers also requires mathematical functions that can perform operations on them.
# The imaginary component of a complex number is denoted by a suffix of ``J'' or ``j'';
import cmath # Makes the cmath library available.
def main(): # beginning of the code
print "Hello! This program finds the complex solutions to a quadratic"
print
a, b, c = input("Please enter the coefficients (a, b, c): ")
discRoot = cmath.sqrt(b * b - 4 * a * c) # formula solving discriminant
root1 = (-b + discRoot) / (2 * a) # formula solving root1
root2 = (-b - discRoot) / (2 * a) # formula solving root2
print
print "The solutions are:", root1, root2 # display the results
main() # end of the codeLast edited by DaWei; Oct 30th, 2007 at 10:26 PM. Reason: Added code tags. Read the rules. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
Re: Quadratic Formula in Python
Don't use cmath. Use math.
Sorry about that. Edit: Just force it to be positive before you square root it, and then indicate that it's an imaginary number, calculating the real and imaginary portion how you normally would by hand. disc = b**2 - 4*a*c # discriminant
if disc < 0:
disc = -disc # force it to be positive
else:
print "This quadratic does not have imaginary roots"
return
discRoot = math.sqrt(disc)
root1r = (-b) / (2 * a) # the real portion of root1
root1i = discRoot / (2 * a) # the imaginary portion of root1
root2r = root1r # the real portions are the same for each root
root2i = -root1i # the only difference is it's negativeSo it's basically done how someone solving it with a pencil would do it. That's usually an ideal way of thinking about how to program a solution. By the way, wrap your code in [code][/code] tags next time. Last edited by Sane; Oct 30th, 2007 at 10:09 PM. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2007
Posts: 17
Rep Power: 0
![]() |
Re: Quadratic Formula in Python
Is this the same for real numbers too?
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
Re: Quadratic Formula in Python
[Delete]
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
Re: Quadratic Formula in Python
Sorry about that. I forgot that imaginary numbers are calculated in pieces when you use the quadratic formula. It's been too long.
My first post is now edited. Last edited by Sane; Oct 30th, 2007 at 10:13 PM. |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Oct 2007
Posts: 17
Rep Power: 0
![]() |
Re: Quadratic Formula in Python
Oh Thank you! Yes I am new to programming in python. I thought that the code was straight. But when I have noticed on import math, it would calculate the real numbers of the quadratic formula. I also thought that cmath would calculate complex numbers for quadratic formula. I thought in python, j represented complex numbers.
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: Oct 2007
Posts: 17
Rep Power: 0
![]() |
Re: Quadratic Formula in Python
So is this the correct format for the code?
def main(): # beginning of the code
print "Hello! This program finds the complex solutions to a quadratic"
print
a, b, c = input("Please enter the coefficients (a, b, c): ")
disc = b**2 - 4*a*c # discriminant
if disc < 0:
disc = -disc # force it to be positive
else:
print "This quadratic does not have imaginary roots"
return
discRoot = math.sqrt(disc)
root1r = (-b) / (2 * a) # the real portion of root1
root1i = discRoot / (2 * a) # the imaginary portion of root1
root2r = root1r # the real portions are the same for each root
root2i = -root1i # the only difference is it's negative
main() |
|
|
|
|
|
#8 |
|
Programming Guru
![]() |
Re: Quadratic Formula in Python
Yes, that should work, if you fix your indenting. And print out the output in a way the user can understand.
If you want it to be able to handle both real and imaginary roots, you'll basically need to split the program up into two parts. If the discriminant is less than 0, you use the end part of the code I gave you. Otherwise, you make it use the end part of your original code. If you understand enough about your code, and the code in my edited post, it should be enough to piece together a complete solution. And you could use the cmath library for complex numbers, but it's intended for doing calculations on known complex numbers. Such as taking the cosine of an imaginary number. It's not good for what you're trying to do here. |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Oct 2007
Posts: 17
Rep Power: 0
![]() |
Re: Quadratic Formula in Python
I am going to try it out and then let you know if it works. Hopefully it will! Let me go check! Thanks Sane. But let me go check really quick.
|
|
|
|
|
|
#10 |
|
Newbie
Join Date: Oct 2007
Posts: 17
Rep Power: 0
![]() |
Re: Quadratic Formula in Python
I tried running the program and it doesn't display my results at all. Also, when I tried performing calculations, the program would not run.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [tutorial] Python for programming beginners | coldDeath | Python | 30 | Dec 14th, 2005 11:35 AM |
| If you want to learn Python or improve your Python skills | coldDeath | Python | 2 | Nov 23rd, 2005 12:27 AM |
| Convert Python script to C++ code | clanotheduck | Python | 17 | Sep 25th, 2005 8:55 AM |
| Advanced Python Tricks | Arevos | Python | 19 | Sep 24th, 2005 7:39 AM |
| Python - A Programmers Introduction | coldDeath | Python | 17 | Aug 19th, 2005 12:41 PM |