prePEP: Decimal data type
Batista, Facundo
FBatista at uniFON.com.ar
Tue Nov 4 12:40:37 EST 2003
Paul Moore wrote:
#- >> When passing floating point to the constructor, what
#- should happen?
#- >>
#- >> j. ``Decimal(1.1) == Decimal('1.1')``
#- >> k. ``Decimal(1.1) ==
#- >> Decimal('110000000000000008881784197001252...e-51')``
#- >
#- > Clearly, j is the correct answer. It's not all that hard
#- to do, either.
#-
#- No way. Consider:
#-
#- >>> 1.1
#- 1.1000000000000001
#- >>> 1.1==1.1000000000000001
#- True
#-
#- So what should Decimal(1.1000000000000001) evaluate to? It can't be
#- Decimal('1.1'), as that contradicts your statement that j "clearly"
#- applies. But it *also* can't be
#- Decimal('1.1000000000000001'), as then
#- we have the *same number* converting to two *different* Decimal
#- values.
This is really the problem. Option (j) seems to be correct but is not.
Option (k) *is* correct, but not what you want to do, :(
Maybe the more natural solution is not to accept float (you always can write
that *same* number as a string, and it'll be correct, and sure i'll be doing
what you expect).
#- into :-) Having a special method, say Decimal.round_float(f, digits),
#- is probably OK, though...
Mmmm. Digits in the first meaning or the second? Should round or truncate?
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
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/20031104/b5465dab/attachment.html>
More information about the Python-list
mailing list