[Django] #30363: utils.numberformat.format renders small decimals in exponential notation.

3 views
Skip to first unread message

Django

unread,
Apr 13, 2019, 5:33:37 AM4/13/19
to django-...@googlegroups.com
#30363: utils.numberformat.format renders small decimals in exponential notation.
----------------------------------------------+------------------------
Reporter: Sjoerd Job Postmus | Owner: nobody
Type: Uncategorized | Status: new
Component: Utilities | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
----------------------------------------------+------------------------
When using `utils.number_format` with `decimal_pos`, extremely small
numbers get displayed using exponential notation.

{{{
>>> from django.utils.numberformat import format as nformat
>>> nformat(Decimal('1e-199'), '.', decimal_pos=2)
'0.00'
>>> nformat(Decimal('1e-200'), '.', decimal_pos=2)
'1.00e-200'
}}}

This is caused by a hardcoded cut-off point in the internal logic, but I
would argue that when a `decimal_pos` argument is supplied and the number
to be formatted is smaller in absolute size than what can be encoded using
the provided number of decimal positions, the returned string should be
`0.0000...000` instead.

--
Ticket URL: <https://code.djangoproject.com/ticket/30363>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 13, 2019, 5:55:38 AM4/13/19
to django-...@googlegroups.com
#30363: utils.numberformat.format renders small decimals in exponential notation.
--------------------------------------+------------------------------------

Reporter: Sjoerd Job Postmus | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Utilities | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Carlton Gibson):

* type: Uncategorized => Cleanup/optimization
* stage: Unreviewed => Accepted


Comment:

Hi Sjoerd.

OK, so this is related to the changes in
9cc6a60040b0f64f8ea066dd215176d4bd16621d.

Happy to Accept as a **potential** Cleanup/optimization — I guess
ultimately it depends on what the change looks like. (i.e. is the nicer
behaviour worth the extra complexity? etc)
I'm assuming you're ready/willing to make the patch...?

Thanks!

--
Ticket URL: <https://code.djangoproject.com/ticket/30363#comment:1>

Django

unread,
Apr 13, 2019, 5:56:46 AM4/13/19
to django-...@googlegroups.com
#30363: utils.numberformat.format renders small decimals in exponential notation.
--------------------------------------+------------------------------------

Reporter: Sjoerd Job Postmus | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Utilities | Version: master

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Sjoerd Job Postmus):

Yes, definitely willing to pick it up as a patch. It should be a fairly
minimal addition.

--
Ticket URL: <https://code.djangoproject.com/ticket/30363#comment:2>

Django

unread,
Apr 13, 2019, 6:31:30 AM4/13/19
to django-...@googlegroups.com
#30363: utils.numberformat.format renders small decimals in exponential notation.
--------------------------------------+------------------------------------

Reporter: Sjoerd Job Postmus | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Utilities | Version: master

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Sjoerd Job Postmus):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/30363#comment:3>

Django

unread,
Apr 13, 2019, 8:31:29 AM4/13/19
to django-...@googlegroups.com
#30363: utils.numberformat.format renders small decimals in exponential notation.
--------------------------------------+------------------------------------

Reporter: Sjoerd Job Postmus | Owner: nobody
Type: Cleanup/optimization | Status: closed
Component: Utilities | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Florian Apolloner <apollo13@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"e6d57c4d652f16ac8f8d4600c0b7c30fcfcde6c2" e6d57c4d]:
{{{
#!CommitTicketReference repository=""
revision="e6d57c4d652f16ac8f8d4600c0b7c30fcfcde6c2"
Fixed #30363 -- Do not use exponential notation for small decimal numbers.

In 9cc6a60040b0f64f8ea066dd215176d4bd16621d a security patch was
introduced to prevent allocating large segments of memory when a
very large or very small decimal number was to be formatted.

As a side-effect, there was a change in formatting of small decimal
numbers even when the `decimal_pos` argument was provided, which meant
that reasonable small decimal numbers (above 1e-199) would be formatted
as `0.00`, while smaller decimal numbers (under 1e-200) would be
formatted as `1e-200`.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30363#comment:4>

Django

unread,
Apr 13, 2019, 9:16:59 AM4/13/19
to django-...@googlegroups.com
#30363: utils.numberformat.format renders small decimals in exponential notation.
--------------------------------------+------------------------------------

Reporter: Sjoerd Job Postmus | Owner: nobody
Type: Cleanup/optimization | Status: closed
Component: Utilities | Version: master

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Florian Apolloner <apollo13@…>):

In [changeset:"e6d57c4d652f16ac8f8d4600c0b7c30fcfcde6c2" e6d57c4d]:
{{{
#!CommitTicketReference repository=""
revision="e6d57c4d652f16ac8f8d4600c0b7c30fcfcde6c2"
Fixed #30363 -- Do not use exponential notation for small decimal numbers.

In 9cc6a60040b0f64f8ea066dd215176d4bd16621d a security patch was
introduced to prevent allocating large segments of memory when a
very large or very small decimal number was to be formatted.

As a side-effect, there was a change in formatting of small decimal
numbers even when the `decimal_pos` argument was provided, which meant
that reasonable small decimal numbers (above 1e-199) would be formatted
as `0.00`, while smaller decimal numbers (under 1e-200) would be
formatted as `1e-200`.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30363#comment:5>

Reply all
Reply to author
Forward
0 new messages