A pretty dumb newbie question

Pete Shinners pete at shinners.org
Mon Dec 3 21:33:55 EST 2001


N64fan2017 wrote:

> How would you calculate square root? I've tried
> x**1/2

couple things to point out. first. in your expression, the "power of" 
operator is happening before the "divide" operator. therefore your 
expression is working like this, "(x**1)/2". adding parenthesis to the 
statement "x**(1/2)" is not going to help you in this situation, why?

dividing two integers gives you an integer result.. therefore
1/2 == 0. if you want to calculate the square root like this, you'll 
want to use floating values, "x**(1.0/2.0)". this will give you the 
correct results you are looking for.

note, you can also call the squareroot function from the python math module:
	import math
	math.sqrt(x)

good luck with python!




More information about the Python-list mailing list