[Python-Dev] Adventures with Decimal

2005-05-22 Thread Mike Cowlishaw
mal("1.1001");

ends up with a having an object with the value you see in the
string, and for it to be otherwise one would have to write:

  BigDecimal a = new BigDecimal("1.1001", context);

which gives a very nice clue that something may happen to the
value.  This, to me, seems a clean design.


So why does my specification appear to say something different?
---
Both the languages described so far support arbitrary-length
decimal numbers.  Over the past five years, however, I have been
concentrating more on fixed-length decimals, as in languages such
as C# and as will be in C and C++ and in hardware.

When the representation of a decimal number has a fixed length,
then the nice clean model of a one-to-one mapping of a literal to
the internal representation is no longer always possible.  For
example, the IEEE 754r proposed decimal32 format can represent a
maximum of 7 decimal digits in the significand.  Hence, the
assignment:

  decimal32 d = 1.1001;

(in some hypothetical C-like language) cannot result in d having
the value shown in the literal.  This is the point where language
history or precedent comes in: some languages might quietly round
at this point, others might give a compile-time warning or error
(my preference, at least for decimal types).  Similar concerns
apply when the conversion to internal form causes overflow or
underflow.

The wording in the specification was intended to allow for these
kinds of behaviors, and to allow for explicit rounding, using a
context, when a string is converted to some internal
representation.  It was not intended to restrict the behavior of
(for example) the Java constructor: one might consider that
constructor to be working with an implied context which has an
infinite precision.  In other words, the specification does not
attempt to define where the context comes from, as this would seem
to be language-dependent.  In Java, any use of a programmer-
supplied context is explicit and visible, and if none is supplied
then the implied context has UNLIMITED precision.  In Rexx, the
context is always implicit -- but there is no 'conversion from
string to number' because numbers _are_ strings.


So what should Python do?
-
Since your Decimal class has the ability to preserve the value of
a literal exactly, my recommendation is that that should be the
default behavior.  Changing the value supplied as a literal
without some explicit syntax is likely to surprise, given the
knowledge that there are no length restrictions in the class.

My view is that only in the case of a fixed-length destination
or an explicit context might such a rounding be appropriate.

Given that Python has the concept of an implicit decimal context,
I can see why Tim can argue that the implicit context is the
context which applies for a constructor.  However, perhaps you can
define that the implicit context 'only applies to arithmetic
operations', or some such definition, much as in Rexx?

And I should clarify my specification to make it clear that
preserving the value, if possible, is preferable to rounding --
any suggestions for wording?


Mike Cowlishaw

[I'll try and follow this thread in the mailing list for a while,
but I am flying to the USA on Monday so my e-mail access will be
erratic for the next week or so.]
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Adventures with Decimal

2005-06-01 Thread Mike Cowlishaw
> Raymond Hettinger wrote:
> > IMO, user input (or
> > the full numeric strings in a text data file) is sacred and presumably
> > done for a reason -- the explicitly requested digits should not be
> > throw-away without good reason.
> 
> I still don't understand what's so special about the
> input phase that it should be treated sacredly, while
> happily desecrating the result of any *other* operation.

The 'difference' here is, with unlimited precision decimal 
representations, there is no "input phase".  The decimal number can 
represent the value, sign, and exponent in the character string the user 
provided _exactly_, and indeed it could be implemented using strings as 
the internal representation -- in which case the 'construction' of a new 
number is simply a string copy operation.

There is no operation taking place as there is no narrowing necessary. 
This is quite unlike (for example) converting an ASCII string "1.01" to a 
binary floating-point double which has a fixed precision and no base-5 
component.

mfc
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com