Mailing List Archive

Re: Fixed Decimal types
Andy Robinson wrote:
For the work I do, simply taking the precision of the
most precise input (4.00L)would do the trick, but your
answer (4.0000L) is purer. We should provide a
rounding function, and in practice anyone using such a
function would round (or floor, or ceiling) to get to
the desired precision immediately.

I'm not sure on division either but I'm sure there are
precedents to look at.

The AS400 provides a useful example of the right way to do scaled
decimals.

In the RPG programming language, all internal calculations (i.e.
multiplication, division) are performed to the maximum precision of the
intermediate result (in the multiplication example below), the intermediate
result would be 4.0000L. When the intermediate result is assigned to the
target scaled decimal number, the decimal precision is automatically
extended or truncated to fit the target precision. One extra wrinkle in all
of this is the option to "half-adjust" the intermediate value on assignment;
that is to apply automatic 5/4 rounding to the precision of the target.

So, if the target field is defined as numeric(4,2), the result will
be 4.00L.

These are probably the kind of semantics that a scaled decimal type
would require in Python also; i.e. allow unlimited precision in intermediate
calculations, with a sensible set of rules for assignment to a variable of
different scale and precision.

However, unlike RPG, we should probably ensure that attempts to
overflow or underflow the scale result in NaN or Overflow conditions, rather
than assuming the user is right and losing the significant digits.

Regards,
Mike da Silva
Re: Fixed Decimal types [ In reply to ]
"Da Silva, Mike" wrote:
>
> Andy Robinson wrote:
> For the work I do, simply taking the precision of the
> most precise input (4.00L)would do the trick, but your
> answer (4.0000L) is purer. We should provide a
> rounding function, and in practice anyone using such a
> function would round (or floor, or ceiling) to get to
> the desired precision immediately.
>
> I'm not sure on division either but I'm sure there are
> precedents to look at.
>
> The AS400 provides a useful example of the right way to do scaled
> decimals.
>
> In the RPG programming language, all internal calculations (i.e.
> multiplication, division) are performed to the maximum precision of the
> intermediate result (in the multiplication example below), the intermediate
> result would be 4.0000L. When the intermediate result is assigned to the
> target scaled decimal number, the decimal precision is automatically
> extended or truncated to fit the target precision. One extra wrinkle in all
> of this is the option to "half-adjust" the intermediate value on assignment;
> that is to apply automatic 5/4 rounding to the precision of the target.

Yee ha! This is great input. Anyone have any other examples of what
any other systems do? Anyone got a PL/I manual handy. ;)

> So, if the target field is defined as numeric(4,2), the result will
> be 4.00L.

Since Python doesn't have types values, this is not an issue
internally, but would be an issue when binding to external databases.

> These are probably the kind of semantics that a scaled decimal type
> would require in Python also; i.e. allow unlimited precision in intermediate
> calculations, with a sensible set of rules for assignment to a variable of
> different scale and precision.
>
> However, unlike RPG, we should probably ensure that attempts to
> overflow or underflow the scale result in NaN or Overflow conditions, rather
> than assuming the user is right and losing the significant digits.

Since this would be based on infinite-precision numbers, I don't
think that this would be an issue.

Jim

--
Jim Fulton mailto:jim@digicool.com Python Powered!
Technical Director (888) 344-4332 http://www.python.org
Digital Creations http://www.digicool.com http://www.zope.org

Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email
address may not be added to any commercial mail list with out my
permission. Violation of my privacy with advertising or SPAM will
result in a suit for a MINIMUM of $500 damages/incident, $1500 for
repeats.
Re: Fixed Decimal types [ In reply to ]
Jim Fulton wrote:
> "Da Silva, Mike" wrote:

[AS400 RPG rules...]

> Yee ha! This is great input. Anyone have any other examples of
> what any other systems do? Anyone got a PL/I manual handy. ;)

From memory of IBM COBOL and SQL, the rules for
intermediates seem similar to what Mike outlines. In both
cases, the target is pre-specified, and I think by default you
get auto-rounding.

Tim's BCD class seem to always return the higher precision
on an arithmetic op, although the intermediate is full precision.

>> However, unlike RPG, we should probably ensure
>> that attempts to overflow or underflow the scale
>> result in NaN or Overflow conditions, rather
>> than assuming the user is right and losing
>> the significant digits.

> Since this would be based on infinite-precision numbers, I don't
> think that this would be an issue.

It's an issue if the result of an arithmetic op is other than "full"
precision. The issue certainly comes up when you e.g. talk to
a DB, and it might be better to have it come up sooner rather
than later.

- Gordon
Re: Fixed Decimal types [ In reply to ]
> >> However, unlike RPG, we should probably ensure
> >> that attempts to overflow or underflow the scale
> >> result in NaN or Overflow conditions, rather
> >> than assuming the user is right and losing
> >> the significant digits.
>
> > Since this would be based on infinite-precision
> numbers, I don't
> > think that this would be an issue.


Three very general observations before I disappear for
Christmas:

(1) I think there is great mileage in combining the
fixed-decimal concept with Martin Fowler's Quantity
pattern, so that a variable could be defined as not
just two decimal places but also (say) "GBP" or "USD",
and it would be an error to add the two. Same applies
for adding metres, kilograms and other quantities.
There has also been discussion that the 'type' of a
quantity should determine what math should apply.

(2) If Python is going to be used increasingly in
eCommerce, it should be good at dealing with money -
maybe not in the core language, but we should aim for
one standard package.

(3) We have a python-finance list
(python-finance@egroups.com), recently generalized to
cover business systems, which is a good place to
discuss this if anyone wants to. There are people
there who have time, would love to prototype something
(indeed some work started in this area 3 months back),
and would use it at work too. This would be an ideal
first target for that group - or indeed for a
finance-sig. I'll pursue this in the New Year.

Merry Christmas,

Andy

=====
Andy Robinson
Robinson Analytics Ltd.
------------------
My opinions are the official policy of Robinson Analytics Ltd.
They just vary from day to day.

_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
Re: Fixed Decimal types [ In reply to ]
> (1) I think there is great mileage in combining the
> fixed-decimal concept with Martin Fowler's Quantity
> pattern, so that a variable could be defined as not
> just two decimal places but also (say) "GBP" or "USD",
> and it would be an error to add the two. Same applies
> for adding metres, kilograms and other quantities.
> There has also been discussion that the 'type' of a
> quantity should determine what math should apply.

Isn't this something that is ideally suited for implementation in a Python
module, based on a core implementation of fixed decimal numbers?
--
Jack Jansen | ++++ stop the execution of Mumia Abu-Jamal ++++
Jack.Jansen@oratrix.com | ++++ if you agree copy these lines to your sig ++++
www.oratrix.nl/~jack | see http://www.xs4all.nl/~tank/spg-l/sigaction.htm
Re: Fixed Decimal types [ In reply to ]
On Fri, 24 Dec 1999, Jack Jansen wrote:
> > (1) I think there is great mileage in combining the
> > fixed-decimal concept with Martin Fowler's Quantity
> > pattern, so that a variable could be defined as not
> > just two decimal places but also (say) "GBP" or "USD",
> > and it would be an error to add the two. Same applies
> > for adding metres, kilograms and other quantities.
> > There has also been discussion that the 'type' of a
> > quantity should determine what math should apply.
>
> Isn't this something that is ideally suited for implementation in a Python
> module, based on a core implementation of fixed decimal numbers?

I'd agree with Jack here.

The "simple" change of a scale for the Long values is nice. Starting to
lump in features like this begins to get a little messier...

Happy Holidays,
-g

--
Greg Stein, http://www.lyra.org/
Re: Fixed Decimal types [ In reply to ]
On Fri, 24 Dec 1999 12:05:22 -0800 (PST), you wrote:

>On Fri, 24 Dec 1999, Jack Jansen wrote:
>> > (1) I think there is great mileage in combining the
>> > fixed-decimal concept with Martin Fowler's Quantity
>> > pattern, so that a variable could be defined as not
>> > just two decimal places but also (say) "GBP" or "USD",
>> > and it would be an error to add the two. Same applies
>> > for adding metres, kilograms and other quantities.
>> > There has also been discussion that the 'type' of a
>> > quantity should determine what math should apply.
>>
>> Isn't this something that is ideally suited for implementation in a Python
>> module, based on a core implementation of fixed decimal numbers?
>
>I'd agree with Jack here.
>
Me too - I thought I said that in point 2, but in retrospect I didn't
say it clearly enough :-)


- Andy