0

Confusing error

So I wrote this code: https://code.sololearn.com/cfLCvHb65hJ2 It solves the quadratic formula, but for some reason I get "math domain error". Can someone help me? I would like to know what it means or how to fix it. :)

10th Apr 2020, 8:09 PM
Sherlyn A.
Sherlyn A. - avatar
2 Answers
+ 4
The issue you have is as you described a math domain error. This is because (depending on the input numbers) the sqrt function is getting a negativ number, which is not valid: Lets say a = 10, b = 20, c = 30, you get the following expression to use sqrt: x2 = (-20-sqrt(400 - 1200)) / 20 #=>>> this causes the error If you use other values like a = 1, b = 30, c = 5 it will work. So you have to make sure in your code, that only valid expressions are passed to the sprt function.
11th Apr 2020, 11:38 AM
Lothar
Lothar - avatar
+ 1
That wouldn't work. I wrote this on the Python IDLE, where that would not matter. It gives me the error nevertheless. :( EDIT: Thanks Lothar!!!!!!
11th Apr 2020, 12:54 AM
Sherlyn A.
Sherlyn A. - avatar