0
import math
a = float(input("Digite 'a': "))
b = float(input("Digite 'b': "))
c = float(input("Digite 'c': "))
delta = b**2 - (4*a*c)
if(delta < 0):
print("Raízes complexas!")
elif(delta == 0):
print("2 raízes reais iguais!")
else:
print("2 raízes reais diferentes!")
x1 = (-b + math.sqrt(delta))/2*a
x2 = (-b - math.sqrt(delta))/2*a
print("As raízes são:", x1, " e ", x2)
Digite 'a': 1
Digite 'b': 1
Digite 'c': 1
Raízes complexas!
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-14-51bd2b58d810> in <module>()
14 print("2 raízes reais diferentes!")
15
---> 16 x1 = (-b + math.sqrt(delta))/2*a
17 x2 = (-b - math.sqrt(delta))/2*a
18
ValueError: math domain error
But if it has complex roots why you try to calculate them as if they were real?
– anonimo
In reality the roots are: (-0.500000 - 0.866025i) and (-0.500000 + 0.866025i), where i is the square root of (-1).
– anonimo
What I’m trying to understand is what are the factors that caused the 'Valueerror', what is wrong about programming, not mathematics.
– João Couto
Without seeing the code is hard to guess (it would be nice to [Edit] the question and add it), but assuming that the delta has been calculated correctly, so it is negative and
math.sqrt
spear oneValueError
when the number is negative. In this case you should use the cmath module: https://docs.python.org/3/library/cmath.html#module-cmath - And not to be boring, but be aware that the error is related to programming and also to mathematics :-)– hkotsubo
Well, either you use the module
cmath
, that can work with complex numbers (including square root of negative numbers), or only calculate the delta root if the delta is positive, as was done for example here: https://answall.com/a/437857/112052– hkotsubo
Sorry ignorance, I am extremely 'Noob' in programming. How I use cmath?
– João Couto
The same way you used Math:
import cmath
and thencmath.sqrt
(basically, where there is "Math", change to "cmath")– hkotsubo
And where is
math.sqrt(delta)
usecmath.sqrt(delta)
– Augusto Vasques
That’s exactly what I figured, thank you very much.
– João Couto