1

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)
Eric
  • 95,302
  • 53
  • 242
  • 374
  • `assert` fails because `Decimal` is not a subclass of `Rational`. Why is it not a subclass of `Rational` is a different question. – DYZ Mar 16 '17 at 00:04
  • From a cursory glance at the decimal docs I see that this module does some kind of roundoff which fractions as far as I know don't. – Paul Panzer Mar 16 '17 at 00:08

1 Answers1

3

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.)

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • Right, my mistake is confusing "stores a rational number" and "supports only rational operations" – Eric Mar 16 '17 at 00:09