Why isn't Python king of the hill?
pmartel at ne.mediaone.net
Sun Jun 3 02:15:43 CEST 2001
Geoffrey Gerrietts <geoff at homegain.com> wrote in message
news:mailman.991438863.26627.python-list at python.org...
> Martijn writes:
> > So there's fixed-point, decimal floating-point and rational
> > arithmetic..
> > And plain-old-confusing floats. :)
> > They all involve numbers with points in them.. and when you do math
> > with them, the answers may be different. That's about the extent
> > of my knowledge.
> Here's a five minute explanation, and maybe I can dig up a link to a quick
> tutorial on FP in general. (Ok, maybe it's more like a fifteen-minute
> explanation. Somebody stop me!) Someone who knows better ought to correct
> if I'm talking out my butt.
> Unbounded rationals
> Unbounded rationals would basically give the language the power to express
> any rational number accurately. I confess that I'm not sure how it would
> about doing that exactly. I THINK it would do so by maintaining an
> representation of the operation that gives rise to an infinitely-repeating
> decimal, and use that expression in conjunction with known mathematical
> properties to join in expressions with other values.
> This is pretty complicated to do, and, according to several people, has
> real problems when common repeating decimals get into your calculations --
> like .333 for instance. Your performance is highly variable depending on
> hard it is to keep track of the numbers you're using.
I'm not a Python expert, but the general idea is to have a class with two
variables, "Numerator" and "Denominator". For unbounded rationals, they
should be long integers. Arithmetic operations are done like fractional
arithmetic. Using the notation N/D to represent the unbpunded rational with
Mumerator = N and Denominator = D,
Addition: A/B + C/D = (A * D + B * C)/(B * D)
Multiplication: A/B * C/D = (A * C)/(B * D)
Division: A/B / C/D = A/B * D/C = (A * D)/(B * C)
It is traditional, though not absolutely necessary, to remove any common
factors from the Numerator and Denominator, thus:
1/6 + 1/4 = (1 * 4 + 6 * 1 )/(6 * 4 ) = 10/24 which would reduce to 5/12
If you use rational arithmetic, you avoid repeating decimals. 0.333333 is an
approximation for 1/3
> Geoff Gerrietts <geoff at homegain.com>
> Software Engineer, HomeGain.com
> 510-655-0800 x4320
More information about the Python-list