Why not FP for Money?
Bengt Richter
bokr at oz.net
Fri Sep 24 20:35:55 EDT 2004
On Fri, 24 Sep 2004 17:12:06 -0300, Carlos Ribeiro <carribeiro at gmail.com> wrote:
>On Fri, 24 Sep 2004 16:37:56 -0300, Batista, Facundo
><fbatista at unifon.com.ar> wrote:
>> [Paul Rubin]
>>
>> #- I really do think now that if we're serious about supporting
>> #- decimals,
>> #- we need decimal literals.
>>
>> What do you mean with "decimal literals"?
>
>If I got Paul right, it means the same I've been advocating: a
>notation to write floating point decimals directly without the need to
>pass strings or floats. We've proposed some alternatives:
>
>1) 'd' suffix: a = 10.123d
>2) '$ prefix: a = $10.123
>
>Both alternatives are clean and easy to read, even for novices. The
>'d' suffix is closer to what Python does already for longs (the 'L'
>suffix). The dollar sign is easily recognized, even outside US, as a
>money sign (it would be better for money/fixed point, really). The
>changes to the parser are minimal, and there is *no* chance of any of
>the proposed alternatives being of the 'that's already being used
>class' -- no keywords are needed. It's just a matter of getting BDFL's
>grace for this <wink>.
>
>For now, I'm just playing with the alternatives -- it's pretty much
>like the big decorator debate, only the timing is better :-) (just
>joking, really -- a good literal notation is *much* needed to allow
>use of decimals without binary floating point surprises happening from
>time to time).
>
Thinking a little more, it occurs to me that there are some little details though ;-)
E.g., what should the result of 12.34d + 10.005d be? 12.345d or 12.35d ?
I would vote to preserve at least as many decimals as the literal with the most in a sum
or difference, but what should the result of 0.1d * 0.1d be? 0.0d or 0.01d ?
ISTM maybe decimal _literals_ should be represented internally _exactly_, and exactness
be preserved in results of expressions involving only literals with +-*/ operations.
Then rules could be made for how to round such values when involved in expressions
with Decimal instances, where I gather[1] a "context" is used to preselect rounding
rules and number of decimals to result from any operation. Exact is easy enough for
+-*/ with (num, den, pow10) representing the exact value of (num/den)*10**pow10.
BTW what does the Decimal module do with Decimal arguments that have different
numbers of decimals?
BTW, a slight extension of the d suffix notation might be an optional count suffixed
to the d, e.g., 1.200d == 1.2d3 etc., but there's no need if literals are always exact
until they are used with Decimal instances.
[1] Sorry I haven't read the specs, I just haven't goten to it ;-/
Regards,
Bengt Richter
More information about the Python-list
mailing list