Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2010-05-22.10:28:31.524>labels= ['type-feature', 'library']
title='Add support for IEEE 754 contexts to decimal module.'updated_at=<Date2013-08-30.16:08:54.755>user='https://github.com/mdickinson'
Discussion migrated from bpo-8540 into its own issue.
For ease of communication with other libraries, it would be good to be able to easily create contexts corresponding to the IEEE 754 (2008) decimal interchange formats.
It's Table 3.6 ("Decimal interchange format parameters") in the final version of IEEE 754; I'm not sure what that corresponds to in the various drafts. It has column headings: "decimal32", "decimal64", "decimal128" and "decimal{k} (k >= 32)".
Parameters for decimal{k}: k must be a multiple of 32. precision is 9*k/32-2. emax is 3*2**(k/16+3). I think these formulas all work for the specific cases k in {32, 64, 128} too, so it should be easy to check that they make sense.
They give an example below the table, too:
"For example, decimal256 would have p = 70 and emax = 1572864."
Thinking ahead a bit: at some point we might well also want functions to pack and unpack these IEEE formats into byte sequences, using the bit representations described in the standard.
A natural place for those functions would be as methods on a Context object; perhaps IEEEContext should be a subclass of Context? (OTOH, the struct module is another possible place for such functionality.)
I'll think about this.
mmaker commented on Sep 18, 2012
mmakermannequin
Mannequin
More actions
Something like this? That's a pretty trivial draft for the patch.
About "byte sequences", those features should be available using builtins bin(), oct() and hex(), hacking on __index__, or with internal methods?
I am lacking imagination, what else there should be to test?
I do not think IEEEContext should not be made a class rather than a function until it really *is* a subclass with altered or added behavior. This means at least its own __str__ method and in this case, a __setattr__ that enforces the invariants. Here that means refusing to modify the context, or at least not prec and Emax (assuming that changes to any other context attributes (are there any?) are harmless. Something like
$ ./configure CFLAGS=-DEXTRA_FUNCTIONALITY -q && make -s
configure: WARNING: no system libmpdecimal found; falling back to bundled libmpdecimal (deprecated and scheduled for removal in Python 3.15)
In function ‘word_to_string’,
inlined from ‘coeff_to_string’ at ./Modules/_decimal/libmpdec/io.c:411:13,
inlined from ‘_mpd_to_string’ at ./Modules/_decimal/libmpdec/io.c:612:18:
./Modules/_decimal/libmpdec/io.c:349:40: warning: writing 1 byte into a region of size 0 [-Wstringop-overflow=]
349 | if (s == dot) *s++ = '.'; *s++ = '0' + (char)(x / d); x %= d
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
./Modules/_decimal/libmpdec/io.c:360:14: note: in expansion of macro ‘EXTRACT_DIGIT’
360 | case 15: EXTRACT_DIGIT(s, x, 100000000000000ULL, dot);
| ^~~~~~~~~~~~~
[... and similar warnings from issue #108562, hardly relevant]
Checked 112 modules (34 built-in, 77 shared, 1 n/a on linux-x86_64, 0 disabled, 0 missing, 0 failed on import)
$ ./python
Python 3.14.0a0 (heads/main:cecaceea31, Jul 19 2024, 05:34:00) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import *
>>> IEEEContext(11)
Traceback (most recent call last):
File "<python-input-1>", line 1, in <module>
IEEEContext(11)
~~~~~~~~~~~^^^^
ValueError: argument must be a multiple of 32, with a maximum of 512
>>> IEEEContext(1024)
Traceback (most recent call last):
File "<python-input-2>", line 1, in <module>
IEEEContext(1024)
~~~~~~~~~~~^^^^^^
ValueError: argument must be a multiple of 32, with a maximum of 512
>>> IEEEContext(512) # oops
Segmentation fault
Activity
mdickinson commented on May 22, 2010
Discussion migrated from bpo-8540 into its own issue.
For ease of communication with other libraries, it would be good to be able to easily create contexts corresponding to the IEEE 754 (2008) decimal interchange formats.
skrah commented on May 22, 2010
Some context from bpo-8540:
[Stefan Krah]
[Mark Dickinson]
I like that, too. Where do you find the "multiple of 32" in the standard?
In a draft of IEEE 754, I only see:
Table 2—Interchange format parameters defining floating-point numbers:
storage format: decimal32
basic format: decimal64 and decimal128
This is what Java and decNumber offer.
mdickinson commented on May 22, 2010
It's Table 3.6 ("Decimal interchange format parameters") in the final version of IEEE 754; I'm not sure what that corresponds to in the various drafts. It has column headings: "decimal32", "decimal64", "decimal128" and "decimal{k} (k >= 32)".
Parameters for decimal{k}: k must be a multiple of 32. precision is 9*k/32-2. emax is 3*2**(k/16+3). I think these formulas all work for the specific cases k in {32, 64, 128} too, so it should be easy to check that they make sense.
They give an example below the table, too:
"For example, decimal256 would have p = 70 and emax = 1572864."
skrah commented on May 22, 2010
Mark Dickinson <report@bugs.python.org> wrote:
Thanks! I think this is not in the draft I have.
+1 for IEEEContext(n). Could we have module constants Decimal32, Decimal64 and
Decimal128 so that people coming from Java or DecNumber can write:
It is somewhat redundant, but 99% of people will use only those.
mdickinson commented on May 22, 2010
Thinking ahead a bit: at some point we might well also want functions to pack and unpack these IEEE formats into byte sequences, using the bit representations described in the standard.
A natural place for those functions would be as methods on a Context object; perhaps IEEEContext should be a subclass of Context? (OTOH, the struct module is another possible place for such functionality.)
I'll think about this.
mmaker commented on Sep 18, 2012
Something like this? That's a pretty trivial draft for the patch.
About "byte sequences", those features should be available using builtins bin(), oct() and hex(), hacking on __index__, or with internal methods?
I am lacking imagination, what else there should be to test?
terryjreedy commented on Oct 14, 2012
I do not think IEEEContext should not be made a class rather than a function until it really *is* a subclass with altered or added behavior. This means at least its own __str__ method and in this case, a __setattr__ that enforces the invariants. Here that means refusing to modify the context, or at least not prec and Emax (assuming that changes to any other context attributes (are there any?) are harmless. Something like
skrah commented on Aug 30, 2013
BTW, in _decimal the feature can already be enabled with:
./configure CFLAGS=-DEXTRA_FUNCTIONALITY
Of course this isn't the official API yet, but I think it's
reasonable.
skirpichev commented on Jul 19, 2024
It seems, this doesn't work anymore:
$ ./configure CFLAGS=-DEXTRA_FUNCTIONALITY -q && make -s configure: WARNING: no system libmpdecimal found; falling back to bundled libmpdecimal (deprecated and scheduled for removal in Python 3.15) In function ‘word_to_string’, inlined from ‘coeff_to_string’ at ./Modules/_decimal/libmpdec/io.c:411:13, inlined from ‘_mpd_to_string’ at ./Modules/_decimal/libmpdec/io.c:612:18: ./Modules/_decimal/libmpdec/io.c:349:40: warning: writing 1 byte into a region of size 0 [-Wstringop-overflow=] 349 | if (s == dot) *s++ = '.'; *s++ = '0' + (char)(x / d); x %= d | ~~~~~^~~~~~~~~~~~~~~~~~~~~ ./Modules/_decimal/libmpdec/io.c:360:14: note: in expansion of macro ‘EXTRACT_DIGIT’ 360 | case 15: EXTRACT_DIGIT(s, x, 100000000000000ULL, dot); | ^~~~~~~~~~~~~ [... and similar warnings from issue #108562, hardly relevant] Checked 112 modules (34 built-in, 77 shared, 1 n/a on linux-x86_64, 0 disabled, 0 missing, 0 failed on import) $ ./python Python 3.14.0a0 (heads/main:cecaceea31, Jul 19 2024, 05:34:00) [GCC 12.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from decimal import * >>> IEEEContext(11) Traceback (most recent call last): File "<python-input-1>", line 1, in <module> IEEEContext(11) ~~~~~~~~~~~^^^^ ValueError: argument must be a multiple of 32, with a maximum of 512 >>> IEEEContext(1024) Traceback (most recent call last): File "<python-input-2>", line 1, in <module> IEEEContext(1024) ~~~~~~~~~~~^^^^^^ ValueError: argument must be a multiple of 32, with a maximum of 512 >>> IEEEContext(512) # oops Segmentation faultI'm working on this.
pythongh-53032: support IEEE 754 contexts in the decimal module
pythongh-53032: support IEEE 754 contexts in the decimal module
skirpichev commented on Jul 19, 2024
PR is ready for review: #122003
gh-53032: support IEEE 754 contexts in the decimal module (#122003)
vstinner commented on Apr 28, 2025
Implemented by the change 5bf0f36.
vstinner commented on Apr 28, 2025
It only took 15 years to add this feature :-)