-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Add support for IEEE 754 contexts to decimal module. #53032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
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. |
Some context from bpo-8540: [Stefan Krah]
[Mark Dickinson]
I like that, too. Where do you find the "multiple of 32" in the standard? Table 2—Interchange format parameters defining floating-point numbers: storage format: decimal32 basic format: decimal64 and decimal128 This is what Java and decNumber offer. |
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." |
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 c = IEEEContext(Decimal64) It is somewhat redundant, but 99% of people will use only those. |
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. |
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 def __setattr__(self, key, value):
if key in ('prec', 'Emax'):
raise AttributeError("cannot change {} of IEEEContext".format(key))
else:
super(IEEEContext, self).__setattr__(key, name) |
BTW, in _decimal the feature can already be enabled with: ./configure CFLAGS=-DEXTRA_FUNCTIONALITY >>> IEEEContext(DECIMAL128)
Context(prec=34, rounding=ROUND_HALF_EVEN, Emin=-6143, Emax=6144, capitals=1, clamp=1, flags=[], traps=[])
>>> IEEEContext(DECIMAL64)
Context(prec=16, rounding=ROUND_HALF_EVEN, Emin=-383, Emax=384, capitals=1, clamp=1, flags=[], traps=[])
>>> IEEEContext(DECIMAL32)
Context(prec=7, rounding=ROUND_HALF_EVEN, Emin=-95, Emax=96, capitals=1, clamp=1, flags=[], traps=[])
>>>
>>> IEEEContext(512)
Context(prec=142, rounding=ROUND_HALF_EVEN, Emin=-103079215103, Emax=103079215104, capitals=1, clamp=1, flags=[], traps=[])
>>> IEEEContext(1024)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: argument must be a multiple of 32, with a maximum of 512
>>> Of course this isn't the official API yet, but I think it's |
It seems, this doesn't work anymore:
I'm working on this. |
This was in C version from beginning, but available only on conditional compilation (EXTRA_FUNCTIONALITY). Current patch adds function to create IEEE contexts to the pure-python module as well.
This was in C version from beginning, but available only on conditional compilation (EXTRA_FUNCTIONALITY). Current patch adds function to create IEEE contexts to the pure-python module as well.
PR is ready for review: #122003 |
This was in C version from beginning, but available only on conditional compilation (EXTRA_FUNCTIONALITY). Current patch adds function to create IEEE contexts to the pure-python module as well. Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Implemented by the change 5bf0f36. |
It only took 15 years to add this feature :-) |
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:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: