Re: gmpy adapter

From: Daniel Popowich <danielpopowich(at)gmail(dot)com>
To: Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>
Cc: psycopg(at)postgresql(dot)org
Subject: Re: gmpy adapter
Date: 2011-03-01 17:03:28
Message-ID: 19821.9952.90507.737360@io.astro.umass.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg


Daniele Varrazzo writes:
> On Mon, Feb 28, 2011 at 11:07 PM, Daniele Varrazzo
> <daniele(dot)varrazzo(at)gmail(dot)com> wrote:
>
> > Also notice that float64 has 53 bits of precision, 15 full decimal
> > digits. If you don't need more precision when you write data in the
> > database (even if you have used more during calculations) you may just
> > use the repr(float()) of your mpq to write into the database.
>
> Or else, in the adapter you may use mpf to approximate the rational to
> the precision you need.
>
> >>> gmpy.mpq(2,3)
> mpq(2,3)
> >>> gmpy.mpf(_, 100)
> mpf('6.666666666666666666666666666666666666667e-1',100)
> >>> str(_)
> '0.6666666666666666666666666666666666666667'

Daniele,

Thanks for the ideas. One thing I didn't specify in my original post
is that the numeric columnss in my database are for monetary values,
so I can't use binary floating point representations in my python
code, else I risk inexactness and accumulated error in long
computations.

I need to use decimal floating point, e.g., decimal.Decimal (or
cdecimal for a C implementation) or a rational object, like
fractions.Fraction (or gmpy.mpq for a C impl).

I could use mpq for internal computations then use mpf for the final
conversion to the string I need for my adapter, but I want a single
type I can use throughout my code without such concerns.

I'm going to try cdecimal for now.

Thanks, again!

Cheers,

Dan

In response to

Responses

Browse psycopg by date

  From Date Subject
Next Message Daniele Varrazzo 2011-03-01 17:19:29 Re: gmpy adapter
Previous Message Daniele Varrazzo 2011-03-01 10:19:55 Re: gmpy adapter