On Dec 18, 5:10 pm, Steven D'Aprano <st...@remove-this-
cybersource.com.au> wrote:
> On Thu, 18 Dec 2008 11:37:35 -0800, collin.day.0 wrote:
> > I am trying to write a simple application to factor polynomials. I wrote
> > (simple) raw_input lines to collect the a, b, and c values from the
> > user, but I dont know how to implement the quadratic equation
>
> > x = (-b +or- (b^2 - 4ac)^1/2) / 2a
>
> > into python. Any ideas?
>
> def quadratic_solution(a, b, c):
>     sol1 = (-b + (b**2 - 4*a*c)**0.5)/2*a
>     sol2 = (-b - (b**2 - 4*a*c)**0.5)/2*a
>     return (sol1, sol2)
>
> Because this looks like homework, I've deliberately left in two errors in
> the above. One of them is duplicated in the two lines above the return,
> and you must fix it or you'll get radically wrong answers.
>
> The second is more subtle, and quite frankly if this is homework you
> could probably leave it in and probably not even lose marks. You will
> need to do significant research into numerical methods to learn what it
> is, but you will then get significantly more accurate results.
>
> --
> Steven

The corrected function is:
def quadratic_solution(a,b,c)
    sol1 = -1*b + ((b**2 - 4*a*c)**.5)/2*a
    sol1 = -1*b - ((b**2 - 4*a*c)**.5)/2*a
    return (sol1, sol2)

Squaring the -b would give you some strange solutions.... :D

-CD
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to