Decimals and other numbers
Devin Jeanpierre
jeanpierreda at gmail.com
Sat Jan 10 04:57:20 CET 2015
On Fri, Jan 9, 2015 at 7:05 PM, Gregory Ewing
<greg.ewing at canterbury.ac.nz> wrote:
> It's far from clear what *anything* multiplied by
> itself zero times should be.
>
> A better way of thinking about what x**n for integer
> n means is this: Start with 1, and multiply it by
> x n times. The result of this is clearly 1 when n
> is 0, regardless of the value of x.
>
>> 5**4 = 5*5*5*5 = 625
>
> No:
>
> 5**4 = 1*5*5*5*5
> 5**3 = 1*5*5*5
> 5**2 = 1*5*5
> 5**1 = 1*5
> 5**0 = 1
I never liked that, it seemed too arbitrary. How about this explanation:
Assume that we know how to multiply a nonempty list numbers. so
product([a]) == a, product([a, b]) = a * b, and so on.
def product(nums):
if len(nums) == 0:
return ???
return reduce(operator.mul, nums)
It should be the case that given a list of factors A and B,
product(A + B) == product(A) * product(B) (associativity).
We should let this rule apply even if A or B is the empty list,
otherwise our rules are kind of stupid.
Therefore, product([] + X) == product([]) * product(X)
But since [] + X == X, product([] + X) == product(X)
There's only one number like that: product([]) == 1
(Of course if you choose not to have the full associativity rule for
empty products, then anything is possible.)
-- Devin
More information about the Python-list
mailing list