On 07.04.2022 02:41, Greg Ewing wrote:
On 6/04/22 8:58 pm, Mark Dickinson via Python-ideas wrote:
I'd be curious to know what alternatives you see. When a user writes `x + y` with both `x` and `y` instances of `decimal.Decimal`, the decimal module needs to know what precision to compute the result to (as well as what rounding mode to use, etc.). Absent a thread-local context or task-local context, where would that precision information come from?
I'm not sure, but my feeling is that if I want to limit results to a specific number of digits, I'm going to want much finer grained control, like specifying it for each individual operation. The current design doesn't fit any use case I can see myself needing.
GMP uses a smarter approach (https://gmplib.org/manual/Floating_002dpoint-Functions): - GMP floats are mutable objects - all floats have a variable precision and you can even adjust the precision after creation - operations put the result into an existing float object (with defined precision) Of course, this is not what a Python user would expect (numbers in Python are usually immutable), so using the approach directly would break "Python" intuition and likely cause many weird errors down the line. However, in practice, you rarely need decimals with more than 64 bits (or some other fixed upper limit) precision, so the global context works just fine and you can always adjust the precision for output purposes at the I/O boundaries of your application. The MPFR library, which uses a similar strategy for numbers as GMP, adds more flexibility by also providing a rounding context (https://www.mpfr.org/#intro). MPFR provides a global default rounding mode and also allows a per operation rounding mode. Again, applications will typically just use one rounding method for consistency purposes, so a global context works well in practice. Certain algorithms may require special handling of both precision and rounding, but for those, you can either use a thread-local or task-local context which you only enable while the algorithm is running. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Experts (#1, Apr 07 2022)
Python Projects, Coaching and Support ... https://www.egenix.com/ Python Product Development ... https://consulting.egenix.com/
::: We implement business ideas - efficiently in both time and costs ::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 https://www.egenix.com/company/contact/ https://www.malemburg.com/