Factoring Polynomials

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Dec 18 21:11:28 EST 2008


On Thu, 18 Dec 2008 17:42:28 -0800, Collin D wrote:

> 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

So it would, but I didn't do that. Try again.

You have a typo in the above: you've written "sol1" twice and there is no 
sol2.

There is no difference between -1*b and -b (except that -b is probably a 
smidgen faster).

In the quadratic formula, the -b must be divided by 2a as well as the 
discriminant. You're only dividing the discriminant.

The *second* error has already been given away by Mark Dickson, who 
rightly points out that the quadratic equation is subject to round-off 
errors. The first error is still there :)

(Hint: think about the precedence of operators and the need for brackets.)



-- 
Steven



More information about the Python-list mailing list