Home  >  Article  >  Backend Development  >  Use Python to write a function that finds the solution to a quadratic equation in real numbers

Use Python to write a function that finds the solution to a quadratic equation in real numbers

高洛峰
高洛峰Original
2017-03-27 15:09:322106browse

def quar(a,b,c):
    if not isinstance(a,(int,float))|isinstance(b,(int,float))|isinstance(c,(int,float)):
        raise TypeError('Wrong Type inputing!')
    else:
        from math import sqrt
        tmp1=b**2-4*a*c
        if tmp1>0:
             return '%.04f'%float((-b+sqrt(tmp1))/(2*a)),'%.04f'%((-b-sqrt(tmp1))/(2*a))
        elif tmp1==0:
            return -b/(2*a)
        else:
            return 'No rational answer!'

使用Python写一个求实数内二次方程解的函数

使用Python写一个求实数内二次方程解的函数

Listen

The above is the detailed content of Use Python to write a function that finds the solution to a quadratic equation in real numbers. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn