[Tutor] How do I do square roots and exponents?
Raymond Hettinger
python@rcn.com
Thu, 28 Mar 2002 01:52:02 -0500
I like:
def hypot(a,b):
return( a*a + b*b ) ** 0.5
or for numerical types who hate unnecessay truncation error and overflow:
def hypot(a,b):
a, b = abs(a), abs(b)
if a > b: return a * (1.0+(b/a)**2)**0.5
elif a == 0.0: return b
else: return b * (1.0+(aa/bb)**2)**0.5
Raymond
----- Original Message -----
From: "Ian!" <imcmeans@shaw.ca>
To: <tutor@python.org>
Sent: Thursday, March 28, 2002 1:25 AM
Subject: Re: [Tutor] How do I do square roots and exponents?
> Here's my attempt. I was wondering if there was a more elegant way of
doing
> it?
>
> >>> def f(alist):
> sum = 0
> for elem in alist:
> sum += elem**2
> return sum**0.5
>
> >>> f([3,4])
> 5.0
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>