#! /usr/bin/julia

# Rosetta Code, Roots of a quadratic function

function quad_roots{S<:Real,T<:Real,U<:Real}(x::S, y::T, z::U)
    (a, b, c) = promote(float(x), y, z)
    2eps(a) < abs(a) || return [-c/b]
    disc = b^2 - 4a*c
    2eps(b^2) < abs(disc) || return [-sqrt(c/a)]
    if disc < 0
        disc += 0.0im
    end
    d = sqrt(disc)
    if b < 0
        d += -b
        return [d/2a, 2c/d]
    else
        d = -b - d
        return [2c/d, d/2a]
    end
end

a = {1, 1, 1.0, 10}
b = {10, 2, -10.0^9, 1}
c = {1, 1, 1, 1}

for i in 1:length(a)
    pstr = @sprintf "%fx^2 + %fx + %f" a[i] b[i] c[i]
    println("The roots of ", pstr, " are: ")
    println("    ", join(quad_roots(a[i], b[i], c[i]), " and "), "\n")
end

        
