Re: Why not FP for Money?

From: Bengt Richter (bokr_at_oz.net)
Date: 09/25/04


Date: 25 Sep 2004 00:35:55 GMT

On Fri, 24 Sep 2004 17:12:06 -0300, Carlos Ribeiro <carribeiro@gmail.com> wrote:

>On Fri, 24 Sep 2004 16:37:56 -0300, Batista, Facundo
><fbatista@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



Relevant Pages

  • Re: Precision
    ... There is nothing wrong with the D3 floating point algorithm or anyone else's ... It's the same if we use Precision, floating point, or ... positions it is IMPOSIBLE to have a result of 4 decimals with the 2 ... remember what the associated actuarial calculations were. ...
    (comp.databases.pick)
  • Re: Precision
    ... There is nothing wrong with the D3 floating point algorithm or anyone else's ... It's the same if we use Precision, floating point, or ... positions it is IMPOSIBLE to have a result of 4 decimals with the 2 ... remember what the associated actuarial calculations were. ...
    (comp.databases.pick)
  • RE: Packed decimal arithmetic in C
    ... >many decimals to ... >> I once used double precision floating point in exactly that manner. ... >applications that track currency and need to do so exactly. ... >A scaled integer meets the requirement of exactness. ...
    (comp.os.vms)
  • Re: Precision
    ... Whether one does the subsequent calculations in internal ie integer format ... I have always used external format with Precision 4 since my ... floating point calcs which are done in binary have around 3% error albeit at ... positions it is IMPOSIBLE to have a result of 4 decimals with the 2 ...
    (comp.databases.pick)
  • Re: Packed decimal arithmetic in C
    ... With floating point, you can't specify how ... >> many decimals to consider during arithmetic, ... applications that track currency and need to do so exactly. ... A scaled integer consists of two values: ...
    (comp.os.vms)