Finding size of Variable

Jussi Piitulainen jpiitula at ling.helsinki.fi
Wed Feb 12 10:35:22 CET 2014


Chris Angelico writes:
> On Wed, Feb 12, 2014 at 7:57 PM, Jussi Piitulainen wrote:
> >> In Python, integers have arbitrary precision, but floats, Fractions,
> >> and Decimals, don't. Nearly any operation on arbitrarily large
> >> numbers will be either more accurate or more efficient (maybe both)
> >> with integers than with any of the other types.
> >
> > Is that true about Fractions?
> 
> I'm not 100% sure if fraction.Fraction and decimal.Decimal ever limit
> the size or precision of their data, but certainly if they don't,
> it'll be at horrendous expense of performance. (Decimal can add and
> subtract in reasonable time complexity, but multiplication and
> division will get slow when you have huge numbers of digits. Fraction
> can multiply and divide efficiently, but will get crazily slow on
> addition and subtraction.) Integers are an optimized case in many
> ways. I can do accurate arbitrary-precision integer arithmetic without
> worrying about simple operations suddenly saturating the CPU. I can't
> do that with non-integers in any way.
> 
> It's not optimized for natural numbers (nonnegative integers), as
> negatives are just as cheap as positives, but it's certainly an
> optimization for integers.

Right. I don't know about Decimal, but I don't think there are any
precision restrictions in Fraction, other than running out of heap (or
possibly integer precision).

In my (quite limited) experience, the most expensive operation on both
exact rationals and exact integers has been the printing, in decimal,
of several screenfuls of digits. The actual calculations have taken a
couple of seconds and then I have wished that I could interrupt the
printing of a single number :)



More information about the Python-list mailing list