Re: integer square root function proposed

From: "Martin L(dot) Buchanan" <martinlbuchanan(at)gmail(dot)com>
To: Rob Sargent <robjsargent(at)gmail(dot)com>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: integer square root function proposed
Date: 2022-12-18 03:40:01
Message-ID: CAPfd9M=qBH0BKFs6ySnBnovG3=4wOVXcT1N1bnE7f6e+_N_usQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Dear Rob and all readers:

Generating prime numbers is one example where you use integer square root
in the inner loop, going from integer to integer.

Calculating an integer square root from an integer input may have a more
efficient algorithm than doing so in floating-point, with the caveat that
an underlying processor architecture may provide floating-point square root
instructions but not integer square root instructions. In that particular
case an implementation could use the floating-point instructions internally.

Some but not all programming languages provide isqrt directly, math.isqrt
in Python or isqrt in Common Lisp for example.

It would be a useful and convenient function and would not, I believe,
impair the other features of PostgreSQL in any way.

That said, as a PG novice (2+ years now), I completely defer to the greater
wisdom of those much more involved in PostgreSQL. So something for you all
to think about.

Best wishes, Happy Channukah, and Merry Christmas,

Martin L. Buchanan
software developer and writer since 1976
Laramie, WY

On Sat, Dec 17, 2022 at 8:20 PM Rob Sargent <robjsargent(at)gmail(dot)com> wrote:

> On 12/17/22 19:39, Martin L. Buchanan wrote:
>
> Dear PostgreSQL colleagues:
>
> I have just joined this, my first PG mailing list.
>
> Reading the documentation I found no built-in function for integer square
> root, requiring a sequence of:
>
> floor(sqrt(foo))::integer
>
> to go from an integer to the integer square root as an integer.
>
> If PG leaders smile on this suggestion, could we have *isqrt*(foo) where
> foo has a numeric type and the result type is the same numeric type, with
> the result type being the integer square root for foo >= 0 and producing an
> error if negative? (And with infinities and NaNs processed as described in
> 8.1 Numeric Types).
>
> I did not see an explicit enhancement request list in the PG mailing
> lists. Recommendations for a more specific list to post this to are welcome.
>
>
>
> I suspect the majority are comfortable getting a non-integer result for
> the square root of a number. What environment have you which needs
> (sqrt(a) * sqrt(a)) < a-epsilon?
>
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Rob Sargent 2022-12-18 03:56:16 Re: integer square root function proposed
Previous Message Rob Sargent 2022-12-18 03:20:21 Re: integer square root function proposed