When testing or checking a function, a message appears stating that the line
root2 = (-b + sqrt (discriminant)) / (2 * a)
passes math domain error. What’s the catch? Thanks in advance
Answer 1, authority 100%
For real numbers, the root of a negative number does not exist:
& gt; & gt; & gt; import math
& gt; & gt; & gt; math.sqrt (-1)
Traceback (most recent call last):
File "& lt; stdin & gt;", line 1, in & lt; module & gt;
ValueError: math domain error
math domain error
For complex numbers, the root is:
& gt; & gt; & gt; import cmath
& gt; & gt; & gt; cmath.sqrt (-1)
1j
Answer 2
You are taking the square root of a negative number.
Here is the result of running your code on the site (I also pasted print (‘discriminant:’, discriminant))
https://repl.it/FKpS/1