[Python-Dev] Decimal data type issues (original) (raw)

Ka-Ping Yee python-dev at zesty.ca
Thu Apr 15 05:49:17 EDT 2004


On Wed, 14 Apr 2004, Batista, Facundo wrote:

Decimal(string) works the way we want/need/desire. Decimal.fromstring(string) works the way the documentation specify.

I think we can only change the way the former works or the name of the latter.

Or you can add an optional argument, which is what I suggested. Aren't you going to need an optional argument anyway to let the user specify a context?

> I also think his last suggestion could provide a nice, convenient > solution to the conversion-from-float issue, since it raises the > same question (a method named "Decimal.fromfloat" doesn't explain > how or why it is different from just constructing with a float).

You can NOT construct directly from a float. That's the difference.

I understand that you want to make it hard for people to construct a Decimal from a float. But what you just stated does not explain anything.

I assume the purpose of from_float is to prevent people from being confused about whether Decimal(1.1) produces exactly 1.1 or the machine value of the double-precision rounded float closest to 1.1. Is that correct?

If so, then the real exceptional case is exact conversion, not float conversion. Decimal(1.1, precision=4) is unambiguous. Therefore:

Decimal(value, default_context=True)
Decimal(value, context=c)
Decimal(value, precision=p)

should all work for integer, float, string, or Decimal values.

To protect against the ambiguity of Decimal(1.1),

Decimal(value)

should work for integer, string, or Decimal values, and raise an exception for a float, such as "precision must be specified for conversion from float to Decimal".

The extra method should be specifically for the exceptional case, and named appropriately.

Decimal.exact_float(value)

It would serve just one purpose: allow exact conversion from floats when it's really desired.

This would both (a) separate the functionality between Decimal() and Decimal.exact_float() in a more logical way; and (b) give the extra method a more meaningful, useful name -- a name that really explains why the extra method exists.

Also, remember that the context doesn't affect the creation when you create directly from string (only if you use fromstring). Your example means to change the behaviour of the creation when creating directly from string? And when create directly from int or long?

That's what I'm suggesting: that you can supply a context object, or ask for the default context, when constructing any Decimal value.

-- ?!ng



More information about the Python-Dev mailing list