A python Decimal
stores a base 10 number, which can be thought of as a ratio with a power of 10 on the bottom.
So why does this fail?
from decimal import Decimal
from numbers import Rational
assert issubclass(Decimal, Rational)
A python Decimal
stores a base 10 number, which can be thought of as a ratio with a power of 10 on the bottom.
So why does this fail?
from decimal import Decimal
from numbers import Rational
assert issubclass(Decimal, Rational)
Same reason float
isn't Rational
: while all non-infinite, non-NaN values of float
or Decimal
are rational, the types are intended to model real-number arithmetic. For example, they support square roots:
2.0**0.5
Decimal(2).sqrt()
which a class intended to model rational numbers or a subset of the rationals would not.
(Yes, okay, you can do 2**0.5
, but that coerces the int to a float.)