Should DecimalField have special rounding to make float assignments match string assignments?

146 views
Skip to first unread message

Tim Graham

unread,
Apr 5, 2016, 9:49:02 AM4/5/16
to Django developers (Contributions to Django itself)
If you assign a float with more decimal places than a DecimalField field supports to that field, the value may round differently than if you assign the same value as a string. For example:

class Invoice(models.Model):
    gross = models.DecimalField(max_digits=10, decimal_places=1)

invoice.gross = 2.15    # saves as 2.1
invoice.gross = '2.15'  # saves as 2.2

The default behavior of decimal rounding is ROUND_HALF_EVEN (to nearest with ties going to nearest even integer). There's a proposal to change this to cast floats to string and then use ROUND_HALF_UP to match the value of strings [0][1]. Do you have any concerns about this? Is it something we should even care about?

[0] https://github.com/django/django/pull/6410
[1] https://code.djangoproject.com/ticket/26459

Javier Guerra Giraldez

unread,
Apr 5, 2016, 10:04:16 AM4/5/16
to django-d...@googlegroups.com
On 5 April 2016 at 14:49, Tim Graham <timog...@gmail.com> wrote:
> The default behavior of decimal rounding is ROUND_HALF_EVEN (to nearest with
> ties going to nearest even integer). There's a proposal to change this to
> cast floats to string and then use ROUND_HALF_UP to match the value of
> strings [0][1]. Do you have any concerns about this? Is it something we
> should even care about?


Am I reading it wrong, or the ticket/patch assume that the "correct"
rounding is ROUND_HALF_UP? In my experience, ROUND_HALF_EVEN produces
less accumulated error after a string of operations.


--
Javier

Aymeric Augustin

unread,
Apr 5, 2016, 10:17:33 AM4/5/16
to django-d...@googlegroups.com
I have three ideas about this.

1) In my opinion, assigning a float to a decimal field is a programming error. Sadly Python doesn’t raise an exception in that case.

>>> decimal.Decimal(2.15)
Decimal('2.149999999999999911182158029987476766109466552734375')

So Django has to support it as well.

2) Python 3 provides the `create_decimal_from_float(<float>)` class method on `decimal.Context` for this purpose. I think that’s what Django should use.

>>> decimal.Context(prec=10).create_decimal_from_float(2.15)
Decimal('2.150000000')


(I don’t care much about Python 2 at this point. We should just make sure we don’t introduce a debatable behavior in the last LTS release supporting Python 2.)

3) Currently DecimalField accepts max_digits and decimal_places options. I think it should accept a decimal context and delegate all operations to that context.

I suggest the following behavior:

- start with the decimal context provided in a kwarg to DecimalField or, if there is None, the current context returned by `getcontext()`
- modify that context to take into account max_digits and decimal_places
- ask the context to perform whatever operations we need


I’m -1 on emulating that with arbitrary string conversions and rounding rules and also -1 on deviating from Python’s default behavior, which matches the IEEE 754 specification, without providing a way to customize that behavior properly.

I hope this helps !

-- 
Aymeric.

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/3749bbaf-ded7-4de3-912b-3a9e654b0207%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aymeric Augustin

unread,
Apr 5, 2016, 10:30:29 AM4/5/16
to django-d...@googlegroups.com
Small corrections:

About 1) — actually decimal.Decimal(<float>) was an error Python 3.0 and 3.1.

About 2) — I mixed up two options for the factory to build decimals from floats:

- the decimal.Decimal.from_float class method — which I assume uses the global context
- the decimal.Context..create_decimal_from_float method — which relies on a specific context


I assume the latter will be more convenient for low level control.

-- 
Aymeric.

Daniel Moisset

unread,
Apr 5, 2016, 10:39:35 AM4/5/16
to django-d...@googlegroups.com
I'm not sure when it changed, but decimal.Decimal(some_float) was also an error in 2.4 and 2.6 at least. I'm seeing that it works in 2.7, 3.3, 3.4, 3.5, so it's probably a recent decision, looking up the rationale for that could be relevant


For more options, visit https://groups.google.com/d/optout.



--
Daniel F. Moisset - Technical Leader
Skype: @dmoisset
Reply all
Reply to author
Forward
0 new messages