Skip to content

Can create decimal.Decimal objects that exponent exceed Emax or Emin limit #142032

@1nftf

Description

@1nftf

Bug report

Bug description:

from decimal import Decimal, getcontext
EMIN = getcontext().Emin
EMAX = getcontext().Emax

exprs = (
    "Decimal(f'1E+{EMAX}')",
    "Decimal(f'1E+{EMAX+10}')",
    # "Decimal(f'{10 ** (EMAX+10)}')",
    "Decimal(f'1E+{EMAX+10}') / 10",
    "Decimal(f'1E{EMIN}')",
    "Decimal(f'1E{EMIN-10}')",
    "Decimal(f'1E{EMIN-10}') * 10",
)

for expr in exprs:
    print(expr)
    try:
        print(eval(expr))
    except Exception as e:
        print(e)
    print()

Output:

Decimal(f'1E+{EMAX}')
1E+999999

Decimal(f'1E+{EMAX+10}')
1E+1000009

Decimal(f'1E+{EMAX+10}') / 10
[<class 'decimal.Overflow'>]

Decimal(f'1E{EMIN}')
1E-999999

Decimal(f'1E{EMIN-10}')
1E-1000009

Decimal(f'1E{EMIN-10}') * 10
1.0E-1000008

CPython versions tested on:

3.15, 3.14, 3.13, 3.12, 3.11, 3.10, 3.9

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirinvalidtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions