Re: Proposal: Division operator for (interval / interval => double precision)

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: Gurjeet Singh <gurjeet(at)singh(dot)im>, Postgres Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Proposal: Division operator for (interval / interval => double precision)
Date: 2024-06-24 08:34:00
Message-ID: 79cecf2a6626b1da2f338f1ec8d613b7306af252.camel@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sun, 2024-06-23 at 17:57 -0700, Gurjeet Singh wrote:
> Is there a desire to have a division operator / that takes dividend
> and divisor of types interval, and results in a quotient of type
> double precision.
>
> This would be helpful in calculating how many times the divisor
> interval can fit into the dividend interval.
>
> To complement this division operator, it would be desirable to also
> have a remainder operator %.
>
> For example,
>
> ('365 days'::interval / '5 days'::interval) => 73
> ('365 days'::interval % '5 days'::interval) => 0
>
> ('365 days'::interval / '3 days'::interval) => 121
> ('365 days'::interval % '3 days'::interval) => 2

I think that is a good idea in principle, but I have one complaint,
and one thing should be discussed.

The complaint is that the result should be double precision or numeric.
I'd want the result of '1 minute' / '8 seconds' to be 7.5.
That would match how the multiplication operator works.

What should be settled is how to handle divisions that are not well defined.
For example, what is '1 year' / '1 day'?
- 365.24217, because that is the number of solar days in a solar year?
- 365, because we don't consider leap years?
- 360, because we use the usual conversion of 1 month -> 30 days?

Yours,
Laurenz Albe

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Dave Page 2024-06-24 08:44:57 Re: Meson far from ready on Windows
Previous Message Stepan Neretin 2024-06-24 08:30:32 Re: New function normal_rand_array function to contrib/tablefunc.