X root Operator help

Michael Hoffman cam.ac.uk at mh391.invalid
Wed Apr 18 08:52:38 EDT 2007


lucidparadox wrote:
> I'm currently new to Python and I haven't been able to find the
> operator/math function to find the square root or even the x root of a
> number.

For square root, use math.sqrt(y)

For x root use y**(1/x)

 > I'm rewriting a program that I wrote in BASIC that does the
 > math of a quadratic equation (user puts in a, b, and c values) and
 > tells the user whether it has 1 root, 2 roots, or no real roots and
 > displays the roots if it has real roots.

In floating point arithmetic, the naive way of calculating both roots 
always using the formula (-b +/- sqrt(b**2 - 4*a*c))/2*a will give you 
inaccurate results sometimes. See 
<http://www.cse.uiuc.edu/eot/modules/floating_point/quadratic_formula/>.

For a better formula, see how r_1 and r_2 are defined in 
<http://en.wikipedia.org/wiki/Quadratic_equation#Alternative_formula>.
-- 
Michael Hoffman



More information about the Python-list mailing list