Discuss / Python / code

JustEXPer

#1 Created at ... [Delete] [Delete and Lock User]
# -*- coding: utf-8 -*-
import math
def quadratic(a,b,c):
    derta = b**2 - 4*a*c
    x1 = (-b+math.sqrt(derta))/(2*a)
    x2 = (-b-math.sqrt(derta))/(a*2)
    return x1,x2
print(quadratic(2,3,1))
print(quadratic(1,3,-4))

廖雪峰

#2 Created at ... [Delete] [Delete and Lock User]

相同的math.sqrt(derta)计算了两次

JustEXPer

#3 Created at ... [Delete] [Delete and Lock User]
# -*- coding: utf-8 -*-
import math
def quadratic(a,b,c):
    d = math.sqrt(b**2 - 4*a*c)
    x1 = (-b+d)/(2*a)
    x2 = (-b-d)/(a*2)
    return x1,x2
print(quadratic(2,3,1))
print(quadratic(1,3,-4))

  • 1

Reply