Question:
Quadratic equation in python program, problem!?
2012-01-27 12:46:39 UTC
Okay, so i'm writing this program in python to solve quadratic equations, and I've found out that whenever all the variables (a, b, c) are positive, I get an error. Why is that so?

Source code:

----------------------------------------------------------------------------------------------------------------

# its in estonian but the strings of text doesn't have anything to do with the
# problem.

# ax^2 + bx + c = 0

import time
import math
import sys
import os

a = input("sisestage ruutliikme vaartus (a) ") # input a
time.sleep(1)
print "..."
b = input ("sisestage lineaarliikme vaartus (b) ") # input b
time.sleep(1)
print "..."
c = input ('sisestage vabaliikme vaartus (c) ') # input c
time.sleep(1)
x1 = (-b + (math.sqrt (b*b -4*a*c))) / (2*a)
x2 = (-b - (math.sqrt (b*b -4*a*c))) / (2*a)
print "Antud funktsiooni nullkohad" # x1, x2 = ...
time.sleep(1)
print "..."
print "x1"
print x1
print "x2"
print x2
Xh = -b/2*a
Yh = a*Xh*Xh+b*Xh+c
print "Antud funktsiooni haripunkti koordinaadid" # Xh, Yh = ...
time.sleep(1)
print "..."
print "Xh"
print Xh
print "Yh"
print Yh
def restart_program(): # restart, yes, no?
python = sys.executable
os.execl(python, python, * sys.argv)

if __name__ == "__main__":
answer = raw_input("Do you want to restart this program ? ")
if answer.strip() in "y Y yes Yes YES".split():
restart_program()

----------------------------------------------------------------------------------------------------------

Example of the error I get when all variables are positive:

(a) 2
...
(b) 4
...
(c) 5

Traceback (most recent call last):
File "C:\Users\kasutaja\Documents\Programs\Ruutfunktsioon.py", line 19, in
x1 = (-b + (math.sqrt (b*b -4*a*c))) / (2*a)
ValueError: math domain error


Any help would be greatly appreciated!
Three answers:
?
2012-01-28 04:51:52 UTC
Install the Python interpreter on your computer. This is the program that reads Python programs and carries out their instructions; you need it before you can do any Python programming.
just "JR"
2012-01-27 12:54:53 UTC
Even if all variables are positives, the radicant MAY become negative...

a=1, b=1, c=1 ==> b² - 4ac = 1 - 4 = -3

a=1, b=2, c=3 ==> 4 - 12 = -8 etc...



I have not seen any check to see if the radicant (b²-4ac) is positive. If it is negative, your math function will return a "domain error" (imaginary root). You should also check if the radicant is not 0 (two equal roots), as well as if "a" is not zero (division by zero). In this case, you would not be solving a quadratic equation, but a simple linear, but your input system ALLOWS the user to do so! NEVER ASSUME the user to do what you expect!!!! LOL.



Sorry, I am not sure we call it "radicant". maybe "radical"? I confuse languages, sometimes... :-)
mari
2016-06-20 17:43:27 UTC
If x is a root of this equation, then 1/x will probably be a root of the related operate. So (1/x)^2 + 1/x -12 = zero, or 1 + x - 12x^2 = zero or 12x^2 -x -1 = zero has the reciprocals as roots.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...