How about adding rational fraction to Python?

M.-A. Lemburg mal at egenix.com
Mon Feb 25 10:36:48 EST 2008


On 2008-02-25 16:03, Steven D'Aprano wrote:
> On Sun, 24 Feb 2008 23:09:39 -0800, Carl Banks wrote:
> 
>> On Feb 25, 2:04 am, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
>>> Carl Banks <pavlovevide... at gmail.com> writes:
>>>> 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.
>>> Usually you would round to the nearest penny before storing in the
>>> database.
>> I throw it out there as a hypothetical, not as a real world example.
>> "This is why we don't (usually) use rationals for accounting."
> 
> But since accountants (usually) round to the nearest cent, accounting is 
> a *good* use-case for rationals. Decimal might be better, but floats are 
> worst.

That's not necessarily true in general: finance libraries usually try
to always do calculations at the best possible precision and then only
apply rounding at the very end of a calculation. Most of the time a float
is the best data type for this.

Accounting uses a somewhat different approach and one which various
between the different accounting standards and use cases. The decimal
type is usually better suited for this, since it supports various
ways of doing rounding.

Rationals are not always the best alternative, but they do help
in cases where you need to guarantee that the sum of all parts
is equal to the whole for all values. Combined with interval
arithmetic they go a long way towards more accurate calculations.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Feb 25 2008)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611



More information about the Python-list mailing list