-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Closed as not planned
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtype-featureA feature request or enhancementA feature request or enhancement
Description
Bug report
Bug description:
Mixing Decimals and floats will often get you the wrong answer. By default, the Decimal library allows such behavior:
>>> import decimal
>>> decimal.Decimal(0.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')
>>> decimal.Decimal("0.1") < 0.1
True
>>> decimal.Decimal("0.1") == 0.1
False
Fortunately, you can turn on floating point strict mode, where mixing Decimals and floats is prohibited:
>>> decimal.getcontext().traps[decimal.FloatOperation] = True
>>> decimal.Decimal(0.1)
Traceback (most recent call last):
File "<console>", line 1, in <module>
decimal.FloatOperation: [<class 'decimal.FloatOperation'>]
>>> decimal.Decimal("0.1") < 0.1
Traceback (most recent call last):
File "<console>", line 1, in <module>
decimal.FloatOperation: [<class 'decimal.FloatOperation'>]
HOWEVER, for some reason, ==
and !=
are allowed in floating point strict mode, and produce wrong answers:
>>> decimal.Decimal("0.1") == 0.1
False
>>>
When floating point strict mode is on, why is ==
and !=
still allowed?
if isinstance(other, float):
context = getcontext()
if equality_op:
context.flags[FloatOperation] = 1
else:
context._raise_error(FloatOperation,
"strict semantics for mixing floats and Decimals are enabled")
return self, Decimal.from_float(other)
This code would be better without the if equality_op:
:
if isinstance(other, float):
context = getcontext()
context._raise_error(FloatOperation,
"strict semantics for mixing floats and Decimals are enabled")
return self, Decimal.from_float(other)
Why are ==
and !=
allowed in floating point strict mode?
CPython versions tested on:
3.12
Operating systems tested on:
Linux
Metadata
Metadata
Assignees
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtype-featureA feature request or enhancementA feature request or enhancement