Re: Native type for storing fractions (e.g 1/3)? - Mailing list pgsql-general

From John D. Burger
Subject Re: Native type for storing fractions (e.g 1/3)?
Date
Msg-id 581EF10B-CAB4-4D5F-BDB8-D5DDA7DA8872@mitre.org
Whole thread Raw
In response to Re: Native type for storing fractions (e.g 1/3)?  (Stephane Bortzmeyer <bortzmeyer@nic.fr>)
Responses Re: Native type for storing fractions (e.g 1/3)?  (Ken Johanson <pg-user@kensystem.com>)
List pgsql-general
Stephane Bortzmeyer wrote:

> But he can write one in PostgreSQL quite easily. Rational numbers are
> always the first exercice in CS courses about Abstract Data Types :-)

It's a little tricky to get good performance for all the operations:

> The addition and subtraction operations are complex. They will
> require approximately two gcd operations, 3 divisions, 3
> multiplications and an addition on the underlying integer type.
> The multiplication and division operations require two gcd
> operations, two multiplications, and four divisions.  The
> comparison operations require two gcd operations, two
> multiplications, four divisions and a comparison in the worst
> case.  On the assumption that IntType comparisons are the cheapest
> of these operations (and that comparisons agains zero may be
> cheaper still), these operations have a number of special case
> optimisations to reduce the overhead where possible.  In
> particular, equality and inequality tests are only as expensive as
> two of the equivalent tests on the underlying integer type.

(From the Booost rational package - http://www.boost.org/libs/
rational/rational.html)

I'd try to link to an existing library that provides rationals, or
model my code closely after one.

- John D. Burger
   MITRE



pgsql-general by date:

Previous
From: "A. Kretschmer"
Date:
Subject: Re: PgSql on Vista?
Next
From: George Weaver
Date:
Subject: Re: Is This A Set Based Solution?