root_formula Subroutine

public pure subroutine root_formula(a, b, c, x1, x2)

一元二次方程求根公式

Arguments

Type IntentOptional Attributes Name
real(kind=sk_real_kind), intent(in) :: a
real(kind=sk_real_kind), intent(in) :: b
real(kind=sk_real_kind), intent(in) :: c
real(kind=sk_real_kind), intent(out), optional :: x1
real(kind=sk_real_kind), intent(out), optional :: x2

Contents

Source Code


Source Code

    pure subroutine root_formula(a, b, c, x1, x2)
        real(kind=sk_real_kind), intent(in) :: a, b, c
        real(kind=sk_real_kind), intent(out), optional :: x1, x2
        if (b*b - 4*a*c < 0) return
        if (a > 0) then
            if (present(x1)) x1 = (-b - sqrt(b*b - 4*a*c))/(2*a)
            if (present(x2)) x2 = (-b + sqrt(b*b - 4*a*c))/(2*a)
        else
            if (present(x1)) x1 = (-b + sqrt(b*b - 4*a*c))/(2*a)
            if (present(x2)) x2 = (-b - sqrt(b*b - 4*a*c))/(2*a)
        end if
    end subroutine root_formula