Math domain error


#1

am facing math domain errror in ths code to solve quadratic equation.

To solve quadratic equation

a=float(input("Please enter the value of a:"))
b=float(input("Please enter the value for b:"))
c=float(input("Please enter the value for c:"))

Calculation

import math
value=(math.sqrt(b**2-(4*a*c)))/2*a
print("The solution is:",value)


#2

My answer didn't help?


#3

it helped.Got the message just now.


#4

i am facing math domain errror in ths code to solve quadratic equation.

#To solve quadratic equation
a=float(input("Please enter the value of a:"))
b=float(input("Please enter the value for b:"))
c=float(input("Please enter the value for c:"))
#Calculation
import math
value=(math.sqrt(b**2-(4*a*c)))/2*a
print("The solution is:",value)

#5

Is this exercise related? If so, please include the exercise url and error message


#6

No this topic is not related.


#7

I am practicing a few scripts by scripting codes.actualy i get the output.in that output it asks for the values of a,b and c.but after that when i hit enter,error shows up.


#8

math.sqrt() can't handle a negative value. So if you make b high (For example 9) and a and c low (1 and 2 for example), it works

its been a while since i used this, but looking at this image from wikipedia there are two possible outcomes:

its either + or -, you only do minus, maybe you should also attempt plus?

i think you need to break it down into pieces, first calculate b**2 and 4*a*c, then determine which is bigger to see if you need to do b**2-(4*a*c) or b**2+(4*a*c)


#9

Thank you! i will do it.


#10

if sqrt() gets a negative value, it produces a value error

python has try/except which allows us to catch errors:

try:
    # put the code you want to try here
except ValueError:
   # what to do if we get a value error

#11

can square root be given as value**0.5?


#12

sure, why would that not possible?


#13

Is this script technically correct??

To solve quadratic equation

a=float(input("Please enter the value of a:"))
b=float(input("Please enter the value for b:"))
c=float(input("Please enter the value for c:"))

Calculation

import math
value1=(b**2)
value2=(4*a*c)
value3=value1-value2
value4=value3**0.5
value5=-b+value4 or -b-value4
value6=value5/(2*a)
print(value6)
input("Please press enter to exit")


#14

Its running.But not sure whether its correct.


#15

The code is displaying the output for (-b+(sqroot((b^2)-4ac)))/2a and not for (-b-(sqroot((b^2)-4ac)))/2a..I took the values as a:2,b=3 &c=1..I solved it manually and got the answer.


#16

well, you would have to calculate it by hand. Now that you use ** instead of sqrt() you can simply calculate both:

result = []
result.append((-b+(b**2-4*a*c)**0.5)/(2*a))
result.append((-b-(b**2-4*a*c)**0.5)/(2*a))
print result

don't forgot that this formula has 2 possible outcomes


#17

So I can use this.So this gives two outcomes?


#18

its been ages since i used this, but yes. But i thought only one outcome was valid? You need to calculate both and then see which is right, but its too long ago i used this

but this code calculate both


#19

Why is append used in the script?


#20

because i decided as such? I wanted to have the result in a list, you could also make two separate variables