How about adding rational fraction to Python?

Lie Lie.1296 at gmail.com
Tue Feb 26 13:29:18 CET 2008


On Feb 25, 11:18 pm, Carl Banks <pavlovevide... at gmail.com> wrote:
> On Feb 25, 9:41 am, Mensanator <mensana... at aol.com> wrote:
>
>
>
> > On Feb 25, 12:58�am, Carl Banks <pavlovevide... at gmail.com> wrote:
>
> > > On Feb 24, 10:56 pm, Mensanator <mensana... at aol.com> wrote:
>
> > > > But that doesn't mean they become less manageable than
> > > > other unlimited precision usages. Did you see my example
> > > > of the polynomial finder using Newton's Forward Differences
> > > > Method? The denominator's certainly don't settle out, neither
> > > > do they become unmanageable. And that's general mathematics.
>
> > > No, that's a specific algorithm. �That some random algorithm doesn't
> > > blow up the denominators to the point of disk thrashing doesn't mean
> > > they won't generally.
>
> > > Try doing numerical integration sometime with rationals, and tell me
> > > how that works out. �Try calculating compound interest and storing
> > > results for 1000 customers every month, and compare the size of your
> > > database before and after.
>
> > Nobody said rationals were the appropriate solution
> > to _every_ problem, just as floats and integers aren't
> > the appropriate solution to _every_ problem.
>
> I was answering your claim that rationals are appropriate for general
> mathematical uses.
>
> > Your argument is that I should be forced to use
> > an inappropriate type when rationals _are_
> > the appropriate solution.
>
> I don't know where you got that idea.
>
> My argument is that rationals aren't suitable for ordinary uses
> because they have poor performance and can easily blow up in your
> face, trash your disk, and crash your program (your whole system if
> you're on Windows).
>
> In other words, 3/4 in Python rightly yields a float and not a
> rational.
>

And it should, as floats have native CPU support, 3/4 (with __future__
import) should yields 0.75. Fractional is safe for many usage
scenario, but obviously it is not appropriate for all cases, just like
floats and decimals. I think this mailing list already have enough
absolutist, who can go to either side of the camp but doesn't want to
be in the middle.

In real world, money is probably best described by float or Decimals,
as non-computer money calculations are used to do rounding at 1 cents.
But the interest rate is probably better described by rationals.

In trigonometry, _perhaps_ the world would be better if trigonometric
functions returns rational. Perhaps it is better to use rational for
pi (ratio of diameter and circumferrence) and phi (golden ratio). But
I think angles is better described as floats nevertheless.

> J Cliff Dyer:
> I'm in the camp that believes that 3/4 does indeed yield the integer 0,
> but should be spelled 3//4 when that is the intention.

That's creepy for people that are new to programming and doesn't know
how CPUs work and are used to general mathematics. That means most
people. As programming language are now more accessible to regular
people without specialized Computer Science degree, it is a just
natural trend that computer arithmetic must be done in an expectable
manner as seen by those general population not by people who holds a
CS degree.



More information about the Python-list mailing list