Skip to content

Support float's just as int's in decimal.Context methods #125394

Closed as not planned
@skirpichev

Description

@skirpichev

Feature or enhancement

Proposal:

Right now an exception raised:

>>> ctx = decimal.getcontext()
>>> ctx.ln(1.25)  # (1)
Traceback (most recent call last):
  File "<python-input-4>", line 1, in <module>
    ctx.ln(1.25)
    ~~~~~~^^^^^^
TypeError: conversion from float to Decimal is not supported

I believe, that above message is misleading. Actually, conversion is supported:

>>> decimal.Decimal(1.25)
Decimal('1.25')

Note also, that for integer arguments these functions work:

>>> ctx.ln(123)  # (2)
Decimal('4.812184355372417495262008610')

Current workaround is an explicit type cast, e.g.:

>>> ctx.ln(decimal.Decimal(1.25))  # (3)
Decimal('0.2231435513142097557662950903')
>>> decimal.Decimal(1.25).ln()
Decimal('0.2231435513142097557662950903')

But float's could be losslessly converted to Decimal's just as int's. So, probably (1) should behave like (3), without the need for an explicit constructor call, as for int's (2).

If this feature request does make sense, I'll work on a patch.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirstdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions