Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lSTgC-0006jH-1R for pgsql-hackers@arkaria.postgresql.org; Fri, 02 Apr 2021 23:58:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lSTgA-00077S-Sk for pgsql-hackers@arkaria.postgresql.org; Fri, 02 Apr 2021 23:58:18 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lSTgA-00077L-Le for pgsql-hackers@lists.postgresql.org; Fri, 02 Apr 2021 23:58:18 +0000 Received: from momjian.us ([72.94.173.45]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lSTg8-0001W1-JI for pgsql-hackers@postgresql.org; Fri, 02 Apr 2021 23:58:17 +0000 Received: from bruce by momjian.us with local (Exim 4.92) (envelope-from ) id 1lSTg7-0004WD-Nq; Fri, 02 Apr 2021 19:58:15 -0400 Date: Fri, 2 Apr 2021 19:58:15 -0400 From: Bruce Momjian To: John W Higgins Cc: PostgreSQL-development Subject: Re: Have I found an interval arithmetic bug? Message-ID: <20210402235815.GC29126@momjian.us> References: <20210402180549.GF9270@momjian.us> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Apr 2, 2021 at 02:00:03PM -0700, John W Higgins wrote: > On Fri, Apr 2, 2021 at 11:05 AM Bruce Momjian wrote: > While maybe there is an argument to fixing the negative/positive rounding issue > - there is no way this gets solved without breaking the current implementation > > select interval '0.3 years' + interval '0.4 years' - interval '0.7 years' + > interval '0.1 years' should not equal 0 but it certainly does. My new code returns 0.2 months for this, not zero: SELECT interval '0.3 years' + interval '0.4 years' - interval '0.7 years' + interval '0.1 years'; ?column? ---------- 2 mons which is also wrong since: SELECT interval '0.1 years'; interval ---------- 1 mon > Unless we take the concept of 0.3 years = 3 months and move to something along > the lines of  > > 1 year = 360 days > 1 month = 30 days  > > so therefore  > > 0.3 years = 360 days * 0.3 = 108 days = 3 months 18 days  > 0.4 years = 360 days * 0.4 = 144 days = 4 months 24 days > 0.7 years = 360 days * 0.7 = 252 days = 8 months 12 days > > Then, and only if we don't go to any more than tenths of a year, does the math > work. Probably this should resolve down to seconds and then work backwards - > but unless we're looking at breaking the entire way it currently resolves > things - I don't think this is of much value. > > Doing math on intervals is like doing math on rounded numbers - there is always > going to be a pile of issues because the level of precision just is not good > enough. I think the big question is what units do people want with fractional values. I have posted a follow-up email that spills only for one unit, which I think is the best approach. -- Bruce Momjian https://momjian.us EDB https://enterprisedb.com If only the physical world exists, free will is an illusion.