This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Possibly use ROUND_05UP in decimal's localcontext() example
Type: Stage: resolved
Components: Documentation Versions: Python 3.11, Python 3.10
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: mark.dickinson, rhettinger, tim.peters
Priority: normal Keywords:

Created on 2021-11-28 18:05 by rhettinger, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg407215 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-11-28 18:05
Candidate example:

    with localcontext() as ctx:
        ctx.prec += 10        # Perform a higher precision calculation
        ctx.rounding = ROUND_05UP # Avoid double rounding of the final calculation step
        s = calculate_something()
    s = +s  # Round the final result back to the default precision

Thoughts:

* This would highlight the intended purpose of ROUND_05UP.
* Usually, it would slightly improve accuracy.
* OTOH, it would rarely make a difference in practice.

https://docs.python.org/3/library/decimal.html#decimal.localcontext
msg407219 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-11-28 19:05
I’m concerned though that the intermediate calculations might be worse off than with some other rounding mode.
msg407223 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2021-11-28 19:29
Not a good idea in general - this rounding mode is _mostly_ "to zero", and isn't intended for chains of operations. I don't believe I've ever seen it used in a real program, so the current "no example at all" is a fair representation of real usage ;-)
msg407228 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2021-11-28 19:56
I'll add that the rounding mode is intended to ease emulating fixed-point arithmetic. The decimal spec claimed that was a goal, but there really isn't any direct support for saying, e.g., "I want two digits after the decimal point". Only for specifying total precision, independent of the radix point's position.

So, e.g., if you want to work with tax rates, etc, but keeping results to penny precision,

1. Set the rounding mode to ROUND_05UP with "plenty of" precision digits.

2. To add, say, a statutory 3.578% tax rate to an item with cost C:

       C *= decimal.Decimal("1.03578")
       C = C.quantize(decimal.Decimal(".01"), decimal.ROUND_HALF_UP)

or whatever final rounding mode local statutes require.

I"m not sure anyone other than Mike Cowlishaw realizes that, though ;-)
History
Date User Action Args
2022-04-11 14:59:52adminsetgithub: 90076
2021-11-28 19:56:33tim.peterssetmessages: + msg407228
2021-11-28 19:31:34rhettingersetstatus: open -> closed
resolution: rejected
stage: resolved
2021-11-28 19:29:16tim.peterssetmessages: + msg407223
2021-11-28 19:05:02rhettingersetmessages: + msg407219
2021-11-28 18:05:23rhettingercreate