How about adding rational fraction to Python?
Wed Feb 27 05:55:14 CET 2008
Mark Dickinson <dickinsm at gmail.com> writes:
> def mean(number_list):
> return sum(number_list)/len(number_list)
> If you pass a list of floats, complex numbers, Fractions, or Decimal
> instances to mean() then it'll work just fine. But if you pass
> a list of ints or longs, it'll silently return the wrong result.
So use: return sum(number_list) / float(len(number_list))
That makes it somewhat more explicit what you want. Otherwise
I wouldn't be so sure the integer result is "wrong".
More information about the Python-list