PEP 327: Decimal Data Type
Stephen Horne
steve at ninereeds.fsnet.co.uk
Sat Jan 31 20:33:40 CET 2004
On Sat, 31 Jan 2004 09:35:09 -0800, Josiah Carlson
<jcarlson at nospam.uci.edu> wrote:
>> If I needed more than that, I'd use a rational type - I speak from
>> experience as I set out to write a base N float library for C++ once
>> upon a time and ended up writing a rational instead. A rational, BTW,
>> isn't too bad to get working but that's as far as I got - doing it
>> well would probably take a lot of work. And if getting Base N floats
>> working was harder than for rationals, getting them to work well would
>> probably be an order of magnitude harder - for no real benefit to 99%
>> or more of users.
>
>I also wrote a rational type (last summer). It took around 45 minutes.
> Floating point takes a bit longer to get right.
Was your implementation the 'not too bad to get working' or the 'doing
it well'?
For instance, there is the greatest common divisor that you need for
normalising the rationals.
I used the Euclidean algorithm for the GCD. Not too bad, certainly
better than using prime factorisation, but as I understand it doing
the job well means using a better algorithm for this - though I never
did bother looking up the details.
Actually, as far as I remember, just doing the arbitrary length
integer division functions took me more than your 45 minutes. The long
division algorithm is simple in principle, but I seem to remember
messing up the decision of how many bits to shift the divisor after a
subtraction. Of course in Python, that's already done.
Maybe I was just having a bad day. Maybe I remember it worse than it
really was. Still, 45 minutes doesn't seem too realistic in my memory,
even for the 'not too bad to get working' case.
--
Steve Horne
steve at ninereeds dot fsnet dot co dot uk
More information about the Python-list
mailing list