[Python-Dev] Re: Decimal data type issues
Batista, Facundo
FBatista at uniFON.com.ar
Mon Apr 19 09:39:20 EDT 2004
[Jewett, Jim J]
#- Facundo Batista:
#- > ... what is "exact conversion" to you:
#-
#- > 1.1 -> "1.1"
#- > 1.1 -> "1.1000000000000001"
#- > 1.1 -> "1.100000000000000088817841970012523233890533447265625"
#-
#- machine precision of floats, whatever that happens to be.
That is not very portable...
#- That implementation-dependent part is one reason to use a
#- longer name, like exact_float.
#-
#- But if I say
#-
#- Decimal(1.1, precision=4)
#- Decimal(1.1, positions=3)
#-
#- Those are unambiguous. If the context is 43, then later
#- calculations will effectively fill in the extra digits;
#- I have just said to fill them in with zeros, rather than
#- whatever the float approximations were.
And why you want to fill them with zeros? I think this is a presentation
issue and doesn't got nothing to do about how to build the number internals.
I want my number to get showed as "1.100", but that is on Money, not on
Decimal.
#- Sure they are; the reason to specify positions is that the
#- underlying data wasn't really floating point -- it was an
#- integer which by convention is written in a larger unit.
#- Example with money:
#-
#- Assume meat is 0.987USD/pound. There are three
#- price digits to multiply by the weight, but the
#- final cost is rounded to an integer number of
#- pennies.
#-
#- 10 pounds cost 9.87, but
#- 1 pound costs 0.99
#-
#- I don't want to say that my prices have three digits of
#- precision, or I'll keep fractions of a penny. I don't
#- want to say that they have only two, or I'll drop the
#- pennies on expensive items. I want to say that the
#- precision varies depending on the actual price.
I think that you should have all the math with three decimal places, and
then round to two when you have to charge your costumer (how you say "get
the money and put it inside the cash register" in english?).
But, this is an issue of Money, not Decimal.
#- But for any *specific* value, specifying either the total
#- number of valid digits (signficant figures, precision)
#- or the number of fractional digits (position) is enough
#- to determine both.
According to your words, sticking with the specification will do your
purpose. Why should we add another parameter to the ecuation? Decimal is
*now* enough complex.
#- > "number" can be string, int, etc., but NOT float.
#- > The issue with rounding at a defined position has
#- > nothing to do with context.
#-
#- I assume that a new Decimal would normally be created
#- with as much precision as the context would need for
#- calculations. By passing a context/precision/position,
#- the user is saying "yeah, but this measurement wasn't
#- that precise in the first place. Use zeros for the
#- rest, no matter what this number claims."
I don't get to understand you, and I'm not sure if you have the right
concept. Several examples may help:
>>> getcontext().prec = 5
>>> Decimal(124)
Decimal( (0, (1, 2, 4), 0) )
>>> +Decimal(124)
Decimal( (0, (1, 2, 4), 0) )
>>> Decimal('258547.368')
Decimal( (0, (2, 5, 8, 5, 4, 7, 3, 6, 8), -3) )
>>> +Decimal('258547.368')
Decimal( (0, (2, 5, 8, 5, 5), 1L) )
>>> Decimal.from_float(1.1)
Decimal( (0, (1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 1,
7, 8, 4, 1, 9, 7, 0, 0, 1, 2, 5, 2, 3, 2, 3, 3, 8, 9, 0, 5, 3, 3, 4, 4, 7,
2, 6, 5, 6, 2, 5), -51L) )
>>> +Decimal.from_float(1.1)
Decimal( (0, (1, 1, 0, 0, 0), -4L) )
. Facundo
More information about the Python-Dev
mailing list