Re: Native type for storing fractions (e.g 1/3)?

From: "John D(dot) Burger" <john(at)mitre(dot)org>
To: "pgsql-general postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Native type for storing fractions (e.g 1/3)?
Date: 2007-03-15 14:46:07
Message-ID: 581EF10B-CAB4-4D5F-BDB8-D5DDA7DA8872@mitre.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message George Weaver 2007-03-15 14:47:27 Re: Is This A Set Based Solution?
Previous Message A. Kretschmer 2007-03-15 14:38:56 Re: PgSql on Vista?