penny distribution

Batista, Facundo FBatista at uniFON.com.ar
Wed Nov 5 12:38:10 EST 2003


austin at smartobject.biz wrote:

#- A similar but more general use-case arises when you have to 
#- distribute
#- a product n ways.  E.g., when we apply a tax rate r to a total bill q
#- the total tax is r*q.  But if we have to indicate what portion of r*q
#- applies to each line item on the bill, then we require a similar sort
#- of penny-distribution.  Here is a function prototype:
#- 
#- distribute_product(multiplier, *portions)
#- 
#- This function would return a list of amounts, such that:
#- 
#- distribute_product(m, a, b, c) ~= [m*a, m*b, m*c]   _and_
#- sum(distribute_product(m, a, b, c)) = m*(a+b+c)
#- 
#- I.e., the first equality is approximate but the second is 
#- strict.  You
#- could define Alex's divideBy method as:
#- 
#- def divideBy(self, N):
#-     return distribute_product(self, *[1.0/N for i in range(N)])
#- 

But, also I can say that 

	def distribute_product(multiplier, *portions):
		return [multiplier*portion for portion in
divideBy(portions)]

So, divideBy is the general case.

The importance of this is that because of rounding, both approachs don't
have the same results (I think)!

So, which one is valid?

.	Facundo





. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
ADVERTENCIA  

La información contenida en este mensaje y cualquier archivo anexo al mismo,
son para uso exclusivo del destinatario y pueden contener información
confidencial o propietaria, cuya divulgación es sancionada por la ley. 

Si Ud. No es uno de los destinatarios consignados o la persona responsable
de hacer llegar este mensaje a los destinatarios consignados, no está
autorizado a divulgar, copiar, distribuir o retener información (o parte de
ella) contenida en este mensaje. Por favor notifíquenos respondiendo al
remitente, borre el mensaje original y borre las copias (impresas o grabadas
en cualquier medio magnético) que pueda haber realizado del mismo. 

Todas las opiniones contenidas en este mail son propias del autor del
mensaje y no necesariamente coinciden con las de Telefónica Comunicaciones
Personales S.A. o alguna empresa asociada. 

Los mensajes electrónicos pueden ser alterados, motivo por el cual
Telefónica Comunicaciones Personales S.A. no aceptará ninguna obligación
cualquiera sea el resultante de este mensaje. 

Muchas Gracias.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20031105/3ed1715b/attachment.html>


More information about the Python-list mailing list