fractions and decimal: Compare Equal, Can't Add (tested)
Fraction and Decimal are both exact numeric types, so it's natural to assume they interoperate. We measured comparing and sorting mixed Fraction/Decimal values against doing arithmetic on them, and found a sharp line: comparisons work, arithmetic doesn't.
1. Equality, ordering, and sorted() all work across Fraction and Decimal
from fractions import Fraction
from decimal import Decimal
print(Fraction(1, 2) == Decimal("0.5"))
print(Fraction(1, 2) < Decimal("0.75"))
print(sorted([Fraction(1, 2), Decimal("0.3"), Fraction(1, 4)]))
True
True
[Fraction(1, 4), Decimal('0.3'), Fraction(1, 2)]
Measured: Fraction(1, 2) == Decimal("0.5") is True, ordering comparisons work, and sorted() happily merges a list containing both types into correct numeric order.
2. Adding or subtracting the same two values raises TypeError
from fractions import Fraction
from decimal import Decimal
try:
Fraction(1, 2) + Decimal("0.5")
except TypeError as e:
print("TypeError:", e)
try:
Decimal("0.5") + Fraction(1, 2)
except TypeError as e:
print("TypeError:", e)
TypeError: unsupported operand type(s) for +: 'Fraction' and 'decimal.Decimal'
TypeError: unsupported operand type(s) for +: 'decimal.Decimal' and 'Fraction'
Measured: the exact same two values that compared equal in the first example raise TypeError on +, in both directions -- Fraction.__add__ doesn't know how to combine with Decimal, and Decimal.__radd__ doesn't either, so Python has no fallback.
3. Subtraction fails the same way; it isn't specific to addition
from fractions import Fraction
from decimal import Decimal
try:
Fraction(1, 2) - Decimal("0.5")
except TypeError as e:
print("TypeError:", e)
TypeError: unsupported operand type(s) for -: 'Fraction' and 'decimal.Decimal'
Measured: every arithmetic operator we tried between Fraction and Decimal raises TypeError, while every comparison operator and sorted() succeeds. This is the comparison/arithmetic asymmetry other numeric-tower pairs in Python show too (plain Decimal-vs-float has the opposite split: arithmetic there raises but comparisons are silently allowed) -- here it's consistent in one direction, but still easy to miss. Code that stores measured values as Decimal in one module and computed ratios as Fraction in another can pass every comparison-based test (equality checks, sorting, min/max) and then crash the first time the two get added -- for example a reconciliation step that sums line items from both sources. Convert explicitly before combining: Fraction(Decimal("0.5")) + Fraction(1, 2) works, since Fraction's own constructor accepts a Decimal even though its arithmetic operators don't.
Frequently asked questions
Can you add a Fraction and a Decimal directly in Python?
No -- we measured Fraction(1, 2) + Decimal("0.5") raising TypeError: unsupported operand type(s) for +, and the reflected form Decimal("0.5") + Fraction(1, 2) raises the same error. Convert one to the other's type first, e.g. Fraction(1, 2) + Fraction(Decimal("0.5")).
Why does Fraction(1, 2) == Decimal("0.5") work but Fraction(1, 2) + Decimal("0.5") doesn't?
Comparison operators and sorted() have a fallback path that can compare across numeric types by value, which we confirmed works correctly here. Arithmetic operators require one type's __add__/__radd__ to explicitly know how to combine with the other, and neither Fraction nor Decimal implements that for the other type, so Python raises TypeError.
Can you sort a list containing both Fraction and Decimal values?
Yes -- we measured sorted([Fraction(1, 2), Decimal("0.3"), Fraction(1, 4)]) returning the values in correct numeric order. Sorting relies on comparisons, which work across the two types even though arithmetic doesn't.
How do I combine a Fraction and a Decimal value in an expression?
Convert the Decimal to a Fraction explicitly first -- Fraction's constructor accepts a Decimal argument even though its arithmetic operators reject one directly. We confirmed Fraction(1, 2) + Fraction(Decimal("0.5")) works, while Fraction(1, 2) + Decimal("0.5") raises TypeError.
Written with AI assistance. Measured on 2026-10-04 with Python 3.13.5's built-in fractions and decimal modules: compared a Fraction and a Decimal with ==, <, and sorted(), then attempted +, -, and the reflected forms between the same two values. PlainHeaders is our extension; Python's fractions and decimal modules are built in.