Jump to content

Averages/Arithmetic mean: Difference between revisions

→‎{{header|Python}}: Works with 3.0
(→‎{{header|Python}}: fsum tracks multiple partial sums)
(→‎{{header|Python}}: Works with 3.0)
Line 473:
 
=={{header|Python}}==
{{works with|Python|3.0}}.<br>{{works with|Python|2.6}}<br>
Uses fsum which tracks multiple partial sums to avoid losing precision
<lang python>from math import fsum
def average(x):
return fsum(x)/float(len(x)) if x else 0
print (average([3,1,4,1,5,9]))
print (average([1e20,3,1,4,1,5,9,-1e20]))</lang>
 
Output:
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.