Re: Unsigned 64 bit integer to numeric

From: Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk>
To: Dmitry Dolgov <9erthalion6(at)gmail(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Unsigned 64 bit integer to numeric
Date: 2019-12-04 11:49:20
Message-ID: 87k17cb9ho.fsf@news-spur.riddles.org.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

>>>>> "Dmitry" == Dmitry Dolgov <9erthalion6(at)gmail(dot)com> writes:

Dmitry> Hi,

Dmitry> Probably a simple question, but I don't see a simple answer so
Dmitry> far. In one extension I want to convert uint64 into a numeric
Dmitry> to put it eventually into a jsonb object. As far as I see in
Dmitry> numeric.c there are functions only for signed int64. Is there a
Dmitry> way to achive this with uint64 (without duplicating significant
Dmitry> part of numeric implementation in the extension)?

Sure. Flip the top bit; convert the value as if signed; then subtract
-(2^63) from the result. (Easier to subtract -(2^63) than to add 2^63,
since the former can itself be represented in a signed int64 for easy
conversion to numeric.)

--
Andrew (irc:RhodiumToad)

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2019-12-04 12:08:08 Re: [HACKERS] Block level parallel vacuum
Previous Message PG Bug reporting form 2019-12-04 11:46:35 BUG #16147: postgresql 12.1 (from homebrew) - pg_restore -h localhost --jobs=2 crashes