ValueError: math domain error - Quadratic Equation (Python)

前端 未结 1 537
小蘑菇
小蘑菇 2021-01-20 00:59

I\'m very new to python programming and to this site. I\'m currently working on a problem and can\'t seem to understand the error.

import math
# Problem numb         


        
相关标签:
1条回答
  • 2021-01-20 01:43

    If you got an answer, it must have been a complex number (which are not included by default in Python). Look at the line math.sqrt(B5**2 - 4*A5*C5).

    This evaluates as so:

    math.sqrt(B5**2 - 4*A5*C5)
    math.sqrt(0**2 - 4*5*6.5)
    math.sqrt(0 - 130)
    math.sqrt(-130)
    

    The function math.sqrt doesn't find complex roots. You should use cmath.sqrt instead, as that does (this will require importing cmath at the start of your program).

    Using cmath, I get this result:

    Problem #5
    Root 1:  1.1401754250991378j
    Root 2:  1.1401754250991378j
    

    (where j is the square root of -1).

    0 讨论(0)
提交回复
热议问题